lex = new HTMLPurifier_Lexer_DirectLex(); $this->gen = new HTMLPurifier_Generator(); parent::UnitTestCase(); } function assertSeries($inputs, $expect, $config, $context = array()) { foreach ($inputs as $i => $input) { if (!isset($context[$i])) { $context[$i] = null; } if (!isset($config[$i])) { $config[$i] = HTMLPurifier_Config::createDefault(); } $tokens = $this->lex->tokenizeHTML($input, $config[$i]); $result = $this->def->validateChildren($tokens, $config[$i], $context[$i]); if (is_bool($expect[$i])) { $this->assertIdentical($expect[$i], $result, "Test $i: %s"); } else { $result_html = $this->gen->generateFromTokens($result, $config[$i]); $this->assertIdentical($expect[$i], $result_html, "Test $i: %s"); paintIf($result_html, $result_html != $expect[$i]); } } } function test_custom() { $this->def = new HTMLPurifier_ChildDef_Custom( '(a, b?, c*, d+, (a, b)*)'); $inputs[0] = ''; $expect[0] = false; $inputs[1] = ''; $expect[1] = true; $inputs[2] = 'Dobfoofoo'; $expect[2] = true; $inputs[3] = ''; $expect[3] = false; } function test_table() { // currently inactive, awaiting augmentation // the table definition $this->def = new HTMLPurifier_ChildDef_Table(); $inputs = $expect = $config = array(); $inputs[0] = ''; $expect[0] = false; // we're using empty tags to compact the tests: under real circumstances // there would be contents in them $inputs[1] = ''; $expect[1] = true; $inputs[2] = ''. 'asdf'; $expect[2] = true; $inputs[3] = ''; $expect[3] = true; // mixed up order $inputs[4] = '1'; $expect[4] = '1'; // duplicates of singles // - first caption serves // - trailing tfoots/theads get turned into tbodys $inputs[5] = '11'; $expect[5] = '11'; // errant text dropped (until bubbling is implemented) $inputs[6] = 'foo'; $expect[6] = false; // whitespace sticks to the previous element, last whitespace is // stationary $inputs[7] = "\n \n \n "; $expect[7] = true; $inputs[8] = "\n\t\n\t\t\n\t\t\t"; $expect[8] = "\n\t\t\n\t\n\t\t\t"; $this->assertSeries($inputs, $expect, $config); } function test_parsing() { $def = new HTMLPurifier_ChildDef_Required('foobar | bang |gizmo'); $this->assertEqual($def->elements, array( 'foobar' => true ,'bang' => true ,'gizmo' => true )); $def = new HTMLPurifier_ChildDef_Required(array('href', 'src')); $this->assertEqual($def->elements, array( 'href' => true ,'src' => true )); } function test_required_pcdata_forbidden() { $this->def = new HTMLPurifier_ChildDef_Required('dt | dd'); $inputs = $expect = $config = array(); $inputs[0] = ''; $expect[0] = false; $inputs[1] = '
Term
Text in an illegal location'. '
Definition
Illegal tag'; $expect[1] = '
Term
Definition
'; $inputs[2] = 'How do you do!'; $expect[2] = false; // whitespace shouldn't trigger it $inputs[3] = "\n
Definition
"; $expect[3] = true; $inputs[4] ='
Definition
'; $expect[4] = '
Definition
'; $inputs[5] = "\t "; $expect[5] = false; $this->assertSeries($inputs, $expect, $config); } function test_required_pcdata_allowed() { $this->def = new HTMLPurifier_ChildDef_Required('#PCDATA | b'); $inputs = $expect = $config = array(); $inputs[0] = 'Bold text'; $expect[0] = 'Bold text'; // with child escaping on $inputs[1] = 'Bold text'; $expect[1] = 'Bold text<img />'; $config[1] = HTMLPurifier_Config::createDefault(); $config[1]->set('Core', 'EscapeInvalidChildren', true); $this->assertSeries($inputs, $expect, $config); } function test_optional() { $this->def = new HTMLPurifier_ChildDef_Optional('b | i'); $inputs = $expect = $config = array(); $inputs[0] = 'Bold text'; $expect[0] = 'Bold text'; $inputs[1] = 'Not allowed text'; $expect[1] = ''; $this->assertSeries($inputs, $expect, $config); } function test_chameleon() { $this->def = new HTMLPurifier_ChildDef_Chameleon( 'b | i', // allowed only when in inline context 'b | i | div' // allowed only when in block context ); $inputs = $expect = $config = array(); $context = array(); $inputs[0] = 'Allowed.'; $expect[0] = true; $context[0] = 'inline'; $inputs[1] = '
Not allowed.
'; $expect[1] = ''; $context[1] = 'inline'; $inputs[2] = '
Allowed.
'; $expect[2] = true; $context[2] = 'block'; $this->assertSeries($inputs, $expect, $config, $context); } } ?>