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

[1.7.0] Create convenience functions for HTMLModule constructors, HTMLModule_Bdo was hooked up

- Add initial "safe" property for elements, is not set for most though

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1039 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-05-07 01:51:26 +00:00
parent ac50d333a5
commit 47fe34ad81
6 changed files with 126 additions and 17 deletions

1
NEWS
View File

@ -12,6 +12,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
1.7.0, unknown release date 1.7.0, unknown release date
. Unit test for ElementDef created, ElementDef behavior modified to . Unit test for ElementDef created, ElementDef behavior modified to
be more flexible be more flexible
. Added convenience functions for HTMLModule constructors
1.6.1, released 2007-05-05 1.6.1, released 2007-05-05
! Support for more deprecated attributes via transformations: ! Support for more deprecated attributes via transformations:

View File

@ -58,6 +58,7 @@ class HTMLPurifier_ElementDef
/** /**
* Value of $child->type, used to determine which ChildDef to use, * Value of $child->type, used to determine which ChildDef to use,
* used in combination with $content_model. * used in combination with $content_model.
* @warning This must be lowercase
* @public * @public
*/ */
var $content_model_type; var $content_model_type;
@ -86,6 +87,24 @@ class HTMLPurifier_ElementDef
*/ */
var $excludes = array(); var $excludes = array();
/**
* Is this element safe for untrusted users to use?
*/
var $safe = false;
/**
* Factory constructor for creating new standalone element defs
* @static
*/
function create($safe, $content_model, $content_model_type, $attr) {
$def = new HTMLPurifier_ElementDef();
$def->safe = (bool) $safe;
$def->content_model = $content_model;
$def->content_model_type = $content_model_type;
$def->attr = $attr;
return $def;
}
/** /**
* Merges the values of another element definition into this one. * Merges the values of another element definition into this one.
* Values from the new element def take precedence if a value is * Values from the new element def take precedence if a value is

View File

@ -120,6 +120,49 @@ class HTMLPurifier_HTMLModule
*/ */
function setup(&$definition) {} function setup(&$definition) {}
/**
* Convenience function that sets up a new element
* @param $element Name of element to add
* @param $safe Is element safe for untrusted users to use?
* @param $type What content set should element be registered to?
* Set as false to skip this step.
* @param $content_model Content model definition in form of:
* "$content_model_type: $content_model"
* @param $attr_includes What attribute collections to register to
* element?
* @param $attr What unique attributes does the element define?
* @note See ElementDef for in-depth descriptions of these parameters.
* @protected
*/
function addElement($element, $safe, $type, $content_model, $attr_includes, $attr) {
$this->elements[] = $element;
// parse content_model
list($content_model_type, $content_model) = explode(':', $content_model);
$content_model_type = strtolower(trim($content_model_type));
$content_model = trim($content_model);
// merge in attribute inclusions
$attr[0] = $attr_includes;
// add element to content sets
if ($type) $this->addElementToContentSet($element, $type);
// create element
$this->info[$element] = HTMLPurifier_ElementDef::create(
$safe, $content_model, $content_model_type, $attr
);
}
/**
* Convenience function that registers an element to a content set
* @param Element to register
* @param Name content set (warning: case sensitive, usually upper-case
* first letter)
* @protected
*/
function addElementToContentSet($element, $type) {
if (!isset($this->content_sets[$type])) $this->content_sets[$type] = '';
else $this->content_sets[$type] .= ' | ';
$this->content_sets[$type] .= $element;
}
} }
?> ?>

View File

@ -11,30 +11,25 @@ class HTMLPurifier_HTMLModule_Bdo extends HTMLPurifier_HTMLModule
{ {
var $name = 'Bdo'; var $name = 'Bdo';
var $elements = array('bdo');
var $content_sets = array('Inline' => 'bdo');
var $attr_collections = array( var $attr_collections = array(
'I18N' => array('dir' => false) 'I18N' => array('dir' => false)
); );
function HTMLPurifier_HTMLModule_Bdo() { function HTMLPurifier_HTMLModule_Bdo() {
$dir = new HTMLPurifier_AttrDef_Enum(array('ltr','rtl'), false); $dir = new HTMLPurifier_AttrDef_Enum(array('ltr','rtl'), false);
$this->attr_collections['I18N']['dir'] = $dir; $this->addElement(
$this->info['bdo'] = new HTMLPurifier_ElementDef(); 'bdo', true, 'Inline', 'Optional: #PCDATA | Inline', array('Core', 'Lang'),
$this->info['bdo']->attr = array( array(
0 => array('Core', 'Lang'),
'dir' => $dir, // required 'dir' => $dir, // required
// The Abstract Module specification has the attribute // The Abstract Module specification has the attribute
// inclusions wrong for bdo: bdo allows // inclusions wrong for bdo: bdo allows
// xml:lang too (and we'll toss in lang for good measure, // xml:lang too (and we'll toss in lang for good measure,
// though it is not allowed for XHTML 1.1, this will // though it is not allowed for XHTML 1.1, this will
// be managed with a global attribute transform) // be managed with a global attribute transform)
)
); );
$this->info['bdo']->content_model = '#PCDATA | Inline'; $this->info['bdo']->attr_transform_post['required-dir'] = new HTMLPurifier_AttrTransform_BdoDir();
$this->info['bdo']->content_model_type = 'optional'; $this->attr_collections['I18N']['dir'] = $dir;
// provides fallback behavior if dir's missing (dir is required)
$this->info['bdo']->attr_transform_post['required-dir'] =
new HTMLPurifier_AttrTransform_BdoDir();
} }
} }

View File

@ -0,0 +1,50 @@
<?php
require_once 'HTMLPurifier/HTMLModule.php';
require_once 'HTMLPurifier/AttrDef.php';
class HTMLPurifier_HTMLModuleTest extends UnitTestCase
{
function test_addElementToContentSet() {
$module = new HTMLPurifier_HTMLModule();
$module->addElementToContentSet('b', 'Inline');
$this->assertIdentical($module->content_sets, array('Inline' => 'b'));
$module->addElementToContentSet('i', 'Inline');
$this->assertIdentical($module->content_sets, array('Inline' => 'b | i'));
}
function test_addElement() {
$module = new HTMLPurifier_HTMLModule();
$module->addElement(
'a', true, 'Inline', 'Optional: #PCDATA', array('Common'),
array(
'href' => 'URI'
)
);
$module2 = new HTMLPurifier_HTMLModule();
$def = new HTMLPurifier_ElementDef();
$def->safe = true;
$def->content_model = '#PCDATA';
$def->content_model_type = 'optional';
$def->attr = array(
'href' => 'URI',
0 => array('Common')
);
$module2->info['a'] = $def;
$module2->elements = array('a');
$module2->content_sets['Inline'] = 'a';
$this->assertIdentical($module, $module2);
}
}
?>

View File

@ -61,6 +61,7 @@ $test_files[] = 'EntityLookupTest.php';
$test_files[] = 'EntityParserTest.php'; $test_files[] = 'EntityParserTest.php';
$test_files[] = 'GeneratorTest.php'; $test_files[] = 'GeneratorTest.php';
$test_files[] = 'HTMLModuleManagerTest.php'; $test_files[] = 'HTMLModuleManagerTest.php';
$test_files[] = 'HTMLModuleTest.php';
$test_files[] = 'IDAccumulatorTest.php'; $test_files[] = 'IDAccumulatorTest.php';
$test_files[] = 'LanguageFactoryTest.php'; $test_files[] = 'LanguageFactoryTest.php';
$test_files[] = 'LanguageTest.php'; $test_files[] = 'LanguageTest.php';