From 7f39e1e2c339d0c7872eb4c89ac2e56006cbf661 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Wed, 9 May 2007 22:01:07 +0000 Subject: [PATCH] [1.7.0] Convert Image, Legacy and List to use new format. - Make attribute array parameter optional - Optimize contents parsing for keywords git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1041 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier/HTMLModule.php | 10 +++--- library/HTMLPurifier/HTMLModule/Image.php | 18 +++++----- library/HTMLPurifier/HTMLModule/Legacy.php | 24 ++++++------- library/HTMLPurifier/HTMLModule/List.php | 40 ++++++++++++---------- tests/HTMLPurifier/HTMLModuleTest.php | 4 +++ 5 files changed, 52 insertions(+), 44 deletions(-) diff --git a/library/HTMLPurifier/HTMLModule.php b/library/HTMLPurifier/HTMLModule.php index c38610c6..a4d4af73 100644 --- a/library/HTMLPurifier/HTMLModule.php +++ b/library/HTMLPurifier/HTMLModule.php @@ -139,7 +139,7 @@ class HTMLPurifier_HTMLModule * @note See ElementDef for in-depth descriptions of these parameters. * @protected */ - function addElement($element, $safe, $type, $contents, $attr_includes, $attr) { + function addElement($element, $safe, $type, $contents, $attr_includes, $attr = array()) { $this->elements[] = $element; // parse content_model list($content_model_type, $content_model) = $this->parseContents($contents); @@ -175,12 +175,12 @@ class HTMLPurifier_HTMLModule function parseContents($contents) { switch ($contents) { // check for shorthand content model forms + case 'Empty': + return array('empty', ''); case 'Inline': - $contents = 'Optional: Inline | #PCDATA'; - break; + return array('optional', 'Inline | #PCDATA'); case 'Flow': - $contents = 'Optional: Flow | #PCDATA'; - break; + return array('optional', 'Flow | #PCDATA'); } list($content_model_type, $content_model) = explode(':', $contents); $content_model_type = strtolower(trim($content_model_type)); diff --git a/library/HTMLPurifier/HTMLModule/Image.php b/library/HTMLPurifier/HTMLModule/Image.php index bf234b13..cebe4f41 100644 --- a/library/HTMLPurifier/HTMLModule/Image.php +++ b/library/HTMLPurifier/HTMLModule/Image.php @@ -18,16 +18,16 @@ class HTMLPurifier_HTMLModule_Image extends HTMLPurifier_HTMLModule var $content_sets = array('Inline' => 'img'); function HTMLPurifier_HTMLModule_Image() { - $this->info['img'] = new HTMLPurifier_ElementDef(); - $this->info['img']->attr = array( - 0 => array('Common'), - 'alt' => 'Text', - 'height' => 'Length', - 'longdesc' => 'URI', - 'src' => new HTMLPurifier_AttrDef_URI(true), // embedded - 'width' => 'Length' + $this->addElement( + 'img', true, 'Inline', 'Empty', 'Common', + array( + 'alt' => 'Text', + 'height' => 'Length', + 'longdesc' => 'URI', + 'src' => new HTMLPurifier_AttrDef_URI(true), // embedded + 'width' => 'Length' + ) ); - $this->info['img']->content_model_type = 'empty'; $this->info['img']->attr_transform_post[] = new HTMLPurifier_AttrTransform_ImgRequired(); } diff --git a/library/HTMLPurifier/HTMLModule/Legacy.php b/library/HTMLPurifier/HTMLModule/Legacy.php index a0613a2f..357a4f40 100644 --- a/library/HTMLPurifier/HTMLModule/Legacy.php +++ b/library/HTMLPurifier/HTMLModule/Legacy.php @@ -22,22 +22,22 @@ class HTMLPurifier_HTMLModule_Legacy extends HTMLPurifier_HTMLModule // incomplete var $name = 'Legacy'; - var $elements = array('u', 's', 'strike'); - var $non_standalone_elements = array('li', 'ol', 'address', 'blockquote'); function HTMLPurifier_HTMLModule_Legacy() { - // setup new elements - foreach ($this->elements as $name) { - $this->info[$name] = new HTMLPurifier_ElementDef(); - // for u, s, strike, as more elements get added, add - // conditionals as necessary - $this->info[$name]->content_model = 'Inline | #PCDATA'; - $this->info[$name]->content_model_type = 'optional'; - $this->info[$name]->attr[0] = array('Common'); - } + $this->addElement( + 'u', true, 'Inline', 'Inline', 'Common' + ); + $this->addElement( + 's', true, 'Inline', 'Inline', 'Common' + ); + $this->addElement( + 'strike', true, 'Inline', 'Inline', 'Common' + ); // setup modifications to old elements - foreach ($this->non_standalone_elements as $name) { + // perhaps we could make some convenience functions for these... + $elements = array('li', 'ol', 'address', 'blockquote'); + foreach ($elements as $name) { $this->info[$name] = new HTMLPurifier_ElementDef(); $this->info[$name]->standalone = false; } diff --git a/library/HTMLPurifier/HTMLModule/List.php b/library/HTMLPurifier/HTMLModule/List.php index f9f2c4e2..28c943e1 100644 --- a/library/HTMLPurifier/HTMLModule/List.php +++ b/library/HTMLPurifier/HTMLModule/List.php @@ -9,7 +9,6 @@ class HTMLPurifier_HTMLModule_List extends HTMLPurifier_HTMLModule { var $name = 'List'; - var $elements = array('dl', 'dt', 'dd', 'ol', 'ul', 'li'); // According to the abstract schema, the List content set is a fully formed // one or more expr, but it invariably occurs in an optional declaration @@ -19,26 +18,31 @@ class HTMLPurifier_HTMLModule_List extends HTMLPurifier_HTMLModule // Furthermore, the actual XML Schema may disagree. Regardless, // we don't have support for such nested expressions without using // the incredibly inefficient and draconic Custom ChildDef. - var $content_sets = array('List' => 'dl | ol | ul', 'Flow' => 'List'); + + var $content_sets = array('Flow' => 'List'); function HTMLPurifier_HTMLModule_List() { - foreach ($this->elements as $element) { - $this->info[$element] = new HTMLPurifier_ElementDef(); - $this->info[$element]->attr = array(0 => array('Common')); - if ($element == 'li' || $element == 'dd') { - $this->info[$element]->content_model = '#PCDATA | Flow'; - $this->info[$element]->content_model_type = 'optional'; - } elseif ($element == 'ol' || $element == 'ul') { - $this->info[$element]->content_model = 'li'; - $this->info[$element]->content_model_type = 'required'; - } - } - $this->info['dt']->content_model = '#PCDATA | Inline'; - $this->info['dt']->content_model_type = 'optional'; - $this->info['dl']->content_model = 'dt | dd'; - $this->info['dl']->content_model_type = 'required'; - // this could be a LOT more robust + $this->addElement( + 'ol', true, 'List', 'Required: li', 'Common' + ); + $this->addElement( + 'ul', true, 'List', 'Required: li', 'Common' + ); + $this->addElement( + 'dl', true, 'List', 'Required: dt | dd', 'Common' + ); + + $this->addElement( + 'li', true, false, 'Flow', 'Common' + ); $this->info['li']->auto_close = array('li' => true); + + $this->addElement( + 'dd', true, false, 'Flow', 'Common' + ); + $this->addElement( + 'dt', true, false, 'Inline', 'Common' + ); } } diff --git a/tests/HTMLPurifier/HTMLModuleTest.php b/tests/HTMLPurifier/HTMLModuleTest.php index 58ef183e..7f584a48 100644 --- a/tests/HTMLPurifier/HTMLModuleTest.php +++ b/tests/HTMLPurifier/HTMLModuleTest.php @@ -58,6 +58,10 @@ class HTMLPurifier_HTMLModuleTest extends UnitTestCase $module->parseContents('Flow'), array('optional', 'Flow | #PCDATA') ); + $this->assertIdentical( + $module->parseContents('Empty'), + array('empty', '') + ); // normalization procedures $this->assertIdentical(