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

[1.7.0] Convert Image, Legacy and List to use new format.

- Make attribute array parameter optional
- Optimize contents parsing for keywords

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1041 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-05-09 22:01:07 +00:00
parent b81fb0af90
commit 7f39e1e2c3
5 changed files with 52 additions and 44 deletions

View File

@ -139,7 +139,7 @@ class HTMLPurifier_HTMLModule
* @note See ElementDef for in-depth descriptions of these parameters.
* @protected
*/
function addElement($element, $safe, $type, $contents, $attr_includes, $attr) {
function addElement($element, $safe, $type, $contents, $attr_includes, $attr = array()) {
$this->elements[] = $element;
// parse content_model
list($content_model_type, $content_model) = $this->parseContents($contents);
@ -175,12 +175,12 @@ class HTMLPurifier_HTMLModule
function parseContents($contents) {
switch ($contents) {
// check for shorthand content model forms
case 'Empty':
return array('empty', '');
case 'Inline':
$contents = 'Optional: Inline | #PCDATA';
break;
return array('optional', 'Inline | #PCDATA');
case 'Flow':
$contents = 'Optional: Flow | #PCDATA';
break;
return array('optional', 'Flow | #PCDATA');
}
list($content_model_type, $content_model) = explode(':', $contents);
$content_model_type = strtolower(trim($content_model_type));

View File

@ -18,16 +18,16 @@ class HTMLPurifier_HTMLModule_Image extends HTMLPurifier_HTMLModule
var $content_sets = array('Inline' => 'img');
function HTMLPurifier_HTMLModule_Image() {
$this->info['img'] = new HTMLPurifier_ElementDef();
$this->info['img']->attr = array(
0 => array('Common'),
$this->addElement(
'img', true, 'Inline', 'Empty', 'Common',
array(
'alt' => 'Text',
'height' => 'Length',
'longdesc' => 'URI',
'src' => new HTMLPurifier_AttrDef_URI(true), // embedded
'width' => 'Length'
)
);
$this->info['img']->content_model_type = 'empty';
$this->info['img']->attr_transform_post[] =
new HTMLPurifier_AttrTransform_ImgRequired();
}

View File

@ -22,22 +22,22 @@ class HTMLPurifier_HTMLModule_Legacy extends HTMLPurifier_HTMLModule
// incomplete
var $name = 'Legacy';
var $elements = array('u', 's', 'strike');
var $non_standalone_elements = array('li', 'ol', 'address', 'blockquote');
function HTMLPurifier_HTMLModule_Legacy() {
// setup new elements
foreach ($this->elements as $name) {
$this->info[$name] = new HTMLPurifier_ElementDef();
// for u, s, strike, as more elements get added, add
// conditionals as necessary
$this->info[$name]->content_model = 'Inline | #PCDATA';
$this->info[$name]->content_model_type = 'optional';
$this->info[$name]->attr[0] = array('Common');
}
$this->addElement(
'u', true, 'Inline', 'Inline', 'Common'
);
$this->addElement(
's', true, 'Inline', 'Inline', 'Common'
);
$this->addElement(
'strike', true, 'Inline', 'Inline', 'Common'
);
// setup modifications to old elements
foreach ($this->non_standalone_elements as $name) {
// perhaps we could make some convenience functions for these...
$elements = array('li', 'ol', 'address', 'blockquote');
foreach ($elements as $name) {
$this->info[$name] = new HTMLPurifier_ElementDef();
$this->info[$name]->standalone = false;
}

View File

@ -9,7 +9,6 @@ class HTMLPurifier_HTMLModule_List extends HTMLPurifier_HTMLModule
{
var $name = 'List';
var $elements = array('dl', 'dt', 'dd', 'ol', 'ul', 'li');
// According to the abstract schema, the List content set is a fully formed
// one or more expr, but it invariably occurs in an optional declaration
@ -19,26 +18,31 @@ class HTMLPurifier_HTMLModule_List extends HTMLPurifier_HTMLModule
// Furthermore, the actual XML Schema may disagree. Regardless,
// we don't have support for such nested expressions without using
// the incredibly inefficient and draconic Custom ChildDef.
var $content_sets = array('List' => 'dl | ol | ul', 'Flow' => 'List');
var $content_sets = array('Flow' => 'List');
function HTMLPurifier_HTMLModule_List() {
foreach ($this->elements as $element) {
$this->info[$element] = new HTMLPurifier_ElementDef();
$this->info[$element]->attr = array(0 => array('Common'));
if ($element == 'li' || $element == 'dd') {
$this->info[$element]->content_model = '#PCDATA | Flow';
$this->info[$element]->content_model_type = 'optional';
} elseif ($element == 'ol' || $element == 'ul') {
$this->info[$element]->content_model = 'li';
$this->info[$element]->content_model_type = 'required';
}
}
$this->info['dt']->content_model = '#PCDATA | Inline';
$this->info['dt']->content_model_type = 'optional';
$this->info['dl']->content_model = 'dt | dd';
$this->info['dl']->content_model_type = 'required';
// this could be a LOT more robust
$this->addElement(
'ol', true, 'List', 'Required: li', 'Common'
);
$this->addElement(
'ul', true, 'List', 'Required: li', 'Common'
);
$this->addElement(
'dl', true, 'List', 'Required: dt | dd', 'Common'
);
$this->addElement(
'li', true, false, 'Flow', 'Common'
);
$this->info['li']->auto_close = array('li' => true);
$this->addElement(
'dd', true, false, 'Flow', 'Common'
);
$this->addElement(
'dt', true, false, 'Inline', 'Common'
);
}
}

View File

@ -58,6 +58,10 @@ class HTMLPurifier_HTMLModuleTest extends UnitTestCase
$module->parseContents('Flow'),
array('optional', 'Flow | #PCDATA')
);
$this->assertIdentical(
$module->parseContents('Empty'),
array('empty', '')
);
// normalization procedures
$this->assertIdentical(