0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-23 00:41:52 +00:00

PureHTMLDefinition::makeWellFormed() is now complete, but may be missing a few unit tests. Update spec: we need transforms for the foreign elements. PureHTMLDefinition's children definitions have info but not behavior.

git-svn-id: http://htmlpurifier.org/svnroot/html_purifier/trunk@39 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-04-16 16:50:51 +00:00
parent 4c9f4067ab
commit eefb08b6f9
4 changed files with 179 additions and 65 deletions

View File

@ -17,8 +17,8 @@ class MF_Tag extends MF
class MF_TagWithAttributes extends MF_Tag class MF_TagWithAttributes extends MF_Tag
{ {
var $attributes = array(); var $attributes = array();
function MF_TagWithAttributes($type, $attributes = array()) { function MF_TagWithAttributes($name, $attributes = array()) {
$this->MF_Tag($type); $this->MF_Tag($name);
$this->attributes = $attributes; $this->attributes = $attributes;
} }
} }

View File

@ -13,57 +13,52 @@ class PureHTMLDefinition
function loadData() { function loadData() {
// emulates the structure of the DTD // emulates the structure of the DTD
// array( // entities: prefixed with e_ and _ replaces .
// array of allowed child elements, // we don't use an array because that complicates interpolation
// array of rejected child elements // strings are used instead of arrays because if you use arrays,
// indication about how many child elements are needed // you have to do some hideous manipulation with array_merge()
// )
$entity['special.extra'] = array('img'); // these are condensed, remember, with bad stuff taken out
$entity['special.basic'] = array('br','bdo','span');
$entity['special'] = array_merge($entity['special.basic'],
$entity['special.extra']);
$entity['fontstyle.extra'] = array('big','small'); // transforms: font, menu, dir, center
$entity['fontstyle.basic'] = array('tt','i','b','u','s','strike');
$entity['fontstyle'] = array_merge($entity['fontstyle.extra'],
$entity['fontstyle.basic']);
$entity['phrase.extra'] = array('sub','sup'); $e_special_extra = 'img';
$entity['phrase.basic'] = array('em','strong','dfn','code','samp','kbd', $e_special_basic = 'br | span | bdo';
'var','cite','abbr','acronym','q'); $e_special = "$e_special_basic | $e_special_extra";
$entity['phrase'] = array_merge($entity['phrase.extra'], $e_fontstyle_extra = 'big | small';
$entity['phrase.basic']); $e_fontstyle_basic = 'tt | i | b | u | s | strike';
$e_fontstyle = "$e_fontstyle_basic | $e_fontstyle_extra";
$entity['misc.inline'] = array('ins','del'); $e_phrase_extra = 'sub | sup';
$entity['misc'] = $entity['misc.inline']; $e_phrase_basic = 'em | strong | dfn | code | q | samp | kbd | var'.
' | cite | abbr | acronym';
$entity['inline'] = array_merge(array('a'), $entity['special'], $e_phrase = "$e_phrase_basic | $e_phrase_extra";
$entity['fontstyle'], $entity['phrase']); $e_inline_forms = ''; // humor the dtd
$e_misc_inline = 'ins | del';
$entity['heading'] = array('h1','h2','h3','h4','h5','h6'); $e_misc = "$e_misc_inline";
$entity['lists'] = array('ul','ol', 'dl'); $e_inline = "a | $e_special | $e_fontstyle | $e_phrase".
$entity['blocktext'] = array('pre','hr','blockquote','address'); " | $e_inline_forms";
// note the casing
$entity['block'] = array_merge(array('p','div','table'), $e_Inline = new HTMLDTD_ChildDef_Optional("#PCDATA | $e_inline".
$entity['heading'],$entity['lists'], $entity['blocktext']); " | $e_misc_inline");
$e_heading = 'h1|h2|h3|h4|h5|h6';
$entity['Inline'] = array_merge(array('#PCDATA'),$entity['special'], $e_lists = 'ul | ol | dl';
$entity['misc.inline']); $e_blocktext = 'pre | hr | blockquote | address';
$entity['Flow'] = array_merge(array('#PCDATA'), $entity['block'], $e_block = "p | $e_heading | div | $e_lists | $e_blocktext | table";
$entity['inline'], $entity['misc']); $e_Flow = new HTMLDTD_ChildDef_Optional("#PCDATA | $e_block".
$entity['a.content'] = array_merge(array('#PCDATA'), $entity['special'], " | $e_inline | $e_misc");
$entity['fontstyle'], $entity['phrase'], $entity['misc.inline']); $e_a_content = new HTMLDTD_ChildDef_Optional("#PCDATA | $e_special".
" | $e_fontstyle | $e_phrase | $e_inline_forms | $e_misc_inline");
$entity['pre.content'] = array_merge(array('#PCDATA', 'a'), $e_pre_content = new HTMLDTD_ChildDef_Optional("#PCDATA | a".
$entity['special.basic'], $entity['fontstyle.basic'], " | $e_special_basic | $e_fontstyle_basic | $e_phrase_basic".
$entity['phrase.basic'], $entity['misc.inline']); " | $e_inline_forms | $e_misc_inline");
$e_form_content = new HTMLDTD_ChildDef_Optional(''); //unused
$e_form_button_content = new HTMLDTD_ChildDef_Optional(''); // unused
$this->info['ins'] = $this->info['ins'] =
$this->info['del'] = $this->info['del'] =
$this->info['blockquote'] = $this->info['blockquote'] =
$this->info['dd'] = $this->info['dd'] =
$this->info['div'] = array($entity['Flow']); $this->info['div'] = new HTMLDTD_Element($e_Flow);
$this->info['em'] = $this->info['em'] =
$this->info['strong'] = $this->info['strong'] =
@ -99,28 +94,32 @@ class PureHTMLDefinition
$this->info['h3'] = $this->info['h3'] =
$this->info['h4'] = $this->info['h4'] =
$this->info['h5'] = $this->info['h5'] =
$this->info['h6'] = array($entity['Inline']); $this->info['h6'] = new HTMLDTD_Element($e_Inline);
$this->info['ol'] = $this->info['ol'] =
$this->info['ul'] = array(array('li'),array(),'+'); $this->info['ul'] =
// the plus requires at least one child. I don't know what the new HTMLDTD_Element(
// empty array is for though new HTMLDTD_ChildDef_Required('li')
);
$this->info['dl'] = array(array('dt','dd')); $this->info['dl'] =
new HTMLDTD_Element(
new HTMLDTD_ChildDef_Optional('dt|dd')
);
$this->info['address'] = $this->info['address'] =
array( new HTMLDTD_Element(
array_merge( new HTMLDTD_ChildDef_Optional("#PCDATA | p | $e_inline".
array('#PCDATA', 'p'), " | $e_misc_inline")
$entity['inline'], );
$entity['misc.inline']));
$this->info['img'] = $this->info['img'] =
$this->info['br'] = $this->info['br'] =
$this->info['hr'] = 'EMPTY'; $this->info['hr'] = new HTMLDTD_Element(new HTMLDTD_ChildDef_Empty());
$this->info['pre'] = array($entity['pre.content']); $this->info['pre'] = new HTMLDTD_Element($e_pre_content);
$this->info['a'] = new HTMLDTD_Element($e_a_content);
$this->info['a'] = array($entity['a.content']);
} }
function purifyTokens($tokens) { function purifyTokens($tokens) {
@ -158,9 +157,108 @@ class PureHTMLDefinition
$result = array(); $result = array();
$current_nesting = array(); $current_nesting = array();
foreach ($tokens as $token) { foreach ($tokens as $token) {
if (!is_subclass_of($token, 'MF_Tag')) $result[] = $token; if (!is_subclass_of($token, 'MF_Tag')) {
// test if it claims to be a start tag but is empty $result[] = $token;
continue;
} }
$info = $this->info[$token->name]; // assumption but valid
// test if it claims to be a start tag but is empty
if (is_a($info->child_def, 'HTMLDTD_ChildDef_Empty') &&
is_a($token, 'MF_StartTag') ) {
$result[] = new MF_EmptyTag($token->name, $token->attributes);
continue;
}
// test if it claims to be empty but really is a start tag
if (!is_a($info->child_def, 'HTMLDTD_ChildDef_Empty') &&
is_a($token, 'MF_EmptyTag') ) {
$result[] = new MF_StartTag($token->name, $token->attributes);
$result[] = new MF_EndTag($token->name);
continue;
}
// automatically insert empty tags
if (is_a($token, 'MF_EmptyTag')) {
$result[] = $token;
continue;
}
// we give start tags precedence, so automatically accept
if (is_a($token, 'MF_StartTag')) {
$result[] = $token;
$current_nesting[] = $token;
continue;
}
// sanity check
if (!is_a($token, 'MF_EndTag')) continue;
// okay, we're dealing with a closing tag
// make sure that we have something open
if (empty($current_nesting)) {
$result[] = new MF_Text($this->generator->generateFromToken($token));
continue;
}
// first, check for the simplest case: everything closes neatly
// current_nesting is modified
$current_parent = array_pop($current_nesting);
if ($current_parent->name == $token->name) {
$result[] = $token;
continue;
}
// undo the array_pop
$current_nesting[] = $current_parent;
// okay, so we're trying to close the wrong tag
// scroll back the entire nest, trying to find our tag
// feature could be to specify how far you'd like to go
$size = count($current_nesting);
// -2 because -1 is the last element, but we already checked that
$skipped_tags = false;
for ($i = $size - 2; $i >= 0; $i--) {
if ($current_nesting[$i]->name == $token->name) {
// current nesting is modified
$skipped_tags = array_splice($current_nesting, $i);
break;
}
}
// we still didn't find the tag, so translate to text
if ($skipped_tags === false) {
$result[] = new MF_Text($this->generator->generateFromToken($token));
continue;
}
// okay, we found it, close all the skipped tags
// note that skipped tags contains the element we need closed
$size = count($skipped_tags);
for ($i = $size - 1; $i >= 0; $i--) {
$result[] = new MF_EndTag($skipped_tags[$i]->name);
}
// done!
}
// we're at the end now, fix all still unclosed tags
if (!empty($current_nesting)) {
$size = count($current_nesting);
for ($i = $size - 1; $i >= 0; $i--) {
$result[] = new MF_EndTag($current_nesting[$i]->name);
}
}
return $result;
} }
function fixNesting($tokens) { function fixNesting($tokens) {
@ -181,34 +279,47 @@ class HTMLDTD_Element
var $child_def; var $child_def;
var $attr_def = array(); var $attr_def = array();
function HTMLDTD_Element($child_def, $attr_def = array()) {
$this->child_def = $child_def;
$this->attr_def = $attr_def;
}
} }
class HTMLDTD_ChildDef { class HTMLDTD_ChildDef
{
var $dtd_regex; var $dtd_regex;
function HTMLDTD_ChildDef($dtd_regex) { function HTMLDTD_ChildDef($dtd_regex) {
$this->dtd_regex = $dtd_regex; $this->dtd_regex = $dtd_regex;
} }
function validateChildren($tokens_of_children) {} function validateChildren($tokens_of_children) {}
} }
class HTMLDTD_ChildDef_Simple extends HTMLDTD_ChildDef { class HTMLDTD_ChildDef_Simple extends HTMLDTD_ChildDef
{
var $elements = array(); var $elements = array();
function HTMLDTD_ChildDef_Simple($elements) { function HTMLDTD_ChildDef_Simple($elements) {
$this->elements = $elements; $this->elements = $elements;
} }
} }
class HTMLDTD_ChildDef_Required extends HTMLDTD_ChildDef_Simple { class HTMLDTD_ChildDef_Required extends HTMLDTD_ChildDef_Simple
{
function validateChildren($tokens_of_children) { function validateChildren($tokens_of_children) {
} }
} }
class HTMLDTD_ChildDef_Optional extends HTMLDTD_ChildDef_Simple { class HTMLDTD_ChildDef_Optional extends HTMLDTD_ChildDef_Simple
{
function validateChildren($tokens_of_children) { function validateChildren($tokens_of_children) {
} }
} }
class HTMLDTD_ChildDef_Empty extends HTMLDTD_ChildDef
{
function HTMLDTD_ChildDef_Empty() {}
}
class HTMLDTD_AttrDef { class HTMLDTD_AttrDef
{
var $def; var $def;
function HTMLDTD_AttrDef($def) { function HTMLDTD_AttrDef($def) {
$this->def = $def; $this->def = $def;

View File

@ -39,6 +39,9 @@ While we're at it, we can change the Processing Instructions and Java/ASP
Instructions into data blocks, scratch comment blocks, change CharacterData Instructions into data blocks, scratch comment blocks, change CharacterData
into Data (although I don't see why we can't do that at the start). into Data (although I don't see why we can't do that at the start).
One last thing: the remove foreign elements has to do the element
transformations, from FONT to SPAN, etc.
== STAGE 3 - make well formed == == STAGE 3 - make well formed ==
Now we step through the whole thing and correct nesting issues. Most of the Now we step through the whole thing and correct nesting issues. Most of the

View File

@ -119,7 +119,7 @@ class Test_PureHTMLDefinition extends UnitTestCase
); );
foreach ($inputs as $i => $input) { foreach ($inputs as $i => $input) {
$result = $this->def->removeForeignElements($input); $result = $this->def->makeWellFormed($input);
$this->assertEqual($expect[$i], $result); $this->assertEqual($expect[$i], $result);
paintIf($result, $result != $expect[$i]); paintIf($result, $result != $expect[$i]);
} }