0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 15:28:40 +00:00

Add ChildDef_Optional, which piggy-backs off of ChildDef_Required. Some column 80 formatting fixes.

git-svn-id: http://htmlpurifier.org/svnroot/html_purifier/trunk@49 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-05-30 22:44:14 +00:00
parent 3f123d6f53
commit f5486bbbae
2 changed files with 44 additions and 7 deletions

View File

@ -127,7 +127,7 @@ class PureHTMLDefinition
$this->info['dl'] =
new HTMLDTD_Element(
new HTMLDTD_ChildDef_Optional('dt|dd')
new HTMLDTD_ChildDef_Required('dt|dd')
);
$this->info['address'] =
new HTMLDTD_Element(
@ -161,7 +161,9 @@ class PureHTMLDefinition
if (is_subclass_of($token, 'MF_Tag')) {
if (!isset($this->info[$token->name])) {
// invalid tag, generate HTML and insert in
$token = new MF_Text($this->generator->generateFromToken($token));
$token = new MF_Text(
$this->generator->generateFromToken($token)
);
}
} elseif (is_a($token, 'MF_Comment')) {
// strip comments
@ -257,7 +259,9 @@ class PureHTMLDefinition
// make sure that we have something open
if (empty($current_nesting)) {
$result[] = new MF_Text($this->generator->generateFromToken($token));
$result[] = new MF_Text(
$this->generator->generateFromToken($token)
);
continue;
}
@ -290,7 +294,9 @@ class PureHTMLDefinition
// we still didn't find the tag, so translate to text
if ($skipped_tags === false) {
$result[] = new MF_Text($this->generator->generateFromToken($token));
$result[] = new MF_Text(
$this->generator->generateFromToken($token)
);
continue;
}
@ -411,7 +417,9 @@ class HTMLDTD_ChildDef_Required extends HTMLDTD_ChildDef_Simple
if (!isset($this->elements[$token->name])) {
$is_deleting = true;
if ($pcdata_allowed) {
$result[] = new MF_Text($this->gen->generateFromToken($token));
$result[] = new MF_Text(
$this->gen->generateFromToken($token)
);
}
continue;
}
@ -430,12 +438,19 @@ class HTMLDTD_ChildDef_Required extends HTMLDTD_ChildDef_Simple
return $result;
}
}
class HTMLDTD_ChildDef_Optional extends HTMLDTD_ChildDef_Simple
// only altered behavior is that it returns an empty array
// instead of a false (to delete the node)
class HTMLDTD_ChildDef_Optional extends HTMLDTD_ChildDef_Required
{
function validateChildren($tokens_of_children) {
$result = parent::validateChildren($tokens_of_children);
if ($result === false) return array();
return $result;
}
}
// placeholder
class HTMLDTD_ChildDef_Empty extends HTMLDTD_ChildDef
{
function HTMLDTD_ChildDef_Empty() {}

View File

@ -303,6 +303,28 @@ class Test_HTMLDTD_ChildDef extends UnitTestCase
$this->assertEqual($expect, $def->validateChildren($input));
}
function test_optional() {
$def = new HTMLDTD_ChildDef_Optional('b | i');
$input = array(
new MF_StartTag('b')
,new MF_Text('Bold text')
,new MF_EndTag('b')
,new MF_EmptyTag('img') // illegal tag
);
$expect = array(
new MF_StartTag('b')
,new MF_Text('Bold text')
,new MF_EndTag('b')
);
$this->assertEqual($expect, $def->validateChildren($input));
$input = array(
new MF_Text('Not allowed text')
);
$expect = array();
$this->assertEqual($expect, $def->validateChildren($input));
}
}
?>