Source for file HTMLDefinition.php
Documentation is available at HTMLDefinition.php
* Definition of the purified HTML that describes allowed children,
* attributes, and many other things.
* 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
* 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!
// FULLY-PUBLIC VARIABLES ---------------------------------------------
* Associative array of element names to HTMLPurifier_ElementDef
* Associative array of global attribute name to attribute definition.
* String name of parent element HTML will be going into.
* Definition for parent element, allows parent element to be a
* tag that's not allowed inside the HTML fragment.
* String name of element used to wrap inline elements in block context
* @note This is rarely used except for BLOCKQUOTEs in strict mode
* Associative array of deprecated tag name to HTMLPurifier_TagTransform
* Indexed list of HTMLPurifier_AttrTransform to be performed before validation.
* Indexed list of HTMLPurifier_AttrTransform to be performed after validation.
* Nested lookup array of content set name (Block, Inline) to
* element name to whether or not it belongs in that content set.
* Indexed list of HTMLPurifier_Injector to be used.
// RAW CUSTOMIZATION STUFF --------------------------------------------
* Adds a custom attribute to a pre-existing element
* @note This is strictly convenience, and does not have a corresponding
* method in HTMLPurifier_HTMLModule
* @param $element_name String element name to add attribute to
* @param $attr_name String name of attribute
* @param $def Attribute definition, can be string or object, see
* HTMLPurifier_AttrTypes for details
public function addAttribute($element_name, $attr_name, $def) {
if (!isset
($module->info[$element_name])) {
$element =
$module->addBlankElement($element_name);
$element =
$module->info[$element_name];
$element->attr[$attr_name] =
$def;
* Adds a custom element to your HTML definition
* @note See HTMLPurifier_HTMLModule::addElement for detailed
* parameter and return value descriptions.
public function addElement($element_name, $type, $contents, $attr_collections, $attributes) {
// 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);
* Adds a blank element to your HTML definition, for overriding
* @note See HTMLPurifier_HTMLModule::addBlankElement for detailed
* parameter and return value descriptions.
$element =
$module->addBlankElement($element_name);
* Retrieves a reference to the anonymous module, so you can
* bust out advanced features without having to make your own
if (!$this->_anonModule) {
$this->_anonModule->name =
'Anonymous';
return $this->_anonModule;
// PUBLIC BUT INTERNAL VARIABLES --------------------------------------
public $manager; /**< Instance of HTMLPurifier_HTMLModuleManager */
* Performs low-cost, preliminary initialization.
protected function doSetup($config) {
// 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);
* Extract out the information from the manager
if ($this->_anonModule) {
// for user specific changes
// this is late-loaded so we don't have to deal with PHP4
$this->manager->addModule($this->_anonModule);
unset
($this->_anonModule);
foreach ($this->manager->modules as $module) {
foreach($module->info_tag_transform as $k =>
$v) {
foreach($module->info_attr_transform_pre as $k =>
$v) {
foreach($module->info_attr_transform_post as $k =>
$v) {
foreach ($module->info_injector as $k =>
$v) {
* Sets up stuff based on config. We need a better way of doing this.
$block_wrapper =
$config->get('HTML', 'BlockWrapper');
$parent =
$config->get('HTML', 'Parent');
$def =
$this->manager->getElement($parent, true);
$support =
"(for information on implementing this, see the ".
// setup allowed elements -----------------------------------------
$allowed_elements =
$config->get('HTML', 'AllowedElements');
$allowed_attributes =
$config->get('HTML', 'AllowedAttributes'); // retrieve early
$allowed =
$config->get('HTML', 'Allowed');
foreach ($this->info as $name =>
$d) {
if(!isset
($allowed_elements[$name])) unset
($this->info[$name]);
unset
($allowed_elements[$name]);
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!
// 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!
$keys =
array($attr, "*@$attr", "*.$attr");
foreach ($keys as $key) {
if ($delete && isset
($allowed_attributes[$key])) {
if (isset
($allowed_attributes_mutable[$key])) {
unset
($allowed_attributes_mutable[$key]);
foreach ($this->info as $tag =>
$info) {
foreach ($info->attr as $attr =>
$x) {
$keys =
array("$tag@$attr", $attr, "*@$attr", "$tag.$attr", "*.$attr");
foreach ($keys as $key) {
if ($delete && isset
($allowed_attributes[$key])) {
if (isset
($allowed_attributes_mutable[$key])) {
unset
($allowed_attributes_mutable[$key]);
if ($delete) unset
($this->info[$tag]->attr[$attr]);
foreach ($allowed_attributes_mutable as $elattr =>
$d) {
if (!isset
($this->info[$element])) {
trigger_error("Cannot allow attribute '$attribute' if element '$element' is not allowed/supported $support");
trigger_error("Attribute '$attribute' in element '$element' not supported $support",
// otherwise fall through
"supported in any elements $support",
// 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]);
foreach ($info->attr as $attr =>
$x) {
isset
($forbidden_attributes["$tag@$attr"]) ||
isset
($forbidden_attributes["*@$attr"]) ||
isset
($forbidden_attributes[$attr])
unset
($this->info[$tag]->attr[$attr]);
} // 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;
trigger_error("Error with $key: *.attr syntax not supported for HTML.ForbiddenAttributes; use attr instead", E_USER_WARNING);
// setup injectors -----------------------------------------------------
if ($injector->checkNeeded($config) !==
false) {
// remove injector that does not have it's required
// elements/attributes present, and is thus not needed.
* 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
* @param $list String list to parse
* @param array($allowed_elements, $allowed_attributes)
* @todo Give this its own class, probably static interface
foreach ($chunks as $chunk) {
if (empty($chunk)) continue;
// remove TinyMCE element control characters
list
($element, $attr) =
explode('[', $chunk);
if ($element !==
'*') $elements[$element] =
true;
$attr =
substr($attr, 0, strlen($attr) -
1); // remove trailing ]
foreach ($attr as $key) {
$attributes["$element.$key"] =
true;
return array($elements, $attributes);
Documentation generated on Thu, 19 Jun 2008 18:49:29 -0400 by phpDocumentor 1.4.2