From 9f2f6c3166f79cd74e701e4f902ecf5d21b42a51 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Wed, 26 Mar 2008 04:31:04 +0000 Subject: [PATCH] [3.1.0] Fix bug with addAttribute when called multiple times on the same element git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1634 48356398-32a2-884e-a903-53898d9a118a --- NEWS | 2 ++ TODO | 1 - library/HTMLPurifier/HTMLDefinition.php | 8 +++++++- tests/HTMLPurifier/HTMLDefinitionTest.php | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index bd07f290..8333734c 100644 --- a/NEWS +++ b/NEWS @@ -46,6 +46,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier - Fix bug in comment parsing for DirectLex - Flush output now displayed when in command line mode for unit tester - Fix bug with rgb(0, 1, 2) color syntax with spaces inside shorthand syntax +- HTMLPurifier_HTMLDefinition->addAttribute can now be called multiple times + on the same element without emitting errors. . Plugins now get their own changelogs according to project conventions. . Convert tokens to use instanceof, reducing memory footprint and improving comparison speed. diff --git a/TODO b/TODO index cfea5371..6aa5beb2 100644 --- a/TODO +++ b/TODO @@ -46,7 +46,6 @@ NICE FEATURES BUGS - Style attribute height/width limiting for images - - addAttribute multiple calls with HTMLDefinition - Support for hard-coded paths/dirname(__FILE__) in include stub and autoload - Easy way to blacklist elements and attributes - Investigate iconv error emitting diff --git a/library/HTMLPurifier/HTMLDefinition.php b/library/HTMLPurifier/HTMLDefinition.php index f8cf093f..360bc4be 100644 --- a/library/HTMLPurifier/HTMLDefinition.php +++ b/library/HTMLPurifier/HTMLDefinition.php @@ -87,6 +87,8 @@ class HTMLPurifier_HTMLDefinition extends HTMLPurifier_Definition /** * Adds a custom attribute to a pre-existing element + * @note This is strictly convenience, and does not have a corresponding + * method in HTMLPurifier_HTMLModule * @param $element_name String element name to add attribute to * @param $attr_name String name of attribute * @param $def Attribute definition, can be string or object, see @@ -94,7 +96,11 @@ class HTMLPurifier_HTMLDefinition extends HTMLPurifier_Definition */ public function addAttribute($element_name, $attr_name, $def) { $module =& $this->getAnonymousModule(); - $element =& $module->addBlankElement($element_name); + if (!isset($module->info[$element_name])) { + $element =& $module->addBlankElement($element_name); + } else { + $element =& $module->info[$element_name]; + } $element->attr[$attr_name] = $def; } diff --git a/tests/HTMLPurifier/HTMLDefinitionTest.php b/tests/HTMLPurifier/HTMLDefinitionTest.php index b2a03ef9..64e9e1a3 100644 --- a/tests/HTMLPurifier/HTMLDefinitionTest.php +++ b/tests/HTMLPurifier/HTMLDefinitionTest.php @@ -85,6 +85,22 @@ a[href|title] } + function test_addAttribute_multiple() { + + $config = HTMLPurifier_Config::create(array( + 'HTML.DefinitionID' => 'HTMLPurifier_HTMLDefinitionTest->test_addAttribute_multiple' + )); + $def =& $config->getHTMLDefinition(true); + $def->addAttribute('span', 'custom', 'Enum#attribute'); + $def->addAttribute('span', 'foo', 'Text'); + + $purifier = new HTMLPurifier($config); + $input = 'Custom!'; + $output = $purifier->purify($input); + $this->assertIdentical($input, $output); + + } + function test_addElement() { $config = HTMLPurifier_Config::create(array(