0
0
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:
Edward Z. Yang 2006-04-16 03:27:40 +00:00
parent c4b23cc775
commit 7c6775d552
3 changed files with 104 additions and 0 deletions

37
HTML_Generator.php Normal file
View 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);
}
}
?>

View File

@ -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
View 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.&lt;&gt;';
$inputs[1] = new MF_StartTag('a', array('href' => 'dyn?a=foo&b=bar'));
$expect[1] = '<a href="dyn?a=foo&amp;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:&quot;Courier New&quot;;" />';
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&amp;b=bar"';
$inputs[2] = array('style' => 'font-family:"Courier New";');
$expect[2] = 'style="font-family:&quot;Courier New&quot;;"';
$inputs[3] = array('src' => 'picture.jpg', 'alt' => 'Short & interesting');
$expect[3] = 'src="picture.jpg" alt="Short &amp; interesting"';
foreach ($inputs as $i => $input) {
$result = $this->gen->generateAttributes($input);
$this->assertEqual($result, $expect[$i]);
paintIf($result, $result != $expect[$i]);
}
}
}
?>