ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
HTMLPurifier_HTMLDefinition Class Reference

Definition of the purified HTML that describes allowed children, attributes, and many other things. More...

+ Inheritance diagram for HTMLPurifier_HTMLDefinition:
+ Collaboration diagram for HTMLPurifier_HTMLDefinition:

Public Member Functions

 addAttribute ($element_name, $attr_name, $def)
 Adds a custom attribute to a pre-existing element.
 addElement ($element_name, $type, $contents, $attr_collections, $attributes=array())
 Adds a custom element to your HTML definition.
 addBlankElement ($element_name)
 Adds a blank element to your HTML definition, for overriding existing behavior.
 getAnonymousModule ()
 Retrieves a reference to the anonymous module, so you can bust out advanced features without having to make your own module.
 __construct ()
 Performs low-cost, preliminary initialization.
 parseTinyMCEAllowedList ($list)
 Parses a TinyMCE-flavored Allowed Elements and Attributes list into separate lists for processing.
- Public Member Functions inherited from HTMLPurifier_Definition
 setup ($config)
 Setup function that aborts if already setup.

Data Fields

 $info = array()
 Associative array of element names to HTMLPurifier_ElementDef.
 $info_global_attr = array()
 Associative array of global attribute name to attribute definition.
 $info_parent = 'div'
 String name of parent element HTML will be going into.
 $info_parent_def
 Definition for parent element, allows parent element to be a tag that's not allowed inside the HTML fragment.
 $info_block_wrapper = 'p'
 String name of element used to wrap inline elements in block context.
 $info_tag_transform = array()
 Associative array of deprecated tag name to HTMLPurifier_TagTransform.
 $info_attr_transform_pre = array()
 Indexed list of HTMLPurifier_AttrTransform to be performed before validation.
 $info_attr_transform_post = array()
 Indexed list of HTMLPurifier_AttrTransform to be performed after validation.
 $info_content_sets = array()
 Nested lookup array of content set name (Block, Inline) to element name to whether or not it belongs in that content set.
 $info_injector = array()
 Indexed list of HTMLPurifier_Injector to be used.
 $doctype
 Doctype object.
 $type = 'HTML'
 $manager
 Instance of HTMLPurifier_HTMLModuleManager.
- Data Fields inherited from HTMLPurifier_Definition
 $setup = false
 Has setup() been called yet?
 $optimized = null
 If true, write out the final definition object to the cache after setup.
 $type
 What type of definition is it?

Protected Member Functions

 doSetup ($config)
 Sets up the definition object into the final form, something not done by the constructor.
 processModules ($config)
 Extract out the information from the manager.
 setupConfigStuff ($config)
 Sets up stuff based on config.

Private Attributes

 $_anonModule

Detailed Description

Definition of the purified HTML that describes allowed children, attributes, and many other things.

Conventions:

All member variables that are prefixed with info (including the main $info array) are used by HTML Purifier internals and should not be directly edited when customizing the HTMLDefinition. They can usually be set via configuration directives or custom modules.

On the other hand, member variables without the info prefix are used internally by the HTMLDefinition and MUST NOT be used by other HTML Purifier internals. Many of them, however, are public, and may be edited by userspace code to tweak the behavior of HTMLDefinition.

Note
This class is inspected by Printer_HTMLDefinition; please update that class if things here change.
Warning
Directives that change this object's structure must be in the HTML or Attr namespace!

Definition at line 26 of file HTMLDefinition.php.

Constructor & Destructor Documentation

HTMLPurifier_HTMLDefinition::__construct ( )

Performs low-cost, preliminary initialization.

Definition at line 161 of file HTMLDefinition.php.

{
$this->manager = new HTMLPurifier_HTMLModuleManager();
}

Member Function Documentation

HTMLPurifier_HTMLDefinition::addAttribute (   $element_name,
  $attr_name,
  $def 
)

Adds a custom attribute to a pre-existing element.

Note
This is strictly convenience, and does not have a corresponding method in HTMLPurifier_HTMLModule
Parameters
$element_nameString element name to add attribute to
$attr_nameString name of attribute
$defAttribute definition, can be string or object, see HTMLPurifier_AttrTypes for details

Definition at line 102 of file HTMLDefinition.php.

References getAnonymousModule().

{
$module = $this->getAnonymousModule();
if (!isset($module->info[$element_name])) {
$element = $module->addBlankElement($element_name);
} else {
$element = $module->info[$element_name];
}
$element->attr[$attr_name] = $def;
}

+ Here is the call graph for this function:

HTMLPurifier_HTMLDefinition::addBlankElement (   $element_name)

Adds a blank element to your HTML definition, for overriding existing behavior.

Note
See HTMLPurifier_HTMLModule::addBlankElement for detailed parameter and return value descriptions.

Definition at line 131 of file HTMLDefinition.php.

References getAnonymousModule().

{
$module = $this->getAnonymousModule();
$element = $module->addBlankElement($element_name);
return $element;
}

+ Here is the call graph for this function:

HTMLPurifier_HTMLDefinition::addElement (   $element_name,
  $type,
  $contents,
  $attr_collections,
  $attributes = array() 
)

Adds a custom element to your HTML definition.

Note
See HTMLPurifier_HTMLModule::addElement for detailed parameter and return value descriptions.

Definition at line 117 of file HTMLDefinition.php.

References $type, and getAnonymousModule().

{
$module = $this->getAnonymousModule();
// assume that if the user is calling this, the element
// is safe. This may not be a good idea
$element = $module->addElement($element_name, $type, $contents, $attr_collections, $attributes);
return $element;
}

+ Here is the call graph for this function:

HTMLPurifier_HTMLDefinition::doSetup (   $config)
protected

Sets up the definition object into the final form, something not done by the constructor.

Parameters
$configHTMLPurifier_Config instance

Reimplemented from HTMLPurifier_Definition.

Definition at line 165 of file HTMLDefinition.php.

References $config, processModules(), and setupConfigStuff().

{
unset($this->manager);
// cleanup some of the element definitions
foreach ($this->info as $k => $v) {
unset($this->info[$k]->content_model);
unset($this->info[$k]->content_model_type);
}
}

+ Here is the call graph for this function:

HTMLPurifier_HTMLDefinition::getAnonymousModule ( )

Retrieves a reference to the anonymous module, so you can bust out advanced features without having to make your own module.

Definition at line 142 of file HTMLDefinition.php.

References $_anonModule.

Referenced by addAttribute(), addBlankElement(), and addElement().

{
if (!$this->_anonModule) {
$this->_anonModule = new HTMLPurifier_HTMLModule();
$this->_anonModule->name = 'Anonymous';
}
}

+ Here is the caller graph for this function:

HTMLPurifier_HTMLDefinition::parseTinyMCEAllowedList (   $list)

Parses a TinyMCE-flavored Allowed Elements and Attributes list into separate lists for processing.

Format is element[attr1|attr2],element2...

Warning
Although it's largely drawn from TinyMCE's implementation, it is different, and you'll probably have to modify your lists
Parameters
$listString list to parse
array($allowed_elements,$allowed_attributes)
Todo:
Give this its own class, probably static interface

Definition at line 392 of file HTMLDefinition.php.

Referenced by setupConfigStuff().

{
$list = str_replace(array(' ', "\t"), '', $list);
$elements = array();
$attributes = array();
$chunks = preg_split('/(,|[\n\r]+)/', $list);
foreach ($chunks as $chunk) {
if (empty($chunk)) continue;
// remove TinyMCE element control characters
if (!strpos($chunk, '[')) {
$element = $chunk;
$attr = false;
} else {
list($element, $attr) = explode('[', $chunk);
}
if ($element !== '*') $elements[$element] = true;
if (!$attr) continue;
$attr = substr($attr, 0, strlen($attr) - 1); // remove trailing ]
$attr = explode('|', $attr);
foreach ($attr as $key) {
$attributes["$element.$key"] = true;
}
}
return array($elements, $attributes);
}

+ Here is the caller graph for this function:

HTMLPurifier_HTMLDefinition::processModules (   $config)
protected

Extract out the information from the manager.

Definition at line 180 of file HTMLDefinition.php.

References $config.

Referenced by doSetup().

{
if ($this->_anonModule) {
// for user specific changes
// this is late-loaded so we don't have to deal with PHP4
// reference wonky-ness
$this->manager->addModule($this->_anonModule);
unset($this->_anonModule);
}
$this->manager->setup($config);
$this->doctype = $this->manager->doctype;
foreach ($this->manager->modules as $module) {
foreach($module->info_tag_transform as $k => $v) {
if ($v === false) unset($this->info_tag_transform[$k]);
else $this->info_tag_transform[$k] = $v;
}
foreach($module->info_attr_transform_pre as $k => $v) {
if ($v === false) unset($this->info_attr_transform_pre[$k]);
else $this->info_attr_transform_pre[$k] = $v;
}
foreach($module->info_attr_transform_post as $k => $v) {
if ($v === false) unset($this->info_attr_transform_post[$k]);
else $this->info_attr_transform_post[$k] = $v;
}
foreach ($module->info_injector as $k => $v) {
if ($v === false) unset($this->info_injector[$k]);
else $this->info_injector[$k] = $v;
}
}
$this->info = $this->manager->getElements();
$this->info_content_sets = $this->manager->contentSets->lookup;
}

+ Here is the caller graph for this function:

HTMLPurifier_HTMLDefinition::setupConfigStuff (   $config)
protected

Sets up stuff based on config.

We need a better way of doing this.

Definition at line 220 of file HTMLDefinition.php.

References $config, $d, $info, and parseTinyMCEAllowedList().

Referenced by doSetup().

{
$block_wrapper = $config->get('HTML.BlockWrapper');
if (isset($this->info_content_sets['Block'][$block_wrapper])) {
$this->info_block_wrapper = $block_wrapper;
} else {
trigger_error('Cannot use non-block element as block wrapper',
E_USER_ERROR);
}
$parent = $config->get('HTML.Parent');
$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, true);
}
// support template text
$support = "(for information on implementing this, see the ".
"support forums) ";
// setup allowed elements -----------------------------------------
$allowed_elements = $config->get('HTML.AllowedElements');
$allowed_attributes = $config->get('HTML.AllowedAttributes'); // retrieve early
if (!is_array($allowed_elements) && !is_array($allowed_attributes)) {
$allowed = $config->get('HTML.Allowed');
if (is_string($allowed)) {
list($allowed_elements, $allowed_attributes) = $this->parseTinyMCEAllowedList($allowed);
}
}
if (is_array($allowed_elements)) {
foreach ($this->info as $name => $d) {
if(!isset($allowed_elements[$name])) unset($this->info[$name]);
unset($allowed_elements[$name]);
}
// emit errors
foreach ($allowed_elements as $element => $d) {
$element = htmlspecialchars($element); // PHP doesn't escape errors, be careful!
trigger_error("Element '$element' is not supported $support", E_USER_WARNING);
}
}
// setup allowed attributes ---------------------------------------
$allowed_attributes_mutable = $allowed_attributes; // by copy!
if (is_array($allowed_attributes)) {
// This actually doesn't do anything, since we went away from
// global attributes. It's possible that userland code uses
// it, but HTMLModuleManager doesn't!
foreach ($this->info_global_attr as $attr => $x) {
$keys = array($attr, "*@$attr", "*.$attr");
$delete = true;
foreach ($keys as $key) {
if ($delete && isset($allowed_attributes[$key])) {
$delete = false;
}
if (isset($allowed_attributes_mutable[$key])) {
unset($allowed_attributes_mutable[$key]);
}
}
if ($delete) unset($this->info_global_attr[$attr]);
}
foreach ($this->info as $tag => $info) {
foreach ($info->attr as $attr => $x) {
$keys = array("$tag@$attr", $attr, "*@$attr", "$tag.$attr", "*.$attr");
$delete = true;
foreach ($keys as $key) {
if ($delete && isset($allowed_attributes[$key])) {
$delete = false;
}
if (isset($allowed_attributes_mutable[$key])) {
unset($allowed_attributes_mutable[$key]);
}
}
if ($delete) {
if ($this->info[$tag]->attr[$attr]->required) {
trigger_error("Required attribute '$attr' in element '$tag' was not allowed, which means '$tag' will not be allowed either", E_USER_WARNING);
}
unset($this->info[$tag]->attr[$attr]);
}
}
}
// emit errors
foreach ($allowed_attributes_mutable as $elattr => $d) {
$bits = preg_split('/[.@]/', $elattr, 2);
$c = count($bits);
switch ($c) {
case 2:
if ($bits[0] !== '*') {
$element = htmlspecialchars($bits[0]);
$attribute = htmlspecialchars($bits[1]);
if (!isset($this->info[$element])) {
trigger_error("Cannot allow attribute '$attribute' if element '$element' is not allowed/supported $support");
} else {
trigger_error("Attribute '$attribute' in element '$element' not supported $support",
E_USER_WARNING);
}
break;
}
// otherwise fall through
case 1:
$attribute = htmlspecialchars($bits[0]);
trigger_error("Global attribute '$attribute' is not ".
"supported in any elements $support",
E_USER_WARNING);
break;
}
}
}
// setup forbidden elements ---------------------------------------
$forbidden_elements = $config->get('HTML.ForbiddenElements');
$forbidden_attributes = $config->get('HTML.ForbiddenAttributes');
foreach ($this->info as $tag => $info) {
if (isset($forbidden_elements[$tag])) {
unset($this->info[$tag]);
continue;
}
foreach ($info->attr as $attr => $x) {
if (
isset($forbidden_attributes["$tag@$attr"]) ||
isset($forbidden_attributes["*@$attr"]) ||
isset($forbidden_attributes[$attr])
) {
unset($this->info[$tag]->attr[$attr]);
continue;
} // this segment might get removed eventually
elseif (isset($forbidden_attributes["$tag.$attr"])) {
// $tag.$attr are not user supplied, so no worries!
trigger_error("Error with $tag.$attr: tag.attr syntax not supported for HTML.ForbiddenAttributes; use tag@attr instead", E_USER_WARNING);
}
}
}
foreach ($forbidden_attributes as $key => $v) {
if (strlen($key) < 2) continue;
if ($key[0] != '*') continue;
if ($key[1] == '.') {
trigger_error("Error with $key: *.attr syntax not supported for HTML.ForbiddenAttributes; use attr instead", E_USER_WARNING);
}
}
// setup injectors -----------------------------------------------------
foreach ($this->info_injector as $i => $injector) {
if ($injector->checkNeeded($config) !== false) {
// remove injector that does not have it's required
// elements/attributes present, and is thus not needed.
unset($this->info_injector[$i]);
}
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Field Documentation

HTMLPurifier_HTMLDefinition::$_anonModule
private

Definition at line 150 of file HTMLDefinition.php.

Referenced by getAnonymousModule().

HTMLPurifier_HTMLDefinition::$doctype

Doctype object.

Definition at line 87 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$info = array()

Associative array of element names to HTMLPurifier_ElementDef.

Definition at line 34 of file HTMLDefinition.php.

Referenced by setupConfigStuff().

HTMLPurifier_HTMLDefinition::$info_attr_transform_post = array()

Indexed list of HTMLPurifier_AttrTransform to be performed after validation.

Definition at line 71 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$info_attr_transform_pre = array()

Indexed list of HTMLPurifier_AttrTransform to be performed before validation.

Definition at line 66 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$info_block_wrapper = 'p'

String name of element used to wrap inline elements in block context.

Note
This is rarely used except for BLOCKQUOTEs in strict mode

Definition at line 56 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$info_content_sets = array()

Nested lookup array of content set name (Block, Inline) to element name to whether or not it belongs in that content set.

Definition at line 77 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$info_global_attr = array()

Associative array of global attribute name to attribute definition.

Definition at line 39 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$info_injector = array()

Indexed list of HTMLPurifier_Injector to be used.

Definition at line 82 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$info_parent = 'div'

String name of parent element HTML will be going into.

Definition at line 44 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$info_parent_def

Definition for parent element, allows parent element to be a tag that's not allowed inside the HTML fragment.

Definition at line 50 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$info_tag_transform = array()

Associative array of deprecated tag name to HTMLPurifier_TagTransform.

Definition at line 61 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$manager

Instance of HTMLPurifier_HTMLModuleManager.

Definition at line 156 of file HTMLDefinition.php.

HTMLPurifier_HTMLDefinition::$type = 'HTML'

Definition at line 155 of file HTMLDefinition.php.

Referenced by addElement().


The documentation for this class was generated from the following file: