2006-04-15 01:17:13 +00:00
|
|
|
<?php
|
|
|
|
|
2006-07-22 12:53:04 +00:00
|
|
|
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
|
|
|
require_once 'HTMLPurifier/Lexer/PEARSax3.php';
|
2006-07-22 00:13:08 +00:00
|
|
|
|
2006-07-22 15:38:41 +00:00
|
|
|
class HTMLPurifier_LexerTest extends UnitTestCase
|
2006-04-15 01:17:13 +00:00
|
|
|
{
|
|
|
|
|
2006-07-22 13:50:05 +00:00
|
|
|
var $DirectLex, $PEARSax3, $DOMLex;
|
|
|
|
var $_has_dom;
|
2006-04-15 01:17:13 +00:00
|
|
|
|
|
|
|
function setUp() {
|
2006-07-22 13:50:05 +00:00
|
|
|
$this->DirectLex = new HTMLPurifier_Lexer_DirectLex();
|
|
|
|
$this->PEARSax3 = new HTMLPurifier_Lexer_PEARSax3();
|
|
|
|
|
|
|
|
$this->_has_dom = version_compare(PHP_VERSION, '5', '>=');
|
2006-07-22 14:00:52 +00:00
|
|
|
|
|
|
|
if ($this->_has_dom) {
|
|
|
|
require_once 'HTMLPurifier/Lexer/DOMLex.php';
|
|
|
|
$this->DOMLex = new HTMLPurifier_Lexer_DOMLex();
|
|
|
|
}
|
|
|
|
|
2006-04-15 01:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_tokenizeHTML() {
|
|
|
|
|
2006-04-15 02:19:27 +00:00
|
|
|
$input = array();
|
|
|
|
$expect = array();
|
|
|
|
$sax_expect = array();
|
|
|
|
|
|
|
|
$input[0] = '';
|
|
|
|
$expect[0] = array();
|
2006-04-15 01:17:13 +00:00
|
|
|
|
2006-04-15 02:19:27 +00:00
|
|
|
$input[1] = 'This is regular text.';
|
|
|
|
$expect[1] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Text('This is regular text.')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
2006-04-15 02:19:27 +00:00
|
|
|
$input[2] = 'This is <b>bold</b> text';
|
|
|
|
$expect[2] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Text('This is ')
|
|
|
|
,new HTMLPurifier_Token_Start('b', array())
|
|
|
|
,new HTMLPurifier_Token_Text('bold')
|
|
|
|
,new HTMLPurifier_Token_End('b')
|
|
|
|
,new HTMLPurifier_Token_Text(' text')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
2006-04-15 02:19:27 +00:00
|
|
|
$input[3] = '<DIV>Totally rad dude. <b>asdf</b></div>';
|
|
|
|
$expect[3] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Start('DIV', array())
|
|
|
|
,new HTMLPurifier_Token_Text('Totally rad dude. ')
|
|
|
|
,new HTMLPurifier_Token_Start('b', array())
|
|
|
|
,new HTMLPurifier_Token_Text('asdf')
|
|
|
|
,new HTMLPurifier_Token_End('b')
|
|
|
|
,new HTMLPurifier_Token_End('div')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
2006-07-22 13:50:05 +00:00
|
|
|
// [XML-INVALID]
|
2006-04-15 02:19:27 +00:00
|
|
|
$input[4] = '<asdf></asdf><d></d><poOloka><poolasdf><ds></asdf></ASDF>';
|
|
|
|
$expect[4] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Start('asdf')
|
|
|
|
,new HTMLPurifier_Token_End('asdf')
|
|
|
|
,new HTMLPurifier_Token_Start('d')
|
|
|
|
,new HTMLPurifier_Token_End('d')
|
|
|
|
,new HTMLPurifier_Token_Start('poOloka')
|
|
|
|
,new HTMLPurifier_Token_Start('poolasdf')
|
|
|
|
,new HTMLPurifier_Token_Start('ds')
|
|
|
|
,new HTMLPurifier_Token_End('asdf')
|
|
|
|
,new HTMLPurifier_Token_End('ASDF')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
2006-07-22 13:50:05 +00:00
|
|
|
// DOM is different because it condenses empty tags into REAL empty ones
|
|
|
|
// as well as makes it well-formed
|
|
|
|
$dom_expect[4] = array(
|
|
|
|
new HTMLPurifier_Token_Empty('asdf')
|
|
|
|
,new HTMLPurifier_Token_Empty('d')
|
|
|
|
,new HTMLPurifier_Token_Start('pooloka')
|
|
|
|
,new HTMLPurifier_Token_Start('poolasdf')
|
|
|
|
,new HTMLPurifier_Token_Empty('ds')
|
|
|
|
,new HTMLPurifier_Token_End('poolasdf')
|
|
|
|
,new HTMLPurifier_Token_End('pooloka')
|
|
|
|
);
|
2006-04-15 01:17:13 +00:00
|
|
|
|
2006-04-15 02:19:27 +00:00
|
|
|
$input[5] = '<a'."\t".'href="foobar.php"'."\n".'title="foo!">Link to <b id="asdf">foobar</b></a>';
|
|
|
|
$expect[5] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Start('a',array('href'=>'foobar.php','title'=>'foo!'))
|
|
|
|
,new HTMLPurifier_Token_Text('Link to ')
|
|
|
|
,new HTMLPurifier_Token_Start('b',array('id'=>'asdf'))
|
|
|
|
,new HTMLPurifier_Token_Text('foobar')
|
|
|
|
,new HTMLPurifier_Token_End('b')
|
|
|
|
,new HTMLPurifier_Token_End('a')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
2006-04-15 02:19:27 +00:00
|
|
|
$input[6] = '<br />';
|
|
|
|
$expect[6] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Empty('br')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
2006-07-22 13:50:05 +00:00
|
|
|
// [SGML-INVALID] [RECOVERABLE]
|
2006-04-15 02:19:27 +00:00
|
|
|
$input[7] = '<!-- Comment --> <!-- not so well formed --->';
|
|
|
|
$expect[7] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Comment(' Comment ')
|
|
|
|
,new HTMLPurifier_Token_Text(' ')
|
|
|
|
,new HTMLPurifier_Token_Comment(' not so well formed -')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
2006-04-15 02:19:27 +00:00
|
|
|
$sax_expect[7] = false; // we need to figure out proper comment output
|
2006-04-15 01:17:13 +00:00
|
|
|
|
2006-07-22 13:50:05 +00:00
|
|
|
// [SGML-INVALID]
|
2006-04-15 02:19:27 +00:00
|
|
|
$input[8] = '<a href=""';
|
|
|
|
$expect[8] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Text('<a href=""')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
2006-04-15 02:19:27 +00:00
|
|
|
// SAX parses it into a tag
|
|
|
|
$sax_expect[8] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Start('a', array('href'=>''))
|
2006-04-15 02:19:27 +00:00
|
|
|
);
|
2006-07-22 13:50:05 +00:00
|
|
|
// DOM parses it into an empty tag
|
|
|
|
$dom_expect[8] = array(
|
|
|
|
new HTMLPurifier_Token_Empty('a', array('href'=>''))
|
|
|
|
);
|
2006-04-15 02:19:27 +00:00
|
|
|
|
|
|
|
$input[9] = '<b>';
|
|
|
|
$expect[9] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Text('<b>')
|
2006-04-16 03:00:05 +00:00
|
|
|
);
|
|
|
|
$sax_expect[9] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Text('<')
|
|
|
|
,new HTMLPurifier_Token_Text('b')
|
|
|
|
,new HTMLPurifier_Token_Text('>')
|
2006-04-15 02:19:27 +00:00
|
|
|
);
|
2006-07-20 02:43:06 +00:00
|
|
|
// note that SAX can clump text nodes together. We won't be
|
|
|
|
// too picky though
|
2006-04-15 01:17:13 +00:00
|
|
|
|
2006-07-22 13:50:05 +00:00
|
|
|
// [SGML-INVALID]
|
2006-04-16 00:23:35 +00:00
|
|
|
$input[10] = '<a "=>';
|
2006-07-22 22:48:07 +00:00
|
|
|
// We barf on this, aim for no attributes
|
2006-04-16 00:23:35 +00:00
|
|
|
$expect[10] = array(
|
2006-07-21 11:27:54 +00:00
|
|
|
new HTMLPurifier_Token_Start('a', array('"' => ''))
|
2006-04-16 00:23:35 +00:00
|
|
|
);
|
2006-07-22 22:48:07 +00:00
|
|
|
// DOM correctly has no attributes, but also closes the tag
|
2006-07-22 13:50:05 +00:00
|
|
|
$dom_expect[10] = array(
|
|
|
|
new HTMLPurifier_Token_Empty('a')
|
|
|
|
);
|
2006-07-22 22:48:07 +00:00
|
|
|
// SAX barfs on this
|
|
|
|
$sax_expect[10] = array(
|
|
|
|
new HTMLPurifier_Token_Start('a', array('"' => ''))
|
|
|
|
);
|
2006-04-16 00:23:35 +00:00
|
|
|
|
2006-07-20 02:43:06 +00:00
|
|
|
// [INVALID] [RECOVERABLE]
|
|
|
|
$input[11] = '"';
|
2006-07-21 11:27:54 +00:00
|
|
|
$expect[11] = array( new HTMLPurifier_Token_Text('"') );
|
2006-07-20 02:43:06 +00:00
|
|
|
|
|
|
|
// compare with this valid one:
|
|
|
|
$input[12] = '"';
|
2006-07-21 11:27:54 +00:00
|
|
|
$expect[12] = array( new HTMLPurifier_Token_Text('"') );
|
2006-07-20 02:43:06 +00:00
|
|
|
$sax_expect[12] = false;
|
|
|
|
// SAX chokes on this? We do have entity parsing on, so it should work!
|
|
|
|
|
2006-04-15 02:19:27 +00:00
|
|
|
foreach($input as $i => $discard) {
|
2006-07-22 12:53:04 +00:00
|
|
|
$result = $this->DirectLex->tokenizeHTML($input[$i]);
|
2006-07-22 13:50:05 +00:00
|
|
|
$this->assertEqual($expect[$i], $result, 'Test '.$i.': %s');
|
2006-04-15 01:17:13 +00:00
|
|
|
paintIf($result, $expect[$i] != $result);
|
2006-04-15 01:47:12 +00:00
|
|
|
|
2006-04-15 02:19:27 +00:00
|
|
|
// assert unless I say otherwise
|
2006-07-22 12:53:04 +00:00
|
|
|
$sax_result = $this->PEARSax3->tokenizeHTML($input[$i]);
|
2006-04-15 02:19:27 +00:00
|
|
|
if (!isset($sax_expect[$i])) {
|
|
|
|
// by default, assert with normal result
|
2006-07-22 13:50:05 +00:00
|
|
|
$this->assertEqual($expect[$i], $sax_result, 'Test '.$i.': %s');
|
2006-04-15 02:19:27 +00:00
|
|
|
paintIf($sax_result, $expect[$i] != $sax_result);
|
|
|
|
} elseif ($sax_expect[$i] === false) {
|
|
|
|
// assertions were turned off, optionally dump
|
|
|
|
// paintIf($sax_expect, $i == NUMBER);
|
|
|
|
} else {
|
|
|
|
// match with a custom SAX result array
|
2006-07-22 13:50:05 +00:00
|
|
|
$this->assertEqual($sax_expect[$i], $sax_result, 'Test '.$i.': %s');
|
2006-04-15 02:19:27 +00:00
|
|
|
paintIf($sax_result, $sax_expect[$i] != $sax_result);
|
|
|
|
}
|
2006-07-22 13:50:05 +00:00
|
|
|
if ($this->_has_dom) {
|
|
|
|
$dom_result = $this->DOMLex->tokenizeHTML($input[$i]);
|
|
|
|
// same structure as SAX
|
|
|
|
if (!isset($dom_expect[$i])) {
|
|
|
|
$this->assertEqual($expect[$i], $dom_result, 'Test '.$i.': %s');
|
|
|
|
paintIf($dom_result, $expect[$i] != $dom_result);
|
|
|
|
} elseif ($dom_expect[$i] === false) {
|
|
|
|
// paintIf($dom_result, $i == NUMBER);
|
|
|
|
} else {
|
|
|
|
$this->assertEqual($dom_expect[$i], $dom_result, 'Test '.$i.': %s');
|
|
|
|
paintIf($dom_result, $dom_expect[$i] != $dom_result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-15 01:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|