From 6aa3dfc11617f65db80fbb824988366cfc86906c Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Tue, 19 Jun 2007 01:29:50 +0000 Subject: [PATCH] [1.7.0] Implement addAttribute() of advanced API. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1164 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier/Config.php | 4 +++- library/HTMLPurifier/HTMLDefinition.php | 29 +++++++++++++++++++++++ tests/HTMLPurifier/HTMLDefinitionTest.php | 17 +++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/library/HTMLPurifier/Config.php b/library/HTMLPurifier/Config.php index 67780cbd..6e078eb7 100644 --- a/library/HTMLPurifier/Config.php +++ b/library/HTMLPurifier/Config.php @@ -105,11 +105,13 @@ class HTMLPurifier_Config */ function create($config) { if (is_a($config, 'HTMLPurifier_Config')) { - $config = $config->conf; // create a clone + // pass-through + return $config; } $ret = HTMLPurifier_Config::createDefault(); if (is_string($config)) $ret->loadIni($config); elseif (is_array($config)) $ret->loadArray($config); + if (isset($revision)) $ret->revision = $revision; return $ret; } diff --git a/library/HTMLPurifier/HTMLDefinition.php b/library/HTMLPurifier/HTMLDefinition.php index dfe71114..d0a1f1c7 100644 --- a/library/HTMLPurifier/HTMLDefinition.php +++ b/library/HTMLPurifier/HTMLDefinition.php @@ -204,6 +204,27 @@ class HTMLPurifier_HTMLDefinition extends HTMLPurifier_Definition + // RAW CUSTOMIZATION STUFF -------------------------------------------- + + /** + * Adds a custom attribute to a pre-existing element + */ + function addAttribute($element_name, $attr_name, $def) { + $module =& $this->getAnonymousModule(); + $element =& $module->addBlankElement($element_name); + $element->attr[$attr_name] = $def; + } + + var $_anonModule; + + function &getAnonymousModule() { + if (!$this->_anonModule) { + $this->_anonModule = new HTMLPurifier_HTMLModule(); + $this->_anonModule->name = 'Anonymous'; + } + return $this->_anonModule; + } + // PUBLIC BUT INTERNAL VARIABLES -------------------------------------- @@ -234,6 +255,14 @@ class HTMLPurifier_HTMLDefinition extends HTMLPurifier_Definition */ function processModules($config) { + if ($this->_anonModule) { + // for user specific changes + // this is late-loaded so we don't have to deal with PHP4 + // reference wonky-ness + $this->manager->addModule($this->_anonModule); + unset($this->_anonModule); + } + $this->manager->setup($config); $this->doctype = $this->manager->doctype; diff --git a/tests/HTMLPurifier/HTMLDefinitionTest.php b/tests/HTMLPurifier/HTMLDefinitionTest.php index b9c77e07..d95a6da9 100644 --- a/tests/HTMLPurifier/HTMLDefinitionTest.php +++ b/tests/HTMLPurifier/HTMLDefinitionTest.php @@ -52,6 +52,23 @@ class HTMLPurifier_HTMLDefinitionTest extends UnitTestCase } + function test_addAttribute() { + + $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!'; + $output = $purifier->purify($input); + $this->assertIdentical($input, $output); + + } + } ?> \ No newline at end of file