0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-19 18:55:19 +00:00

Factor out definitions to a ['child'] so that we could assign the ['attr'] definitions separately.

Also, added AttrDef/EnumTest.php

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@127 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-07-30 00:54:38 +00:00
parent 4c737ab430
commit f8eaedb500
8 changed files with 119 additions and 69 deletions

View File

@ -1,10 +1,12 @@
<?php
// AttrDef = Attribute Definition
class HTMLPurifier_AttrDef
{
var $def;
function HTMLPurifier_AttrDef($def) {
$this->def = $def;
function HTMLPurifier_AttrDef() {}
function validate() {
trigger_error('Cannot call abstract function', E_USER_ERROR);
}
}

View File

@ -0,0 +1,25 @@
<?php
// Enum = Enumerated
class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef
{
var $valid_values = array();
var $case_sensitive = false; // values according to W3C spec
function HTMLPurifier_AttrDef_Enum(
$valid_values = array(), $case_sensitive = false) {
$this->valid_values = array_flip($valid_values);
$this->case_sensitive = $case_sensitive;
}
function validate($string) {
if (!$this->case_sensitive) {
$string = ctype_lower($string) ? $string : strtolower($string);
}
return isset($this->valid_values[$string]);
}
}
?>

View File

@ -92,72 +92,66 @@ class HTMLPurifier_Definition
$e_form_content = new HTMLPurifier_ChildDef_Optional(''); //unused
$e_form_button_content = new HTMLPurifier_ChildDef_Optional(''); // unused
$this->info['ins'] =
$this->info['del'] =
$this->info['blockquote'] =
$this->info['dd'] =
$this->info['li'] =
$this->info['div'] = new HTMLPurifier_ElementDef($e_Flow);
$this->info['child'] = array();
$this->info['em'] =
$this->info['strong'] =
$this->info['dfn'] =
$this->info['code'] =
$this->info['samp'] =
$this->info['kbd'] =
$this->info['var'] =
$this->info['code'] =
$this->info['samp'] =
$this->info['kbd'] =
$this->info['var'] =
$this->info['cite'] =
$this->info['abbr'] =
$this->info['acronym'] =
$this->info['q'] =
$this->info['sub'] =
$this->info['tt'] =
$this->info['sup'] =
$this->info['i'] =
$this->info['b'] =
$this->info['big'] =
$this->info['small'] =
$this->info['u'] =
$this->info['s'] =
$this->info['strike'] =
$this->info['bdo'] =
$this->info['span'] =
$this->info['dt'] =
$this->info['p'] =
$this->info['h1'] =
$this->info['h2'] =
$this->info['h3'] =
$this->info['h4'] =
$this->info['h5'] =
$this->info['h6'] = new HTMLPurifier_ElementDef($e_Inline);
$this->info['child']['ins'] =
$this->info['child']['del'] =
$this->info['child']['blockquote'] =
$this->info['child']['dd'] =
$this->info['child']['li'] =
$this->info['child']['div'] = $e_Flow;
$this->info['ol'] =
$this->info['ul'] =
new HTMLPurifier_ElementDef(
new HTMLPurifier_ChildDef_Required('li')
);
$this->info['child']['em'] =
$this->info['child']['strong'] =
$this->info['child']['dfn'] =
$this->info['child']['code'] =
$this->info['child']['samp'] =
$this->info['child']['kbd'] =
$this->info['child']['var'] =
$this->info['child']['code'] =
$this->info['child']['samp'] =
$this->info['child']['kbd'] =
$this->info['child']['var'] =
$this->info['child']['cite'] =
$this->info['child']['abbr'] =
$this->info['child']['acronym'] =
$this->info['child']['q'] =
$this->info['child']['sub'] =
$this->info['child']['tt'] =
$this->info['child']['sup'] =
$this->info['child']['i'] =
$this->info['child']['b'] =
$this->info['child']['big'] =
$this->info['child']['small'] =
$this->info['child']['u'] =
$this->info['child']['s'] =
$this->info['child']['strike'] =
$this->info['child']['bdo'] =
$this->info['child']['span'] =
$this->info['child']['dt'] =
$this->info['child']['p'] =
$this->info['child']['h1'] =
$this->info['child']['h2'] =
$this->info['child']['h3'] =
$this->info['child']['h4'] =
$this->info['child']['h5'] =
$this->info['child']['h6'] = $e_Inline;
$this->info['dl'] =
new HTMLPurifier_ElementDef(
new HTMLPurifier_ChildDef_Required('dt|dd')
);
$this->info['address'] =
new HTMLPurifier_ElementDef(
new HTMLPurifier_ChildDef_Optional("#PCDATA | p | $e_inline".
" | $e_misc_inline")
);
$this->info['child']['ol'] =
$this->info['child']['ul'] = new HTMLPurifier_ChildDef_Required('li');
$this->info['img'] =
$this->info['br'] =
$this->info['hr'] = new HTMLPurifier_ElementDef(new HTMLPurifier_ChildDef_Empty());
$this->info['child']['dl'] = new HTMLPurifier_ChildDef_Required('dt|dd');
$this->info['child']['address'] =
new HTMLPurifier_ChildDef_Optional("#PCDATA | p | $e_inline".
" | $e_misc_inline");
$this->info['pre'] = new HTMLPurifier_ElementDef($e_pre_content);
$this->info['child']['img'] =
$this->info['child']['br'] =
$this->info['child']['hr'] = new HTMLPurifier_ChildDef_Empty();
$this->info['a'] = new HTMLPurifier_ElementDef($e_a_content);
$this->info['child']['pre'] = $e_pre_content;
$this->info['child']['a'] = $e_a_content;
}

View File

@ -39,8 +39,8 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
// $j is index of end token
// have DTD child def validate children
$element_def = $this->definition->info[$tokens[$i]->name];
$result = $element_def->child_def->validateChildren($child_tokens);
$child_def = $this->definition->info['child'][$tokens[$i]->name];
$result = $child_def->validateChildren($child_tokens);
// process result
if ($result === true) {

View File

@ -23,10 +23,10 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
$result[] = $token;
continue;
}
$info = $this->definition->info[$token->name]; // assumption but valid
$info = $this->definition->info['child'][$token->name]; // assumption but valid
// test if it claims to be a start tag but is empty
if ($info->child_def->type == 'empty' &&
if ($info->type == 'empty' &&
$token->type == 'start' ) {
$result[] = new HTMLPurifier_Token_Empty($token->name,
@ -35,7 +35,7 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
}
// test if it claims to be empty but really is a start tag
if ($info->child_def->type != 'empty' &&
if ($info->type != 'empty' &&
$token->type == 'empty' ) {
$result[] = new HTMLPurifier_Token_Start($token->name,

View File

@ -19,7 +19,7 @@ class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
$result = array();
foreach($tokens as $token) {
if (!empty( $token->is_tag )) {
if (!isset($this->definition->info[$token->name])) {
if (!isset($this->definition->info['child'][$token->name])) {
// invalid tag, generate HTML and insert in
$token = new HTMLPurifier_Token_Text(
$this->generator->generateFromToken($token)

View File

@ -0,0 +1,28 @@
<?php
require_once 'HTMLPurifier/AttrDef/Enum.php';
class HTMLPurifier_AttrDef_EnumTest extends UnitTestCase
{
function testCaseInsensitive() {
$def = new HTMLPurifier_AttrDef_Enum(array('one', 'two'));
$this->assertTrue($def->validate('one'));
$this->assertTrue($def->validate('ONE'));
}
function testCaseSensitive() {
$def = new HTMLPurifier_AttrDef_Enum(array('one', 'two'), true);
$this->assertTrue($def->validate('one'));
$this->assertFalse($def->validate('ONE'));
}
}
?>

View File

@ -32,6 +32,7 @@ $test->addTestFile('HTMLPurifier/Strategy/MakeWellFormedTest.php');
$test->addTestFile('HTMLPurifier/Strategy/FixNestingTest.php');
$test->addTestFile('HTMLPurifier/Strategy/CompositeTest.php');
$test->addTestFile('HTMLPurifier/Strategy/CoreTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/EnumTest.php');
$test->run( new HtmlReporter() );