0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 23:28:42 +00:00

[1.7.0] Implement addAttribute() of advanced API.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1164 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-06-19 01:29:50 +00:00
parent c3094275ef
commit 6aa3dfc116
3 changed files with 49 additions and 1 deletions

View File

@ -105,11 +105,13 @@ class HTMLPurifier_Config
*/ */
function create($config) { function create($config) {
if (is_a($config, 'HTMLPurifier_Config')) { if (is_a($config, 'HTMLPurifier_Config')) {
$config = $config->conf; // create a clone // pass-through
return $config;
} }
$ret = HTMLPurifier_Config::createDefault(); $ret = HTMLPurifier_Config::createDefault();
if (is_string($config)) $ret->loadIni($config); if (is_string($config)) $ret->loadIni($config);
elseif (is_array($config)) $ret->loadArray($config); elseif (is_array($config)) $ret->loadArray($config);
if (isset($revision)) $ret->revision = $revision;
return $ret; return $ret;
} }

View File

@ -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 -------------------------------------- // PUBLIC BUT INTERNAL VARIABLES --------------------------------------
@ -234,6 +255,14 @@ class HTMLPurifier_HTMLDefinition extends HTMLPurifier_Definition
*/ */
function processModules($config) { 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->manager->setup($config);
$this->doctype = $this->manager->doctype; $this->doctype = $this->manager->doctype;

View File

@ -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 = '<span custom="attribute">Custom!</span>';
$output = $purifier->purify($input);
$this->assertIdentical($input, $output);
}
} }
?> ?>