mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-12-23 00:41:52 +00:00
Add HTML_Generator class and associated unit tests.
git-svn-id: http://htmlpurifier.org/svnroot/html_purifier/trunk@32 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
c4b23cc775
commit
7c6775d552
37
HTML_Generator.php
Normal file
37
HTML_Generator.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class HTML_Generator
|
||||||
|
{
|
||||||
|
|
||||||
|
function generateFromToken($token) {
|
||||||
|
if (is_a($token, 'MF_StartTag')) {
|
||||||
|
$attr = $this->generateAttributes($token->attributes);
|
||||||
|
return '<' . $token->name . ' ' . $attr . '>';
|
||||||
|
|
||||||
|
} elseif (is_a($token, 'MF_EndTag')) {
|
||||||
|
return '</' . $token->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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -8,6 +8,7 @@ require_once 'HTML_Purifier.php';
|
|||||||
require_once 'HTML_Lexer.php';
|
require_once 'HTML_Lexer.php';
|
||||||
require_once 'MarkupFragment.php';
|
require_once 'MarkupFragment.php';
|
||||||
require_once 'PureHTMLDefinition.php';
|
require_once 'PureHTMLDefinition.php';
|
||||||
|
require_once 'HTML_Generator.php';
|
||||||
|
|
||||||
$test = new GroupTest('HTML_Purifier');
|
$test = new GroupTest('HTML_Purifier');
|
||||||
|
|
||||||
@ -16,6 +17,7 @@ $test->addTestFile('HTML_Purifier.php');
|
|||||||
$test->addTestFile('HTML_Lexer.php');
|
$test->addTestFile('HTML_Lexer.php');
|
||||||
//$test->addTestFile('MarkupFragment.php');
|
//$test->addTestFile('MarkupFragment.php');
|
||||||
$test->addTestFile('PureHTMLDefinition.php');
|
$test->addTestFile('PureHTMLDefinition.php');
|
||||||
|
$test->addTestFile('HTML_Generator.php');
|
||||||
chdir('../');
|
chdir('../');
|
||||||
|
|
||||||
$test->run(new HtmlReporter());
|
$test->run(new HtmlReporter());
|
||||||
|
65
tests/HTML_Generator.php
Normal file
65
tests/HTML_Generator.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class UnitTest_HTML_Generator extends UnitTestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
var $gen;
|
||||||
|
|
||||||
|
function UnitTest_HTML_Generator() {
|
||||||
|
$this->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] = '<a href="dyn?a=foo&b=bar">';
|
||||||
|
|
||||||
|
$inputs[2] = new MF_EndTag('b');
|
||||||
|
$expect[2] = '</b>';
|
||||||
|
|
||||||
|
$inputs[3] = new MF_EmptyTag('br', array('style' => 'font-family:"Courier New";'));
|
||||||
|
$expect[3] = '<br style="font-family:"Courier New";" />';
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
Loading…
Reference in New Issue
Block a user