mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-31 11:20:13 +02:00
Release 2.0.0, merged in 1026 to HEAD.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/branches/strict@1179 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -16,16 +16,14 @@
|
||||
|
||||
class HTMLPurifier_HTMLModule
|
||||
{
|
||||
|
||||
// -- Overloadable ----------------------------------------------------
|
||||
|
||||
/**
|
||||
* Short unique string identifier of the module
|
||||
*/
|
||||
var $name;
|
||||
|
||||
/**
|
||||
* Dynamically set integer that specifies when the module was loaded in.
|
||||
*/
|
||||
var $order;
|
||||
|
||||
/**
|
||||
* Informally, a list of elements this module changes. Not used in
|
||||
* any significant way.
|
||||
@@ -99,27 +97,128 @@ class HTMLPurifier_HTMLModule
|
||||
*/
|
||||
function getChildDef($def) {return false;}
|
||||
|
||||
/**
|
||||
* Hook method that lets module perform arbitrary operations on
|
||||
* HTMLPurifier_HTMLDefinition before the module gets processed.
|
||||
* @param $definition Reference to HTMLDefinition being setup
|
||||
*/
|
||||
function preProcess(&$definition) {}
|
||||
// -- Convenience -----------------------------------------------------
|
||||
|
||||
/**
|
||||
* Hook method that lets module perform arbitrary operations
|
||||
* on HTMLPurifier_HTMLDefinition after the module gets processed.
|
||||
* @param $definition Reference to HTMLDefinition being setup
|
||||
* 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 $contents Allowed children 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.
|
||||
* @return Reference to created element definition object, so you
|
||||
* can set advanced parameters
|
||||
* @protected
|
||||
*/
|
||||
function postProcess(&$definition) {}
|
||||
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);
|
||||
// merge in attribute inclusions
|
||||
$this->mergeInAttrIncludes($attr, $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
|
||||
);
|
||||
// literal object $contents means direct child manipulation
|
||||
if (!is_string($contents)) $this->info[$element]->child = $contents;
|
||||
return $this->info[$element];
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook method that is called when a module gets registered to
|
||||
* the definition.
|
||||
* @param $definition Reference to HTMLDefinition being setup
|
||||
* Convenience function that creates a totally blank, non-standalone
|
||||
* element.
|
||||
* @param $element Name of element to create
|
||||
* @return Reference to created element
|
||||
*/
|
||||
function setup(&$definition) {}
|
||||
function &addBlankElement($element) {
|
||||
if (!isset($this->info[$element])) {
|
||||
$this->elements[] = $element;
|
||||
$this->info[$element] = new HTMLPurifier_ElementDef();
|
||||
$this->info[$element]->standalone = false;
|
||||
} else {
|
||||
trigger_error("Definition for $element already exists in module, cannot redefine");
|
||||
}
|
||||
return $this->info[$element];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function that transforms single-string contents
|
||||
* into separate content model and content model type
|
||||
* @param $contents Allowed children in form of:
|
||||
* "$content_model_type: $content_model"
|
||||
* @note If contents is an object, an array of two nulls will be
|
||||
* returned, and the callee needs to take the original $contents
|
||||
* and use it directly.
|
||||
*/
|
||||
function parseContents($contents) {
|
||||
if (!is_string($contents)) return array(null, null); // defer
|
||||
switch ($contents) {
|
||||
// check for shorthand content model forms
|
||||
case 'Empty':
|
||||
return array('empty', '');
|
||||
case 'Inline':
|
||||
return array('optional', 'Inline | #PCDATA');
|
||||
case 'Flow':
|
||||
return array('optional', 'Flow | #PCDATA');
|
||||
}
|
||||
list($content_model_type, $content_model) = explode(':', $contents);
|
||||
$content_model_type = strtolower(trim($content_model_type));
|
||||
$content_model = trim($content_model);
|
||||
return array($content_model_type, $content_model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function that merges a list of attribute includes into
|
||||
* an attribute array.
|
||||
* @param $attr Reference to attr array to modify
|
||||
* @param $attr_includes Array of includes / string include to merge in
|
||||
*/
|
||||
function mergeInAttrIncludes(&$attr, $attr_includes) {
|
||||
if (!is_array($attr_includes)) {
|
||||
if (empty($attr_includes)) $attr_includes = array();
|
||||
else $attr_includes = array($attr_includes);
|
||||
}
|
||||
$attr[0] = $attr_includes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function that generates a lookup table with boolean
|
||||
* true as value.
|
||||
* @param $list List of values to turn into a lookup
|
||||
* @note You can also pass an arbitrary number of arguments in
|
||||
* place of the regular argument
|
||||
* @return Lookup array equivalent of list
|
||||
*/
|
||||
function makeLookup($list) {
|
||||
if (is_string($list)) $list = func_get_args();
|
||||
$ret = array();
|
||||
foreach ($list as $value) {
|
||||
if (is_null($value)) continue;
|
||||
$ret[$value] = true;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user