0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-05 14:11:52 +00:00

[1.7.0] Modify addElement to return a reference to the created definition, shorten other HTMLModules accordingly.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1046 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-05-12 20:44:47 +00:00
parent ed73fdd5b8
commit e0cf214c44
6 changed files with 22 additions and 16 deletions

View File

@ -137,9 +137,11 @@ class HTMLPurifier_HTMLModule
* element? * element?
* @param $attr What unique attributes does the element define? * @param $attr What unique attributes does the element define?
* @note See ElementDef for in-depth descriptions of these parameters. * @note See ElementDef for in-depth descriptions of these parameters.
* @return Reference to created element definition object, so you
* can set advanced parameters
* @protected * @protected
*/ */
function addElement($element, $safe, $type, $contents, $attr_includes, $attr = array()) { function &addElement($element, $safe, $type, $contents, $attr_includes, $attr = array()) {
$this->elements[] = $element; $this->elements[] = $element;
// parse content_model // parse content_model
list($content_model_type, $content_model) = $this->parseContents($contents); list($content_model_type, $content_model) = $this->parseContents($contents);
@ -153,6 +155,7 @@ class HTMLPurifier_HTMLModule
); );
// literal object $contents means direct child manipulation // literal object $contents means direct child manipulation
if (!is_string($contents)) $this->info[$element]->child = $contents; if (!is_string($contents)) $this->info[$element]->child = $contents;
return $this->info[$element];
} }
/** /**

View File

@ -17,7 +17,7 @@ class HTMLPurifier_HTMLModule_Bdo extends HTMLPurifier_HTMLModule
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->addElement( $bdo =& $this->addElement(
'bdo', true, 'Inline', 'Inline', array('Core', 'Lang'), 'bdo', true, 'Inline', 'Inline', array('Core', 'Lang'),
array( array(
'dir' => $dir, // required 'dir' => $dir, // required
@ -28,7 +28,8 @@ class HTMLPurifier_HTMLModule_Bdo extends HTMLPurifier_HTMLModule
// be managed with a global attribute transform) // be managed with a global attribute transform)
) )
); );
$this->info['bdo']->attr_transform_post['required-dir'] = new HTMLPurifier_AttrTransform_BdoDir(); $bdo->attr_transform_post['required-dir'] = new HTMLPurifier_AttrTransform_BdoDir();
$this->attr_collections['I18N']['dir'] = $dir; $this->attr_collections['I18N']['dir'] = $dir;
} }

View File

@ -12,7 +12,7 @@ class HTMLPurifier_HTMLModule_Hypertext extends HTMLPurifier_HTMLModule
var $name = 'Hypertext'; var $name = 'Hypertext';
function HTMLPurifier_HTMLModule_Hypertext() { function HTMLPurifier_HTMLModule_Hypertext() {
$this->addElement( $a =& $this->addElement(
'a', true, 'Inline', 'Inline', 'Common', 'a', true, 'Inline', 'Inline', 'Common',
array( array(
// 'accesskey' => 'Character', // 'accesskey' => 'Character',
@ -25,7 +25,7 @@ class HTMLPurifier_HTMLModule_Hypertext extends HTMLPurifier_HTMLModule
// 'type' => 'ContentType', // 'type' => 'ContentType',
) )
); );
$this->info['a']->excludes = array('a' => true); $a->excludes = array('a' => true);
} }
} }

View File

@ -18,7 +18,7 @@ class HTMLPurifier_HTMLModule_Image extends HTMLPurifier_HTMLModule
var $content_sets = array('Inline' => 'img'); var $content_sets = array('Inline' => 'img');
function HTMLPurifier_HTMLModule_Image() { function HTMLPurifier_HTMLModule_Image() {
$this->addElement( $img =& $this->addElement(
'img', true, 'Inline', 'Empty', 'Common', 'img', true, 'Inline', 'Empty', 'Common',
array( array(
'alt' => 'Text', 'alt' => 'Text',
@ -28,7 +28,7 @@ class HTMLPurifier_HTMLModule_Image extends HTMLPurifier_HTMLModule
'width' => 'Length' 'width' => 'Length'
) )
); );
$this->info['img']->attr_transform_post[] = $img->attr_transform_post[] =
new HTMLPurifier_AttrTransform_ImgRequired(); new HTMLPurifier_AttrTransform_ImgRequired();
} }

View File

@ -26,8 +26,8 @@ class HTMLPurifier_HTMLModule_List extends HTMLPurifier_HTMLModule
$this->addElement('ul', true, 'List', 'Required: li', 'Common'); $this->addElement('ul', true, 'List', 'Required: li', 'Common');
$this->addElement('dl', true, 'List', 'Required: dt | dd', 'Common'); $this->addElement('dl', true, 'List', 'Required: dt | dd', 'Common');
$this->addElement('li', true, false, 'Flow', 'Common'); $li =& $this->addElement('li', true, false, 'Flow', 'Common');
$this->info['li']->auto_close = array('li' => true); $li->auto_close = array('li' => true);
$this->addElement('dd', true, false, 'Flow', 'Common'); $this->addElement('dd', true, false, 'Flow', 'Common');
$this->addElement('dt', true, false, 'Inline', 'Common'); $this->addElement('dt', true, false, 'Inline', 'Common');

View File

@ -21,7 +21,7 @@ class HTMLPurifier_HTMLModuleTest extends UnitTestCase
function test_addElement() { function test_addElement() {
$module = new HTMLPurifier_HTMLModule(); $module = new HTMLPurifier_HTMLModule();
$module->addElement( $def =& $module->addElement(
'a', true, 'Inline', 'Optional: #PCDATA', array('Common'), 'a', true, 'Inline', 'Optional: #PCDATA', array('Common'),
array( array(
'href' => 'URI' 'href' => 'URI'
@ -29,19 +29,21 @@ class HTMLPurifier_HTMLModuleTest extends UnitTestCase
); );
$module2 = new HTMLPurifier_HTMLModule(); $module2 = new HTMLPurifier_HTMLModule();
$def = new HTMLPurifier_ElementDef(); $def2 = new HTMLPurifier_ElementDef();
$def->safe = true; $def2->safe = true;
$def->content_model = '#PCDATA'; $def2->content_model = '#PCDATA';
$def->content_model_type = 'optional'; $def2->content_model_type = 'optional';
$def->attr = array( $def2->attr = array(
'href' => 'URI', 'href' => 'URI',
0 => array('Common') 0 => array('Common')
); );
$module2->info['a'] = $def; $module2->info['a'] = $def2;
$module2->elements = array('a'); $module2->elements = array('a');
$module2->content_sets['Inline'] = 'a'; $module2->content_sets['Inline'] = 'a';
$this->assertIdentical($module, $module2); $this->assertIdentical($module, $module2);
$this->assertIdentical($def, $def2);
$this->assertReference($def, $module->info['a']);
} }