From 0b0a505c3020b22bf5fa8fd731ea57a50c96fb4f Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Tue, 19 Jun 2007 01:55:31 +0000 Subject: [PATCH] [1.7.0] Implement addElement: the advanced API is complete! git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1165 48356398-32a2-884e-a903-53898d9a118a --- NEWS | 2 ++ library/HTMLPurifier/HTMLDefinition.php | 23 ++++++++++++++++++++++- tests/HTMLPurifier/HTMLDefinitionTest.php | 17 +++++++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index bfe6a285..0219e8be 100644 --- a/NEWS +++ b/NEWS @@ -38,6 +38,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier ! Implement TinyMCE styled whitelist specification format in %HTML.Allowed ! Config object gives more friendly error messages when things go wrong +! Advanced API implemented: easy functions for creating elements (addElement) + and attributes (addAttribute) on HTMLDefinition - Deprecated and removed EnableRedundantUTF8Cleaning. It didn't even work! - DOMLex will not emit errors when a custom error handler that does not honor error_reporting is used diff --git a/library/HTMLPurifier/HTMLDefinition.php b/library/HTMLPurifier/HTMLDefinition.php index d0a1f1c7..b621fb6f 100644 --- a/library/HTMLPurifier/HTMLDefinition.php +++ b/library/HTMLPurifier/HTMLDefinition.php @@ -208,6 +208,10 @@ class HTMLPurifier_HTMLDefinition extends HTMLPurifier_Definition /** * Adds a custom attribute to a pre-existing element + * @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 + * HTMLPurifier_AttrTypes for details */ function addAttribute($element_name, $attr_name, $def) { $module =& $this->getAnonymousModule(); @@ -215,8 +219,23 @@ class HTMLPurifier_HTMLDefinition extends HTMLPurifier_Definition $element->attr[$attr_name] = $def; } - var $_anonModule; + /** + * Adds a custom element to your HTML definition + * @note See HTMLPurifier_HTMLModule::addElement for detailed + * parameter descriptions. + */ + function addElement($element_name, $type, $contents, $attr_collections, $attributes) { + $module =& $this->getAnonymousModule(); + // assume that if the user is calling this, the element + // is safe. This may not be a good idea + $module->addElement($element_name, true, $type, $contents, $attr_collections, $attributes); + } + /** + * Retrieves a reference to the anonymous module, so you can + * bust out advanced features without having to make your own + * module. + */ function &getAnonymousModule() { if (!$this->_anonModule) { $this->_anonModule = new HTMLPurifier_HTMLModule(); @@ -225,6 +244,8 @@ class HTMLPurifier_HTMLDefinition extends HTMLPurifier_Definition return $this->_anonModule; } + var $_anonModule; + // PUBLIC BUT INTERNAL VARIABLES -------------------------------------- diff --git a/tests/HTMLPurifier/HTMLDefinitionTest.php b/tests/HTMLPurifier/HTMLDefinitionTest.php index d95a6da9..13804472 100644 --- a/tests/HTMLPurifier/HTMLDefinitionTest.php +++ b/tests/HTMLPurifier/HTMLDefinitionTest.php @@ -57,10 +57,8 @@ class HTMLPurifier_HTMLDefinitionTest extends UnitTestCase $config = HTMLPurifier_Config::create(array( 'HTML.DefinitionID' => 'HTMLPurifier_HTMLDefinitionTest->test_addAttribute' )); - $config->revision = 9; $def =& $config->getHTMLDefinition(true); $def->addAttribute('span', 'custom', 'Enum#attribute'); - $def = $config->getHTMLDefinition(); $purifier = new HTMLPurifier($config); $input = 'Custom!'; @@ -69,6 +67,21 @@ class HTMLPurifier_HTMLDefinitionTest extends UnitTestCase } + function test_addElement() { + + $config = HTMLPurifier_Config::create(array( + 'HTML.DefinitionID' => 'HTMLPurifier_HTMLDefinitionTest->test_addElement' + )); + $def =& $config->getHTMLDefinition(true); + $def->addElement('marquee', 'Inline', 'Inline', 'Common', array('width' => 'Length')); + + $purifier = new HTMLPurifier($config); + $input = 'Foobar'; + $output = $purifier->purify($input); + $this->assertIdentical($input, $output); + + } + } ?> \ No newline at end of file