diff --git a/HTML_Generator.php b/HTML_Generator.php new file mode 100644 index 00000000..e979db3f --- /dev/null +++ b/HTML_Generator.php @@ -0,0 +1,37 @@ +generateAttributes($token->attributes); + return '<' . $token->name . ' ' . $attr . '>'; + + } elseif (is_a($token, 'MF_EndTag')) { + return 'name . '>'; + + } elseif (is_a($token, 'MF_EmptyTag')) { + $attr = $this->generateAttributes($token->attributes); + return '<' . $token->name . ' ' . $attr . ' />'; + + } elseif (is_a($token, 'MF_Text')) { + return htmlentities($token->data, ENT_COMPAT, 'UTF-8'); + + } else { + return ''; + + } + } + + function generateAttributes($assoc_array_of_attributes) { + $html = ''; + foreach ($assoc_array_of_attributes as $key => $value) { + $html .= $key.'="'.htmlentities($value, ENT_COMPAT, 'UTF-8').'" '; + } + return rtrim($html); + } + +} + +?> \ No newline at end of file diff --git a/tester.php b/tester.php index f4aa2848..552c15e2 100644 --- a/tester.php +++ b/tester.php @@ -8,6 +8,7 @@ require_once 'HTML_Purifier.php'; require_once 'HTML_Lexer.php'; require_once 'MarkupFragment.php'; require_once 'PureHTMLDefinition.php'; +require_once 'HTML_Generator.php'; $test = new GroupTest('HTML_Purifier'); @@ -16,6 +17,7 @@ $test->addTestFile('HTML_Purifier.php'); $test->addTestFile('HTML_Lexer.php'); //$test->addTestFile('MarkupFragment.php'); $test->addTestFile('PureHTMLDefinition.php'); +$test->addTestFile('HTML_Generator.php'); chdir('../'); $test->run(new HtmlReporter()); diff --git a/tests/HTML_Generator.php b/tests/HTML_Generator.php new file mode 100644 index 00000000..e2ad7728 --- /dev/null +++ b/tests/HTML_Generator.php @@ -0,0 +1,65 @@ +UnitTestCase(); + $this->gen = new HTML_Generator(); + } + + function test_generateFromToken() { + + $inputs = array(); + $expect = array(); + + $inputs[0] = new MF_Text('Foobar.<>'); + $expect[0] = 'Foobar.<>'; + + $inputs[1] = new MF_StartTag('a', array('href' => 'dyn?a=foo&b=bar')); + $expect[1] = ''; + + $inputs[2] = new MF_EndTag('b'); + $expect[2] = ''; + + $inputs[3] = new MF_EmptyTag('br', array('style' => 'font-family:"Courier New";')); + $expect[3] = '
'; + + foreach ($inputs as $i => $input) { + $result = $this->gen->generateFromToken($input); + $this->assertEqual($result, $expect[$i]); + paintIf($result, $result != $expect[$i]); + } + + } + + function test_generateAttributes() { + + $inputs = array(); + $expect = array(); + + $inputs[0] = array(); + $expect[0] = ''; + + $inputs[1] = array('href' => 'dyn?a=foo&b=bar'); + $expect[1] = 'href="dyn?a=foo&b=bar"'; + + $inputs[2] = array('style' => 'font-family:"Courier New";'); + $expect[2] = 'style="font-family:"Courier New";"'; + + $inputs[3] = array('src' => 'picture.jpg', 'alt' => 'Short & interesting'); + $expect[3] = 'src="picture.jpg" alt="Short & interesting"'; + + foreach ($inputs as $i => $input) { + $result = $this->gen->generateAttributes($input); + $this->assertEqual($result, $expect[$i]); + paintIf($result, $result != $expect[$i]); + } + + } + +} + +?> \ No newline at end of file