ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
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. More...
 
 addElement ($element_name, $type, $contents, $attr_collections, $attributes=array())
 Adds a custom element to your HTML definition. More...
 
 addBlankElement ($element_name)
 Adds a blank element to your HTML definition, for overriding existing behavior. More...
 
 getAnonymousModule ()
 Retrieves a reference to the anonymous module, so you can bust out advanced features without having to make your own module. More...
 
 __construct ()
 Performs low-cost, preliminary initialization. More...
 
 parseTinyMCEAllowedList ($list)
 Parses a TinyMCE-flavored Allowed Elements and Attributes list into separate lists for processing. More...
 
- Public Member Functions inherited from HTMLPurifier_Definition
 setup ($config)
 Setup function that aborts if already setup. More...
 

Data Fields

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

Protected Member Functions

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

Private Attributes

 $_anonModule = null
 

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

◆ __construct()

HTMLPurifier_HTMLDefinition::__construct ( )

Performs low-cost, preliminary initialization.

Definition at line 185 of file HTMLDefinition.php.

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

Member Function Documentation

◆ addAttribute()

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
string$element_nameElement name to add attribute to
string$attr_nameName of attribute
mixed$defAttribute definition, can be string or object, see HTMLPurifier_AttrTypes for details

Definition at line 113 of file HTMLDefinition.php.

References getAnonymousModule().

114  {
115  $module = $this->getAnonymousModule();
116  if (!isset($module->info[$element_name])) {
117  $element = $module->addBlankElement($element_name);
118  } else {
119  $element = $module->info[$element_name];
120  }
121  $element->attr[$attr_name] = $def;
122  }
getAnonymousModule()
Retrieves a reference to the anonymous module, so you can bust out advanced features without having t...
+ Here is the call graph for this function:

◆ addBlankElement()

HTMLPurifier_HTMLDefinition::addBlankElement (   $element_name)

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

Parameters
string$element_name
Returns
HTMLPurifier_ElementDef
See also
HTMLPurifier_HTMLModule::addBlankElement() for detailed parameter and return value descriptions.

Definition at line 146 of file HTMLDefinition.php.

References getAnonymousModule().

147  {
148  $module = $this->getAnonymousModule();
149  $element = $module->addBlankElement($element_name);
150  return $element;
151  }
getAnonymousModule()
Retrieves a reference to the anonymous module, so you can bust out advanced features without having t...
+ Here is the call graph for this function:

◆ addElement()

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

Adds a custom element to your HTML definition.

See also
HTMLPurifier_HTMLModule::addElement() for detailed parameter and return value descriptions.

Definition at line 129 of file HTMLDefinition.php.

References $contents, $type, and getAnonymousModule().

130  {
131  $module = $this->getAnonymousModule();
132  // assume that if the user is calling this, the element
133  // is safe. This may not be a good idea
134  $element = $module->addElement($element_name, $type, $contents, $attr_collections, $attributes);
135  return $element;
136  }
getAnonymousModule()
Retrieves a reference to the anonymous module, so you can bust out advanced features without having t...
+ Here is the call graph for this function:

◆ doSetup()

HTMLPurifier_HTMLDefinition::doSetup (   $config)
protected
Parameters
HTMLPurifier_Config$config

Definition at line 193 of file HTMLDefinition.php.

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

194  {
195  $this->processModules($config);
196  $this->setupConfigStuff($config);
197  unset($this->manager);
198 
199  // cleanup some of the element definitions
200  foreach ($this->info as $k => $v) {
201  unset($this->info[$k]->content_model);
202  unset($this->info[$k]->content_model_type);
203  }
204  }
processModules($config)
Extract out the information from the manager.
setupConfigStuff($config)
Sets up stuff based on config.
+ Here is the call graph for this function:

◆ getAnonymousModule()

HTMLPurifier_HTMLDefinition::getAnonymousModule ( )

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

Returns
HTMLPurifier_HTMLModule

Definition at line 159 of file HTMLDefinition.php.

References $_anonModule.

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

160  {
161  if (!$this->_anonModule) {
162  $this->_anonModule = new HTMLPurifier_HTMLModule();
163  $this->_anonModule->name = 'Anonymous';
164  }
165  return $this->_anonModule;
166  }
Represents an XHTML 1.1 module, with information on elements, tags and attributes.
Definition: HTMLModule.php:18
+ Here is the caller graph for this function:

◆ parseTinyMCEAllowedList()

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
array$listString list to parse
Returns
array
Todo:
Give this its own class, probably static interface

Definition at line 458 of file HTMLDefinition.php.

References array.

Referenced by setupConfigStuff().

459  {
460  $list = str_replace(array(' ', "\t"), '', $list);
461 
462  $elements = array();
463  $attributes = array();
464 
465  $chunks = preg_split('/(,|[\n\r]+)/', $list);
466  foreach ($chunks as $chunk) {
467  if (empty($chunk)) {
468  continue;
469  }
470  // remove TinyMCE element control characters
471  if (!strpos($chunk, '[')) {
472  $element = $chunk;
473  $attr = false;
474  } else {
475  list($element, $attr) = explode('[', $chunk);
476  }
477  if ($element !== '*') {
478  $elements[$element] = true;
479  }
480  if (!$attr) {
481  continue;
482  }
483  $attr = substr($attr, 0, strlen($attr) - 1); // remove trailing ]
484  $attr = explode('|', $attr);
485  foreach ($attr as $key) {
486  $attributes["$element.$key"] = true;
487  }
488  }
489  return array($elements, $attributes);
490  }
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ processModules()

HTMLPurifier_HTMLDefinition::processModules (   $config)
protected

Extract out the information from the manager.

Parameters
HTMLPurifier_Config$config

Definition at line 210 of file HTMLDefinition.php.

References $config.

Referenced by doSetup().

211  {
212  if ($this->_anonModule) {
213  // for user specific changes
214  // this is late-loaded so we don't have to deal with PHP4
215  // reference wonky-ness
216  $this->manager->addModule($this->_anonModule);
217  unset($this->_anonModule);
218  }
219 
220  $this->manager->setup($config);
221  $this->doctype = $this->manager->doctype;
222 
223  foreach ($this->manager->modules as $module) {
224  foreach ($module->info_tag_transform as $k => $v) {
225  if ($v === false) {
226  unset($this->info_tag_transform[$k]);
227  } else {
228  $this->info_tag_transform[$k] = $v;
229  }
230  }
231  foreach ($module->info_attr_transform_pre as $k => $v) {
232  if ($v === false) {
233  unset($this->info_attr_transform_pre[$k]);
234  } else {
235  $this->info_attr_transform_pre[$k] = $v;
236  }
237  }
238  foreach ($module->info_attr_transform_post as $k => $v) {
239  if ($v === false) {
240  unset($this->info_attr_transform_post[$k]);
241  } else {
242  $this->info_attr_transform_post[$k] = $v;
243  }
244  }
245  foreach ($module->info_injector as $k => $v) {
246  if ($v === false) {
247  unset($this->info_injector[$k]);
248  } else {
249  $this->info_injector[$k] = $v;
250  }
251  }
252  }
253  $this->info = $this->manager->getElements();
254  $this->info_content_sets = $this->manager->contentSets->lookup;
255  }
+ Here is the caller graph for this function:

◆ setupConfigStuff()

HTMLPurifier_HTMLDefinition::setupConfigStuff (   $config)
protected

Sets up stuff based on config.

We need a better way of doing this.

Parameters
HTMLPurifier_Config$config

Definition at line 261 of file HTMLDefinition.php.

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

Referenced by doSetup().

262  {
263  $block_wrapper = $config->get('HTML.BlockWrapper');
264  if (isset($this->info_content_sets['Block'][$block_wrapper])) {
265  $this->info_block_wrapper = $block_wrapper;
266  } else {
267  trigger_error(
268  'Cannot use non-block element as block wrapper',
269  E_USER_ERROR
270  );
271  }
272 
273  $parent = $config->get('HTML.Parent');
274  $def = $this->manager->getElement($parent, true);
275  if ($def) {
276  $this->info_parent = $parent;
277  $this->info_parent_def = $def;
278  } else {
279  trigger_error(
280  'Cannot use unrecognized element as parent',
281  E_USER_ERROR
282  );
283  $this->info_parent_def = $this->manager->getElement($this->info_parent, true);
284  }
285 
286  // support template text
287  $support = "(for information on implementing this, see the support forums) ";
288 
289  // setup allowed elements -----------------------------------------
290 
291  $allowed_elements = $config->get('HTML.AllowedElements');
292  $allowed_attributes = $config->get('HTML.AllowedAttributes'); // retrieve early
293 
294  if (!is_array($allowed_elements) && !is_array($allowed_attributes)) {
295  $allowed = $config->get('HTML.Allowed');
296  if (is_string($allowed)) {
297  list($allowed_elements, $allowed_attributes) = $this->parseTinyMCEAllowedList($allowed);
298  }
299  }
300 
301  if (is_array($allowed_elements)) {
302  foreach ($this->info as $name => $d) {
303  if (!isset($allowed_elements[$name])) {
304  unset($this->info[$name]);
305  }
306  unset($allowed_elements[$name]);
307  }
308  // emit errors
309  foreach ($allowed_elements as $element => $d) {
310  $element = htmlspecialchars($element); // PHP doesn't escape errors, be careful!
311  trigger_error("Element '$element' is not supported $support", E_USER_WARNING);
312  }
313  }
314 
315  // setup allowed attributes ---------------------------------------
316 
317  $allowed_attributes_mutable = $allowed_attributes; // by copy!
318  if (is_array($allowed_attributes)) {
319  // This actually doesn't do anything, since we went away from
320  // global attributes. It's possible that userland code uses
321  // it, but HTMLModuleManager doesn't!
322  foreach ($this->info_global_attr as $attr => $x) {
323  $keys = array($attr, "*@$attr", "*.$attr");
324  $delete = true;
325  foreach ($keys as $key) {
326  if ($delete && isset($allowed_attributes[$key])) {
327  $delete = false;
328  }
329  if (isset($allowed_attributes_mutable[$key])) {
330  unset($allowed_attributes_mutable[$key]);
331  }
332  }
333  if ($delete) {
334  unset($this->info_global_attr[$attr]);
335  }
336  }
337 
338  foreach ($this->info as $tag => $info) {
339  foreach ($info->attr as $attr => $x) {
340  $keys = array("$tag@$attr", $attr, "*@$attr", "$tag.$attr", "*.$attr");
341  $delete = true;
342  foreach ($keys as $key) {
343  if ($delete && isset($allowed_attributes[$key])) {
344  $delete = false;
345  }
346  if (isset($allowed_attributes_mutable[$key])) {
347  unset($allowed_attributes_mutable[$key]);
348  }
349  }
350  if ($delete) {
351  if ($this->info[$tag]->attr[$attr]->required) {
352  trigger_error(
353  "Required attribute '$attr' in element '$tag' " .
354  "was not allowed, which means '$tag' will not be allowed either",
355  E_USER_WARNING
356  );
357  }
358  unset($this->info[$tag]->attr[$attr]);
359  }
360  }
361  }
362  // emit errors
363  foreach ($allowed_attributes_mutable as $elattr => $d) {
364  $bits = preg_split('/[.@]/', $elattr, 2);
365  $c = count($bits);
366  switch ($c) {
367  case 2:
368  if ($bits[0] !== '*') {
369  $element = htmlspecialchars($bits[0]);
370  $attribute = htmlspecialchars($bits[1]);
371  if (!isset($this->info[$element])) {
372  trigger_error(
373  "Cannot allow attribute '$attribute' if element " .
374  "'$element' is not allowed/supported $support"
375  );
376  } else {
377  trigger_error(
378  "Attribute '$attribute' in element '$element' not supported $support",
379  E_USER_WARNING
380  );
381  }
382  break;
383  }
384  // otherwise fall through
385  case 1:
386  $attribute = htmlspecialchars($bits[0]);
387  trigger_error(
388  "Global attribute '$attribute' is not ".
389  "supported in any elements $support",
390  E_USER_WARNING
391  );
392  break;
393  }
394  }
395  }
396 
397  // setup forbidden elements ---------------------------------------
398 
399  $forbidden_elements = $config->get('HTML.ForbiddenElements');
400  $forbidden_attributes = $config->get('HTML.ForbiddenAttributes');
401 
402  foreach ($this->info as $tag => $info) {
403  if (isset($forbidden_elements[$tag])) {
404  unset($this->info[$tag]);
405  continue;
406  }
407  foreach ($info->attr as $attr => $x) {
408  if (isset($forbidden_attributes["$tag@$attr"]) ||
409  isset($forbidden_attributes["*@$attr"]) ||
410  isset($forbidden_attributes[$attr])
411  ) {
412  unset($this->info[$tag]->attr[$attr]);
413  continue;
414  } elseif (isset($forbidden_attributes["$tag.$attr"])) { // this segment might get removed eventually
415  // $tag.$attr are not user supplied, so no worries!
416  trigger_error(
417  "Error with $tag.$attr: tag.attr syntax not supported for " .
418  "HTML.ForbiddenAttributes; use tag@attr instead",
419  E_USER_WARNING
420  );
421  }
422  }
423  }
424  foreach ($forbidden_attributes as $key => $v) {
425  if (strlen($key) < 2) {
426  continue;
427  }
428  if ($key[0] != '*') {
429  continue;
430  }
431  if ($key[1] == '.') {
432  trigger_error(
433  "Error with $key: *.attr syntax not supported for HTML.ForbiddenAttributes; use attr instead",
434  E_USER_WARNING
435  );
436  }
437  }
438 
439  // setup injectors -----------------------------------------------------
440  foreach ($this->info_injector as $i => $injector) {
441  if ($injector->checkNeeded($config) !== false) {
442  // remove injector that does not have it's required
443  // elements/attributes present, and is thus not needed.
444  unset($this->info_injector[$i]);
445  }
446  }
447  }
$x
Definition: example_009.php:98
for($col=0; $col< 50; $col++) $d
$info
Associative array of element names to HTMLPurifier_ElementDef.
Create styles array
The data for the language used.
parseTinyMCEAllowedList($list)
Parses a TinyMCE-flavored Allowed Elements and Attributes list into separate lists for processing...
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $_anonModule

HTMLPurifier_HTMLDefinition::$_anonModule = null
private

Definition at line 168 of file HTMLDefinition.php.

Referenced by getAnonymousModule().

◆ $doctype

HTMLPurifier_HTMLDefinition::$doctype

Doctype object HTMLPurifier_Doctype.

Definition at line 98 of file HTMLDefinition.php.

◆ $info

HTMLPurifier_HTMLDefinition::$info = array()

Associative array of element names to HTMLPurifier_ElementDef.

HTMLPurifier_ElementDef[]

Definition at line 35 of file HTMLDefinition.php.

Referenced by setupConfigStuff().

◆ $info_attr_transform_post

HTMLPurifier_HTMLDefinition::$info_attr_transform_post = array()

Indexed list of HTMLPurifier_AttrTransform to be performed after validation.

HTMLPurifier_AttrTransform[]

Definition at line 79 of file HTMLDefinition.php.

◆ $info_attr_transform_pre

HTMLPurifier_HTMLDefinition::$info_attr_transform_pre = array()

Indexed list of HTMLPurifier_AttrTransform to be performed before validation.

HTMLPurifier_AttrTransform[]

Definition at line 73 of file HTMLDefinition.php.

◆ $info_block_wrapper

HTMLPurifier_HTMLDefinition::$info_block_wrapper = 'p'

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

string

Note
This is rarely used except for BLOCKQUOTEs in strict mode

Definition at line 61 of file HTMLDefinition.php.

◆ $info_content_sets

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.

array

Definition at line 86 of file HTMLDefinition.php.

◆ $info_global_attr

HTMLPurifier_HTMLDefinition::$info_global_attr = array()

Associative array of global attribute name to attribute definition.

array

Definition at line 41 of file HTMLDefinition.php.

◆ $info_injector

HTMLPurifier_HTMLDefinition::$info_injector = array()

Indexed list of HTMLPurifier_Injector to be used.

HTMLPurifier_Injector[]

Definition at line 92 of file HTMLDefinition.php.

◆ $info_parent

HTMLPurifier_HTMLDefinition::$info_parent = 'div'

String name of parent element HTML will be going into.

string

Definition at line 47 of file HTMLDefinition.php.

◆ $info_parent_def

HTMLPurifier_HTMLDefinition::$info_parent_def

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

HTMLPurifier_ElementDef

Definition at line 54 of file HTMLDefinition.php.

◆ $info_tag_transform

HTMLPurifier_HTMLDefinition::$info_tag_transform = array()

Associative array of deprecated tag name to HTMLPurifier_TagTransform.

array

Definition at line 67 of file HTMLDefinition.php.

◆ $manager

HTMLPurifier_HTMLDefinition::$manager

◆ $type

HTMLPurifier_HTMLDefinition::$type = 'HTML'

string

Definition at line 175 of file HTMLDefinition.php.

Referenced by addElement().


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