Lexer = new HTMLPurifier_Lexer(); $this->DirectLex = new HTMLPurifier_Lexer_DirectLex(); // E_STRICT = 2048, int used for PHP4 compat if ( $GLOBALS['HTMLPurifierTest']['PEAR'] && ((error_reporting() & 2048) != 2048) ) { $this->_has_pear = true; require_once 'HTMLPurifier/Lexer/PEARSax3.php'; $this->PEARSax3 = new HTMLPurifier_Lexer_PEARSax3(); } $this->_has_dom = version_compare(PHP_VERSION, '5', '>='); 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() { $config = HTMLPurifier_Config::create(array('Core.MaintainLineNumbers' => true)); $lexer = HTMLPurifier_Lexer::create($config); $this->assertIsA($lexer, 'HTMLPurifier_Lexer_DirectLex'); } function assertExtractBody($text, $extract = true) { $result = $this->Lexer->extractBody($text); if ($extract === true) $extract = $text; $this->assertIdentical($extract, $result); } function test_parseData() { $HP =& $this->Lexer; $this->assertIdentical('asdf', $HP->parseData('asdf')); $this->assertIdentical('&', $HP->parseData('&')); $this->assertIdentical('"', $HP->parseData('"')); $this->assertIdentical("'", $HP->parseData(''')); $this->assertIdentical("'", $HP->parseData(''')); $this->assertIdentical('&&&', $HP->parseData('&&&')); $this->assertIdentical('&&', $HP->parseData('&&')); // [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('-', $HP->parseData('-')); } function test_extractBody() { $this->assertExtractBody('Bold'); $this->assertExtractBody('Bold', 'Bold'); $this->assertExtractBody('Bold', 'Bold'); $this->assertExtractBody( ' xyz
', '
'); $this->assertExtractBody('Bold', 'Bold'); $this->assertExtractBody('asdf'); // not closed, don't accept } function test_tokenizeHTML() { $input = array(); $expect = array(); $sax_expect = array(); $config = array(); $input[0] = ''; $expect[0] = array(); $input[1] = 'This is regular text.'; $expect[1] = array( new HTMLPurifier_Token_Text('This is regular text.') ); $input[2] = 'This is bold text'; $expect[2] = array( 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') ); $input[3] = '
Totally rad dude. asdf
'; $expect[3] = array( 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') ); // [XML-INVALID] $input[4] = ''; $expect[4] = array( 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') ); // 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') ); $input[5] = 'Link to foobar'; $expect[5] = array( 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') ); $input[6] = '
'; $expect[6] = array( new HTMLPurifier_Token_Empty('br') ); // [SGML-INVALID] [RECOVERABLE] $input[7] = ' '; $expect[7] = 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] $input[8] = ''')) ); // DOM parses it into an empty tag $dom_expect[8] = array( new HTMLPurifier_Token_Empty('a', array('href'=>'')) ); $input[9] = '<b>'; $expect[9] = array( 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] $input[10] = ''; // We barf on this, aim for no attributes $expect[10] = array( new HTMLPurifier_Token_Start('a', array('"' => '')) ); // DOM correctly has no attributes, but also closes the tag $dom_expect[10] = array( new HTMLPurifier_Token_Empty('a') ); // SAX barfs on this $sax_expect[10] = array( new HTMLPurifier_Token_Start('a', array('"' => '')) ); // [INVALID] [RECOVERABLE] $input[11] = '"'; $expect[11] = array( new HTMLPurifier_Token_Text('"') ); // compare with this valid one: $input[12] = '"'; $expect[12] = array( new HTMLPurifier_Token_Text('"') ); $sax_expect[12] = false; // choked! // CDATA sections! $input[13] = 'can't get me!]]>'; $expect[13] = array( new HTMLPurifier_Token_Text( 'You can't get me!' // raw ) ); $sax_expect[13] = array( // SAX has a seperate call for each entity new HTMLPurifier_Token_Text('You '), new HTMLPurifier_Token_Text('<'), new HTMLPurifier_Token_Text('b'), new HTMLPurifier_Token_Text('>'), new HTMLPurifier_Token_Text('can'), new HTMLPurifier_Token_Text('&'), new HTMLPurifier_Token_Text('#39;t'), new HTMLPurifier_Token_Text('<'), new HTMLPurifier_Token_Text('/b'), new HTMLPurifier_Token_Text('>'), new HTMLPurifier_Token_Text(' get me!') ); $char_theta = $this->_entity_lookup->table['theta']; $char_rarr = $this->_entity_lookup->table['rarr']; // test entity replacement $input[14] = 'θ'; $expect[14] = array( new HTMLPurifier_Token_Text($char_theta) ); // test that entities aren't replaced in CDATA sections $input[15] = 'θ '; $expect[15] = array( new HTMLPurifier_Token_Text($char_theta . ' →') ); $sax_expect[15] = array( new HTMLPurifier_Token_Text($char_theta . ' '), new HTMLPurifier_Token_Text('&'), new HTMLPurifier_Token_Text('rarr;') ); // test entity resolution in attributes $input[16] = 'Link'; $expect[16] = array( new HTMLPurifier_Token_Start('a',array('href' => 'index.php?title=foo&id=bar')) ,new HTMLPurifier_Token_Text('Link') ,new HTMLPurifier_Token_End('a') ); // test that UTF-8 is preserved $char_hearts = $this->_entity_lookup->table['hearts']; $input[17] = $char_hearts; $expect[17] = array( new HTMLPurifier_Token_Text($char_hearts) ); // test weird characters in attributes $input[18] = '
'; $expect[18] = array( new HTMLPurifier_Token_Empty('br', array('test' => 'x < 6')) ); // test emoticon protection $input[19] = 'Whoa! >.< That\'s not good >.>'; $expect[19] = array( new HTMLPurifier_Token_Start('b'), new HTMLPurifier_Token_Text('Whoa! >.'), new HTMLPurifier_Token_Text('< That\'s not good >'), new HTMLPurifier_Token_Text('.>'), new HTMLPurifier_Token_End('b'), ); $sax_expect[19] = false; // SAX drops the < character $dom_expect[19] = false; // DOM drops the entire pseudo-tag // test comment parsing with funky characters inside $input[20] = '
'; $expect[20] = array( new HTMLPurifier_Token_Comment(' This >< comment '), new HTMLPurifier_Token_Empty('br') ); $sax_expect[20] = false; // test comment parsing of missing end $input[21] = '