0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-18 18:25:18 +00:00

[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
This commit is contained in:
Edward Z. Yang 2007-06-19 01:55:31 +00:00
parent 6aa3dfc116
commit 0b0a505c30
3 changed files with 39 additions and 3 deletions

2
NEWS
View File

@ -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

View File

@ -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 --------------------------------------

View File

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