1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-08-06 06:07:26 +02:00

[1.7.0] Various updates

- Implement addModule(), requires new userModules property
- Remove unnecessary $config passing for getElement(s)
- Revamp HTMLModuleManagerTest
- Fix buggy unit test for unrecognized parent
- Remove anonymous generator member variable from ChildDef_Required

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1063 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2007-05-15 02:33:19 +00:00
parent 2cbb3be602
commit 831a09d455
6 changed files with 86 additions and 280 deletions

View File

@@ -29,7 +29,6 @@ class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
}
}
$this->elements = $elements;
$this->gen = new HTMLPurifier_Generator();
}
var $allow_empty = false;
var $type = 'required';
@@ -57,6 +56,12 @@ class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
// some configuration
$escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
// generator
static $gen = null;
if ($gen === null) {
$gen = new HTMLPurifier_Generator();
}
foreach ($tokens_of_children as $token) {
if (!empty($token->is_whitespace)) {
$result[] = $token;
@@ -80,7 +85,7 @@ class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
$result[] = $token;
} elseif ($pcdata_allowed && $escape_invalid_children) {
$result[] = new HTMLPurifier_Token_Text(
$this->gen->generateFromToken($token, $config)
$gen->generateFromToken($token, $config)
);
}
continue;
@@ -91,7 +96,7 @@ class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
} elseif ($pcdata_allowed && $escape_invalid_children) {
$result[] =
new HTMLPurifier_Token_Text(
$this->gen->generateFromToken( $token, $config )
$gen->generateFromToken( $token, $config )
);
} else {
// drop silently

View File

@@ -198,7 +198,7 @@ class HTMLPurifier_HTMLDefinition
}
}
$this->info = $this->manager->getElements($this->config);
$this->info = $this->manager->getElements();
$this->info_content_sets = $this->manager->contentSets->lookup;
}
@@ -217,15 +217,14 @@ class HTMLPurifier_HTMLDefinition
}
$parent = $this->config->get('HTML', 'Parent');
$def = $this->manager->getElement($parent, $this->config);
$def = $this->manager->getElement($parent, true);
if ($def) {
$this->info_parent = $parent;
$this->info_parent_def = $def;
} else {
trigger_error('Cannot use unrecognized element as parent.',
E_USER_ERROR);
$this->info_parent_def = $this->manager->getElement(
$this->info_parent, $this->config);
$this->info_parent_def = $this->manager->getElement($this->info_parent, true);
}
// support template text

View File

@@ -136,7 +136,7 @@ class HTMLPurifier_HTMLModule
* can set advanced parameters
* @protected
*/
function &addElement($element, $safe, $type, $contents, $attr_includes, $attr = array()) {
function &addElement($element, $safe, $type, $contents, $attr_includes = array(), $attr = array()) {
$this->elements[] = $element;
// parse content_model
list($content_model_type, $content_model) = $this->parseContents($contents);

View File

@@ -76,6 +76,13 @@ class HTMLPurifier_HTMLModuleManager
*/
var $registeredModules = array();
/**
* List of extra modules that were added by the user using addModule().
* These get unconditionally merged into the current doctype, whatever
* it may be.
*/
var $userModules = array();
/**
* Associative array of element name to list of modules that have
* definitions for the element; this array is dynamically filled.
@@ -210,7 +217,9 @@ class HTMLPurifier_HTMLModuleManager
* and then tacking it on to the active doctype
*/
function addModule($module) {
// unimplemented
$this->registerModule($module);
if (is_object($module)) $module = $module->name;
$this->userModules[] = $module;
}
/**
@@ -234,16 +243,19 @@ class HTMLPurifier_HTMLModuleManager
$doctype = $this->doctypes->make($config);
$modules = $doctype->modules;
// merge in custom modules
$modules = array_merge($modules, $this->userModules);
foreach ($modules as $module) {
if (is_object($module)) {
$this->modules[$module->name] = $module;
$this->registeredModules[$module->name] = $module;
continue;
} else {
if (!isset($this->modules[$module])) {
if (!isset($this->registeredModules[$module])) {
$this->registerModule($module);
}
$this->modules[$module] = $this->registeredModules[$module];
}
$this->modules[$module] = $this->registeredModules[$module];
}
// setup lookup table based on all valid modules
@@ -274,11 +286,9 @@ class HTMLPurifier_HTMLModuleManager
/**
* Retrieves merged element definitions.
* @param $config Instance of HTMLPurifier_Config, for determining
* stray elements.
* @return Array of HTMLPurifier_ElementDef
*/
function getElements($config) {
function getElements() {
$elements = array();
foreach ($this->modules as $module) {
@@ -286,7 +296,7 @@ class HTMLPurifier_HTMLModuleManager
if (isset($elements[$name])) continue;
// if element is not safe, don't use it
if (!$this->trusted && ($v->safe === false)) continue;
$elements[$name] = $this->getElement($name, $config);
$elements[$name] = $this->getElement($name);
}
}
@@ -303,12 +313,11 @@ class HTMLPurifier_HTMLModuleManager
/**
* Retrieves a single merged element definition
* @param $name Name of element
* @param $config Instance of HTMLPurifier_Config, may not be necessary.
* @param $trusted Boolean trusted overriding parameter: set to true
* if you want the full version of an element
* @return Merged HTMLPurifier_ElementDef
*/
function getElement($name, $config, $trusted = null) {
function getElement($name, $trusted = null) {
$def = false;
if ($trusted === null) $trusted = $this->trusted;