lex = new HTMLPurifier_Lexer(); $this->gen = new HTMLPurifier_Generator(); parent::UnitTestCase(); } function assertSeries($inputs, $expect, $def) { foreach ($inputs as $i => $input) { $tokens = $this->lex->tokenizeHTML($input); $result = $def->validateChildren($tokens); if (is_bool($expect[$i])) { $this->assertIdentical($expect[$i], $result); } else { $result_html = $this->gen->generateFromTokens($result); $this->assertEqual($expect[$i], $result_html); paintIf($result_html, $result_html != $expect[$i]); } } } function test_complex() { // the table definition $def = new HTMLPurifier_ChildDef( '(caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))'); $inputs[0] = ''; $expect[0] = false; // we really don't care what's inside, because if it turns out // this tr is illegal, we'll end up re-evaluating the parent node // anyway. $inputs[1] = ''; $expect[1] = true; $inputs[2] = '' . ''; $expect[2] = true; $inputs[3] = ''; $expect[3] = true; $this->assertSeries($inputs, $expect, $def); } function test_simple() { // simple is actually an abstract class // but we're unit testing some of the conv. functions it gives $def = new HTMLPurifier_ChildDef_Simple('foobar | bang |gizmo'); $this->assertEqual($def->elements, array( 'foobar' => true ,'bang' => true ,'gizmo' => true )); $def = new HTMLPurifier_ChildDef_Simple(array('href', 'src')); $this->assertEqual($def->elements, array( 'href' => true ,'src' => true )); } function test_required_pcdata_forbidden() { $def = new HTMLPurifier_ChildDef_Required('dt | dd'); $inputs[0] = array(); $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, $def); } function test_required_pcdata_allowed() { $def = new HTMLPurifier_ChildDef_Required('#PCDATA | b'); $inputs[0] = 'Bold text'; $expect[0] = 'Bold text<img />'; $this->assertSeries($inputs, $expect, $def); } function test_optional() { $def = new HTMLPurifier_ChildDef_Optional('b | i'); $inputs[0] = 'Bold text'; $expect[0] = 'Bold text'; $inputs[1] = 'Not allowed text'; $expect[1] = ''; $this->assertSeries($inputs, $expect, $def); } } ?>