0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-03-24 22:57:03 +00:00

More unit test refactoring into seperate methods.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1380 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-08-16 06:48:24 +00:00
parent a19f30fdcf
commit 9881a34712
3 changed files with 436 additions and 347 deletions

View File

@ -35,7 +35,7 @@ class HTMLPurifier_AttrValidator_ErrorsTest extends HTMLPurifier_ErrorsHarness
$this->invoke($token); $this->invoke($token);
} }
// to lazy to check for global post and global pre // too lazy to check for global post and global pre
function testAttributeRemoved() { function testAttributeRemoved() {
$this->expectErrorCollection(E_ERROR, 'AttrValidator: Attribute removed'); $this->expectErrorCollection(E_ERROR, 'AttrValidator: Attribute removed');

View File

@ -17,7 +17,7 @@ class HTMLPurifier_EntityLookupTest extends HTMLPurifier_Harness
// special char // special char
$this->assertIdentical('"', $lookup->table['quot']); $this->assertIdentical('"', $lookup->table['quot']);
$this->assertIdentical('“', $lookup->table['ldquo']); $this->assertIdentical('“', $lookup->table['ldquo']);
$this->assertIdentical('<', $lookup->table['lt']); //expressed strangely $this->assertIdentical('<', $lookup->table['lt']); // expressed strangely in source file
// symbol char // symbol char
$this->assertIdentical('θ', $lookup->table['theta']); $this->assertIdentical('θ', $lookup->table['theta']);

View File

@ -5,71 +5,95 @@ require_once 'HTMLPurifier/Lexer/DirectLex.php';
class HTMLPurifier_LexerTest extends HTMLPurifier_Harness class HTMLPurifier_LexerTest extends HTMLPurifier_Harness
{ {
var $Lexer;
var $DirectLex, $PEARSax3, $DOMLex;
var $_entity_lookup;
var $_has_pear = false; var $_has_pear = false;
var $_has_dom = false;
function setUp() { function HTMLPurifier_LexerTest() {
$this->Lexer = new HTMLPurifier_Lexer(); parent::HTMLPurifier_Harness();
// E_STRICT = 2048, int used for PHP4 compat: this check disables
$this->DirectLex = new HTMLPurifier_Lexer_DirectLex(); // PEAR if PHP 5 strict mode is on, since the class is not strict safe
if (
// E_STRICT = 2048, int used for PHP4 compat $GLOBALS['HTMLPurifierTest']['PEAR'] &&
if ( $GLOBALS['HTMLPurifierTest']['PEAR'] && ((error_reporting() & 2048) != 2048) // ought to be a better way
((error_reporting() & 2048) != 2048)
) { ) {
$this->_has_pear = true;
require_once 'HTMLPurifier/Lexer/PEARSax3.php'; require_once 'HTMLPurifier/Lexer/PEARSax3.php';
$this->PEARSax3 = new HTMLPurifier_Lexer_PEARSax3(); $this->_has_pear = true;
}
} }
$this->_has_dom = version_compare(PHP_VERSION, '5', '>='); // HTMLPurifier_Lexer::create() --------------------------------------------
if ($this->_has_dom) {
require_once 'HTMLPurifier/Lexer/DOMLex.php';
$this->DOMLex = new HTMLPurifier_Lexer_DOMLex();
}
$this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
}
function test_create() { function test_create() {
$config = HTMLPurifier_Config::create(array('Core.MaintainLineNumbers' => true)); $this->config->set('Core', 'MaintainLineNumbers', true);
$lexer = HTMLPurifier_Lexer::create($config); $lexer = HTMLPurifier_Lexer::create($this->config);
$this->assertIsA($lexer, 'HTMLPurifier_Lexer_DirectLex'); $this->assertIsA($lexer, 'HTMLPurifier_Lexer_DirectLex');
} }
// HTMLPurifier_Lexer->parseData() -----------------------------------------
function assertParseData($input, $expect = true) {
if ($expect === true) $expect = $input;
$lexer = new HTMLPurifier_Lexer();
$this->assertIdentical($expect, $lexer->parseData($input));
}
function test_parseData_plainText() {
$this->assertParseData('asdf');
}
function test_parseData_ampersandEntity() {
$this->assertParseData('&amp;', '&');
}
function test_parseData_quotEntity() {
$this->assertParseData('&quot;', '"');
}
function test_parseData_aposNumericEntity() {
$this->assertParseData('&#039;', "'");
}
function test_parseData_aposCompactNumericEntity() {
$this->assertParseData('&#39;', "'");
}
function test_parseData_adjacentAmpersandEntities() {
$this->assertParseData('&amp;&amp;&amp;', '&&&');
}
function test_parseData_trailingUnescapedAmpersand() {
$this->assertParseData('&amp;&', '&&');
}
function test_parseData_internalUnescapedAmpersand() {
$this->assertParseData('Procter & Gamble');
}
function test_parseData_improperEntityFaultToleranceTest() {
$this->assertParseData('&#x2D;');
}
// HTMLPurifier_Lexer->extractBody() ---------------------------------------
function assertExtractBody($text, $extract = true) { function assertExtractBody($text, $extract = true) {
$result = $this->Lexer->extractBody($text); $lexer = new HTMLPurifier_Lexer();
$result = $lexer->extractBody($text);
if ($extract === true) $extract = $text; if ($extract === true) $extract = $text;
$this->assertIdentical($extract, $result); $this->assertIdentical($extract, $result);
} }
function test_parseData() { function test_extractBody_noBodyTags() {
$HP =& $this->Lexer; $this->assertExtractBody('<b>Bold</b>');
$this->assertIdentical('asdf', $HP->parseData('asdf'));
$this->assertIdentical('&', $HP->parseData('&amp;'));
$this->assertIdentical('"', $HP->parseData('&quot;'));
$this->assertIdentical("'", $HP->parseData('&#039;'));
$this->assertIdentical("'", $HP->parseData('&#39;'));
$this->assertIdentical('&&&', $HP->parseData('&amp;&amp;&amp;'));
$this->assertIdentical('&&', $HP->parseData('&amp;&')); // [INVALID]
$this->assertIdentical('Procter & Gamble',
$HP->parseData('Procter & Gamble')); // [INVALID]
// This is not special, thus not converted. Test of fault tolerance,
// realistically speaking, this should never happen
$this->assertIdentical('&#x2D;', $HP->parseData('&#x2D;'));
} }
function test_extractBody_lowercaseBodyTags() {
function test_extractBody() {
$this->assertExtractBody('<b>Bold</b>');
$this->assertExtractBody('<html><body><b>Bold</b></body></html>', '<b>Bold</b>'); $this->assertExtractBody('<html><body><b>Bold</b></body></html>', '<b>Bold</b>');
}
function test_extractBody_uppercaseBodyTags() {
$this->assertExtractBody('<HTML><BODY><B>Bold</B></BODY></HTML>', '<B>Bold</B>'); $this->assertExtractBody('<HTML><BODY><B>Bold</B></BODY></HTML>', '<B>Bold</B>');
}
function test_extractBody_realisticUseCase() {
$this->assertExtractBody( $this->assertExtractBody(
'<?xml version="1.0" '<?xml version="1.0"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
@ -97,150 +121,208 @@ class HTMLPurifier_LexerTest extends HTMLPurifier_Harness
</div> </div>
</form> </form>
'); ');
$this->assertExtractBody('<html><body bgcolor="#F00"><b>Bold</b></body></html>', '<b>Bold</b>');
$this->assertExtractBody('<body>asdf'); // not closed, don't accept
} }
function test_tokenizeHTML() { function test_extractBody_bodyWithAttributes() {
$this->assertExtractBody('<html><body bgcolor="#F00"><b>Bold</b></body></html>', '<b>Bold</b>');
}
$input = array(); function test_extractBody_preserveUnclosedBody() {
$expect = array(); $this->assertExtractBody('<body>asdf'); // not closed, don't accept
$sax_expect = array(); }
$config = array();
$input[0] = ''; // HTMLPurifier_Lexer->tokenizeHTML() --------------------------------------
$expect[0] = array();
$input[1] = 'This is regular text.'; function assertTokenization($input, $expect, $alt_expect = array()) {
$expect[1] = array( $lexers = array();
$lexers['DirectLex'] = new HTMLPurifier_Lexer_DirectLex();
if ($this->_has_pear) $lexers['PEARSax3'] = new HTMLPurifier_Lexer_PEARSax3();
if (version_compare(PHP_VERSION, "5", ">=") && class_exists('DOMDocument')) {
$lexers['DOMLex'] = new HTMLPurifier_Lexer_DOMLex();
}
foreach ($lexers as $name => $lexer) {
$result = $lexer->tokenizeHTML($input, $this->config, $this->context);
if (isset($alt_expect[$name])) {
if ($alt_expect[$name] === false) continue;
$this->assertIdentical($result, $alt_expect[$name]);
} else {
$this->assertIdentical($result, $expect);
}
}
}
function test_tokenizeHTML_emptyInput() {
$this->assertTokenization('', array());
}
function test_tokenizeHTML_plainText() {
$this->assertTokenization(
'This is regular text.',
array(
new HTMLPurifier_Token_Text('This is regular text.') new HTMLPurifier_Token_Text('This is regular text.')
)
); );
}
$input[2] = 'This is <b>bold</b> text'; function test_tokenizeHTML_textAndTags() {
$expect[2] = array( $this->assertTokenization(
new HTMLPurifier_Token_Text('This is ') 'This is <b>bold</b> text',
,new HTMLPurifier_Token_Start('b', array()) array(
,new HTMLPurifier_Token_Text('bold') new HTMLPurifier_Token_Text('This is '),
,new HTMLPurifier_Token_End('b') new HTMLPurifier_Token_Start('b', array()),
,new HTMLPurifier_Token_Text(' text') new HTMLPurifier_Token_Text('bold'),
new HTMLPurifier_Token_End('b'),
new HTMLPurifier_Token_Text(' text'),
)
); );
}
$input[3] = '<DIV>Totally rad dude. <b>asdf</b></div>'; function test_tokenizeHTML_normalizeCase() {
$expect[3] = array( $this->assertTokenization(
new HTMLPurifier_Token_Start('DIV', array()) '<DIV>Totally rad dude. <b>asdf</b></div>',
,new HTMLPurifier_Token_Text('Totally rad dude. ') array(
,new HTMLPurifier_Token_Start('b', array()) new HTMLPurifier_Token_Start('DIV', array()),
,new HTMLPurifier_Token_Text('asdf') new HTMLPurifier_Token_Text('Totally rad dude. '),
,new HTMLPurifier_Token_End('b') new HTMLPurifier_Token_Start('b', array()),
,new HTMLPurifier_Token_End('div') new HTMLPurifier_Token_Text('asdf'),
new HTMLPurifier_Token_End('b'),
new HTMLPurifier_Token_End('div'),
)
); );
}
// [XML-INVALID] function test_tokenizeHTML_notWellFormed() {
$input[4] = '<asdf></asdf><d></d><poOloka><poolasdf><ds></asdf></ASDF>'; $this->assertTokenization(
$expect[4] = array( '<asdf></asdf><d></d><poOloka><poolasdf><ds></asdf></ASDF>',
new HTMLPurifier_Token_Start('asdf') array(
,new HTMLPurifier_Token_End('asdf') new HTMLPurifier_Token_Start('asdf'),
,new HTMLPurifier_Token_Start('d') new HTMLPurifier_Token_End('asdf'),
,new HTMLPurifier_Token_End('d') new HTMLPurifier_Token_Start('d'),
,new HTMLPurifier_Token_Start('poOloka') new HTMLPurifier_Token_End('d'),
,new HTMLPurifier_Token_Start('poolasdf') new HTMLPurifier_Token_Start('poOloka'),
,new HTMLPurifier_Token_Start('ds') new HTMLPurifier_Token_Start('poolasdf'),
,new HTMLPurifier_Token_End('asdf') new HTMLPurifier_Token_Start('ds'),
,new HTMLPurifier_Token_End('ASDF') new HTMLPurifier_Token_End('asdf'),
); new HTMLPurifier_Token_End('ASDF'),
// DOM is different because it condenses empty tags into REAL empty ones ),
// as well as makes it well-formed array(
$dom_expect[4] = array( // DOMLex automatically closes invalid tags
new HTMLPurifier_Token_Empty('asdf') 'DOMLex' => array(
,new HTMLPurifier_Token_Empty('d') new HTMLPurifier_Token_Empty('asdf'),
,new HTMLPurifier_Token_Start('pooloka') new HTMLPurifier_Token_Empty('d'),
,new HTMLPurifier_Token_Start('poolasdf') new HTMLPurifier_Token_Start('pooloka'),
,new HTMLPurifier_Token_Empty('ds') new HTMLPurifier_Token_Start('poolasdf'),
,new HTMLPurifier_Token_End('poolasdf') new HTMLPurifier_Token_Empty('ds'),
,new HTMLPurifier_Token_End('pooloka') new HTMLPurifier_Token_End('poolasdf'),
new HTMLPurifier_Token_End('pooloka'),
),
)
); );
}
$input[5] = '<a'."\t".'href="foobar.php"'."\n".'title="foo!">Link to <b id="asdf">foobar</b></a>'; function test_tokenizeHTML_whitespaceInTag() {
$expect[5] = array( $this->assertTokenization(
new HTMLPurifier_Token_Start('a',array('href'=>'foobar.php','title'=>'foo!')) '<a'."\t".'href="foobar.php"'."\n".'title="foo!">Link to <b id="asdf">foobar</b></a>',
,new HTMLPurifier_Token_Text('Link to ') array(
,new HTMLPurifier_Token_Start('b',array('id'=>'asdf')) new HTMLPurifier_Token_Start('a',array('href'=>'foobar.php','title'=>'foo!')),
,new HTMLPurifier_Token_Text('foobar') new HTMLPurifier_Token_Text('Link to '),
,new HTMLPurifier_Token_End('b') new HTMLPurifier_Token_Start('b',array('id'=>'asdf')),
,new HTMLPurifier_Token_End('a') new HTMLPurifier_Token_Text('foobar'),
new HTMLPurifier_Token_End('b'),
new HTMLPurifier_Token_End('a'),
)
); );
}
$input[6] = '<br />'; function test_tokenizeHTML_emptyTag() {
$expect[6] = array( $this->assertTokenization(
new HTMLPurifier_Token_Empty('br') '<br />',
array( new HTMLPurifier_Token_Empty('br') )
); );
}
// [SGML-INVALID] [RECOVERABLE] function test_tokenizeHTML_comment() {
$input[7] = '<!-- Comment --> <!-- not so well formed --->'; $this->assertTokenization(
$expect[7] = array( '<!-- Comment -->',
new HTMLPurifier_Token_Comment(' Comment ') array( new HTMLPurifier_Token_Comment(' Comment ') )
,new HTMLPurifier_Token_Text(' ')
,new HTMLPurifier_Token_Comment(' not so well formed -')
); );
$sax_expect[7] = false; // we need to figure out proper comment output }
// [SGML-INVALID] function test_tokenizeHTML_malformedComment() {
$input[8] = '<a href=""'; $this->assertTokenization(
$expect[8] = array( '<!-- not so well formed --->',
new HTMLPurifier_Token_Text('<a href=""') array( new HTMLPurifier_Token_Comment(' not so well formed -') ),
); array(
// SAX parses it into a tag 'PEARSax3' => false, // behavior is undefined
$sax_expect[8] = array( )
new HTMLPurifier_Token_Start('a', array('href'=>''))
);
// DOM parses it into an empty tag
$dom_expect[8] = array(
new HTMLPurifier_Token_Empty('a', array('href'=>''))
); );
}
$input[9] = '&lt;b&gt;'; function test_tokenizeHTML_unterminatedTag() {
$expect[9] = array( $this->assertTokenization(
'<a href=""',
array( new HTMLPurifier_Token_Text('<a href=""') ),
array(
// I like our behavior better, but it's non-standard
'DOMLex' => array( new HTMLPurifier_Token_Empty('a', array('href'=>'')) ),
'PEARSax3' => array( new HTMLPurifier_Token_Start('a', array('href'=>'')) ),
)
);
}
function test_tokenizeHTML_specialEntities() {
$this->assertTokenization(
'&lt;b&gt;',
array(
new HTMLPurifier_Token_Text('<b>') new HTMLPurifier_Token_Text('<b>')
),
array(
// it is possible to configure PEARSax3 to clump nodes together,
// I just don't know how
'PEARSax3' => array(
new HTMLPurifier_Token_Text('<'),
new HTMLPurifier_Token_Text('b'),
new HTMLPurifier_Token_Text('>'),
)
)
); );
$sax_expect[9] = array( }
new HTMLPurifier_Token_Text('<')
,new HTMLPurifier_Token_Text('b')
,new HTMLPurifier_Token_Text('>')
);
// note that SAX can clump text nodes together. We won't be
// too picky though
// [SGML-INVALID] function test_tokenizeHTML_earlyQuote() {
$input[10] = '<a "=>'; $this->assertTokenization(
// We barf on this, aim for no attributes '<a "=>',
$expect[10] = array( array( new HTMLPurifier_Token_Empty('a') ),
array(
// we barf on this input
'DirectLex' => $tokens = array(
new HTMLPurifier_Token_Start('a', array('"' => '')) new HTMLPurifier_Token_Start('a', array('"' => ''))
),
'PEARSax3' => $tokens,
)
); );
// DOM correctly has no attributes, but also closes the tag }
$dom_expect[10] = array(
new HTMLPurifier_Token_Empty('a') function test_tokenizeHTML_unescapedQuote() {
$this->assertTokenization(
'"',
array( new HTMLPurifier_Token_Text('"') )
); );
// SAX barfs on this }
$sax_expect[10] = array(
new HTMLPurifier_Token_Start('a', array('"' => '')) function test_tokenizeHTML_escapedQuote() {
$this->assertTokenization(
'&quot;',
array( new HTMLPurifier_Token_Text('"') )
); );
}
// [INVALID] [RECOVERABLE] function test_tokenizeHTML_cdata() {
$input[11] = '"'; $this->assertTokenization(
$expect[11] = array( new HTMLPurifier_Token_Text('"') ); '<![CDATA[You <b>can&#39;t</b> get me!]]>',
array( new HTMLPurifier_Token_Text('You <b>can&#39;t</b> get me!') ),
// compare with this valid one: array(
$input[12] = '&quot;'; // PEAR splits up all of the CDATA
$expect[12] = array( new HTMLPurifier_Token_Text('"') ); 'PEARSax3' => array(
$sax_expect[12] = false; // choked!
// CDATA sections!
$input[13] = '<![CDATA[You <b>can&#39;t</b> get me!]]>';
$expect[13] = array( new HTMLPurifier_Token_Text(
'You <b>can&#39;t</b> get me!' // raw
) );
$sax_expect[13] = array( // SAX has a seperate call for each entity
new HTMLPurifier_Token_Text('You '), new HTMLPurifier_Token_Text('You '),
new HTMLPurifier_Token_Text('<'), new HTMLPurifier_Token_Text('<'),
new HTMLPurifier_Token_Text('b'), new HTMLPurifier_Token_Text('b'),
@ -251,155 +333,162 @@ class HTMLPurifier_LexerTest extends HTMLPurifier_Harness
new HTMLPurifier_Token_Text('<'), new HTMLPurifier_Token_Text('<'),
new HTMLPurifier_Token_Text('/b'), new HTMLPurifier_Token_Text('/b'),
new HTMLPurifier_Token_Text('>'), new HTMLPurifier_Token_Text('>'),
new HTMLPurifier_Token_Text(' get me!') new HTMLPurifier_Token_Text(' get me!'),
),
)
); );
}
$char_theta = $this->_entity_lookup->table['theta']; function test_tokenizeHTML_characterEntity() {
$char_rarr = $this->_entity_lookup->table['rarr']; $this->assertTokenization(
'&theta;',
array( new HTMLPurifier_Token_Text("\xCE\xB8") )
);
}
// test entity replacement function test_tokenizeHTML_characterEntityInCDATA() {
$input[14] = '&theta;'; $this->assertTokenization(
$expect[14] = array( new HTMLPurifier_Token_Text($char_theta) ); '<![CDATA[&rarr;]]>',
array( new HTMLPurifier_Token_Text("&rarr;") ),
// test that entities aren't replaced in CDATA sections array(
$input[15] = '&theta; <![CDATA[&rarr;]]>'; 'PEARSax3' => array(
$expect[15] = array( new HTMLPurifier_Token_Text($char_theta . ' &rarr;') );
$sax_expect[15] = array(
new HTMLPurifier_Token_Text($char_theta . ' '),
new HTMLPurifier_Token_Text('&'), new HTMLPurifier_Token_Text('&'),
new HTMLPurifier_Token_Text('rarr;') new HTMLPurifier_Token_Text('rarr;'),
),
)
); );
}
// test entity resolution in attributes function test_tokenizeHTML_entityInAttribute() {
$input[16] = '<a href="index.php?title=foo&amp;id=bar">Link</a>'; $this->assertTokenization(
$expect[16] = array( '<a href="index.php?title=foo&amp;id=bar">Link</a>',
new HTMLPurifier_Token_Start('a',array('href' => 'index.php?title=foo&id=bar')) array(
,new HTMLPurifier_Token_Text('Link') new HTMLPurifier_Token_Start('a',array('href' => 'index.php?title=foo&id=bar')),
,new HTMLPurifier_Token_End('a') new HTMLPurifier_Token_Text('Link'),
new HTMLPurifier_Token_End('a'),
)
); );
}
// test that UTF-8 is preserved function test_tokenizeHTML_preserveUTF8() {
$char_hearts = $this->_entity_lookup->table['hearts']; $this->assertTokenization(
$input[17] = $char_hearts; "\xCE\xB8",
$expect[17] = array( new HTMLPurifier_Token_Text($char_hearts) ); array( new HTMLPurifier_Token_Text("\xCE\xB8") )
);
}
// test weird characters in attributes function test_tokenizeHTML_specialEntityInAttribute() {
$input[18] = '<br test="x &lt; 6" />'; $this->assertTokenization(
$expect[18] = array( new HTMLPurifier_Token_Empty('br', array('test' => 'x < 6')) ); '<br test="x &lt; 6" />',
array( new HTMLPurifier_Token_Empty('br', array('test' => 'x < 6')) )
);
}
// test emoticon protection function test_tokenizeHTML_emoticonProtection() {
$input[19] = '<b>Whoa! <3 That\'s not good >.></b>'; $this->config->set('Core', 'AggressivelyFixLt', true);
$expect[19] = array( $this->assertTokenization(
'<b>Whoa! <3 That\'s not good >.></b>',
array(
new HTMLPurifier_Token_Start('b'), new HTMLPurifier_Token_Start('b'),
new HTMLPurifier_Token_Text('Whoa! '), new HTMLPurifier_Token_Text('Whoa! '),
new HTMLPurifier_Token_Text('<3 That\'s not good >'), new HTMLPurifier_Token_Text('<3 That\'s not good >'),
new HTMLPurifier_Token_Text('.>'), new HTMLPurifier_Token_Text('.>'),
new HTMLPurifier_Token_End('b'), new HTMLPurifier_Token_End('b')
); ),
$dom_expect[19] = array( array(
// text is absorbed together
'DOMLex' => array(
new HTMLPurifier_Token_Start('b'), new HTMLPurifier_Token_Start('b'),
new HTMLPurifier_Token_Text('Whoa! <3 That\'s not good >.>'), new HTMLPurifier_Token_Text('Whoa! <3 That\'s not good >.>'),
new HTMLPurifier_Token_End('b'), new HTMLPurifier_Token_End('b'),
),
'PEARSax3' => false, // totally mangled
)
); );
$sax_expect[19] = false; // SAX drops the < character }
$config[19] = HTMLPurifier_Config::create(array('Core.AggressivelyFixLt' => true));
// test comment parsing with funky characters inside function test_tokenizeHTML_commentWithFunkyChars() {
$input[20] = '<!-- This >< comment --><br />'; $this->assertTokenization(
$expect[20] = array( '<!-- This >< comment --><br />',
array(
new HTMLPurifier_Token_Comment(' This >< comment '), new HTMLPurifier_Token_Comment(' This >< comment '),
new HTMLPurifier_Token_Empty('br') new HTMLPurifier_Token_Empty('br'),
),
array(
'PEARSax3' => false,
)
); );
$sax_expect[20] = false; }
$config[20] = HTMLPurifier_Config::create(array('Core.AggressivelyFixLt' => true));
// test comment parsing of missing end function test_tokenizeHTML_unterminatedComment() {
$input[21] = '<!-- This >< comment'; $this->assertTokenization(
$expect[21] = array( '<!-- This >< comment',
new HTMLPurifier_Token_Comment(' This >< comment') array( new HTMLPurifier_Token_Comment(' This >< comment') ),
array(
'DOMLex' => false,
'PEARSax3' => false
)
); );
$sax_expect[21] = false; }
$dom_expect[21] = false;
$config[21] = HTMLPurifier_Config::create(array('Core.AggressivelyFixLt' => true));
// test CDATA tags function test_tokenizeHTML_scriptCDATAContents() {
$input[22] = '<script>alert("<foo>");</script>'; $this->config->set('HTML', 'Trusted', true);
$expect[22] = array( $this->assertTokenization(
new HTMLPurifier_Token_Start('script') '<script>alert("<foo>");</script>',
,new HTMLPurifier_Token_Text('alert("<foo>");') array(
,new HTMLPurifier_Token_End('script') new HTMLPurifier_Token_Start('script'),
new HTMLPurifier_Token_Text('alert("<foo>");'),
new HTMLPurifier_Token_End('script'),
),
array(
'PEARSax3' => false,
)
); );
$config[22] = HTMLPurifier_Config::create(array('HTML.Trusted' => true)); }
$sax_expect[22] = false;
// test escaping function test_tokenizeHTML_entitiesInComment() {
$input[23] = '<!-- This comment < &lt; & -->'; $this->config->set('Core', 'AggressivelyFixLt', true);
$expect[23] = array( $this->assertTokenization(
new HTMLPurifier_Token_Comment(' This comment < &lt; & ') ); '<!-- This comment < &lt; & -->',
$sax_expect[23] = false; $config[23] = array( new HTMLPurifier_Token_Comment(' This comment < &lt; & ') ),
HTMLPurifier_Config::create(array('Core.AggressivelyFixLt' => array(
true)); 'PEARSax3' => false
)
);
}
// more DirectLex edge-cases function test_tokenizeHTML_attributeWithSpecialCharacters() {
$input[24] = '<a href="><>">'; $this->assertTokenization(
$expect[24] = array( '<a href="><>">',
array( new HTMLPurifier_Token_Empty('a', array('href' => '><>')) ),
array(
'DirectLex' => array(
new HTMLPurifier_Token_Start('a', array('href' => '')), new HTMLPurifier_Token_Start('a', array('href' => '')),
new HTMLPurifier_Token_Text('<">') new HTMLPurifier_Token_Text('<">'),
),
'PEARSax3' => false,
)
); );
$sax_expect[24] = false; }
$dom_expect[24] = array(
new HTMLPurifier_Token_Empty('a', array('href' => '><>')) function test_tokenizeHTML_emptyTagWithSlashInAttribute() {
$this->assertTokenization(
'<param name="src" value="http://example.com/video.wmv" />',
array( new HTMLPurifier_Token_Empty('param', array('name' => 'src', 'value' => 'http://example.com/video.wmv')) )
); );
}
// empty tag with attributes /*
$input[25] = '<param name="src" value="http://example.com/video.wmv" />';
$expect[25] = array( function test_tokenizeHTML_() {
new HTMLPurifier_Token_Empty('param', array('name' => 'src', 'value' => 'http://example.com/video.wmv')) $this->assertTokenization(
,
array(
)
); );
$default_config = HTMLPurifier_Config::createDefault();
$default_context = new HTMLPurifier_Context();
foreach($input as $i => $discard) {
if (!isset($config[$i])) $config[$i] = $default_config;
$result = $this->DirectLex->tokenizeHTML($input[$i], $config[$i], $default_context);
$this->assertIdentical($expect[$i], $result, 'DirectLexTest '.$i.': %s');
paintIf($result, $expect[$i] != $result);
if ($this->_has_pear) {
// assert unless I say otherwise
$sax_result = $this->PEARSax3->tokenizeHTML($input[$i], $config[$i], $default_context);
if (!isset($sax_expect[$i])) {
// by default, assert with normal result
$this->assertIdentical($expect[$i], $sax_result, 'PEARSax3Test '.$i.': %s');
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
$this->assertIdentical($sax_expect[$i], $sax_result, 'PEARSax3Test (custom) '.$i.': %s');
paintIf($sax_result, $sax_expect[$i] != $sax_result);
}
}
if ($this->_has_dom) {
$dom_result = $this->DOMLex->tokenizeHTML($input[$i], $config[$i], $default_context);
// same structure as SAX
if (!isset($dom_expect[$i])) {
$this->assertIdentical($expect[$i], $dom_result, 'DOMLexTest '.$i.': %s');
paintIf($dom_result, $expect[$i] != $dom_result);
} elseif ($dom_expect[$i] === false) {
// paintIf($dom_result, $i == NUMBER);
} else {
$this->assertIdentical($dom_expect[$i], $dom_result, 'DOMLexTest (custom) '.$i.': %s');
paintIf($dom_result, $dom_expect[$i] != $dom_result);
}
}
}
} }
*/
} }