MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / lib / htmlpurifier / HTMLPurifier / Injector.php
blob59017163877a5f8dbc14411d0c0cb2a4a7279bd2
1 <?php
3 /**
4 * Injects tokens into the document while parsing for well-formedness.
5 * This enables "formatter-like" functionality such as auto-paragraphing,
6 * smiley-ification and linkification to take place.
7 */
8 class HTMLPurifier_Injector
11 /**
12 * Advisory name of injector, this is for friendly error messages
14 var $name;
16 /**
17 * Amount of tokens the injector needs to skip + 1. Because
18 * the decrement is the first thing that happens, this needs to
19 * be one greater than the "real" skip count.
21 var $skip = 1;
23 /**
24 * Instance of HTMLPurifier_HTMLDefinition
26 var $htmlDefinition;
28 /**
29 * Reference to CurrentNesting variable in Context. This is an array
30 * list of tokens that we are currently "inside"
32 var $currentNesting;
34 /**
35 * Reference to InputTokens variable in Context. This is an array
36 * list of the input tokens that are being processed.
38 var $inputTokens;
40 /**
41 * Reference to InputIndex variable in Context. This is an integer
42 * array index for $this->inputTokens that indicates what token
43 * is currently being processed.
45 var $inputIndex;
47 /**
48 * Array of elements and attributes this injector creates and therefore
49 * need to be allowed by the definition. Takes form of
50 * array('element' => array('attr', 'attr2'), 'element2')
52 var $needed = array();
54 /**
55 * Prepares the injector by giving it the config and context objects:
56 * this allows references to important variables to be made within
57 * the injector. This function also checks if the HTML environment
58 * will work with the Injector: if p tags are not allowed, the
59 * Auto-Paragraphing injector should not be enabled.
60 * @param $config Instance of HTMLPurifier_Config
61 * @param $context Instance of HTMLPurifier_Context
62 * @return Boolean false if success, string of missing needed element/attribute if failure
64 function prepare($config, &$context) {
65 $this->htmlDefinition = $config->getHTMLDefinition();
66 // perform $needed checks
67 foreach ($this->needed as $element => $attributes) {
68 if (is_int($element)) $element = $attributes;
69 if (!isset($this->htmlDefinition->info[$element])) return $element;
70 if (!is_array($attributes)) continue;
71 foreach ($attributes as $name) {
72 if (!isset($this->htmlDefinition->info[$element]->attr[$name])) return "$element.$name";
75 $this->currentNesting =& $context->get('CurrentNesting');
76 $this->inputTokens =& $context->get('InputTokens');
77 $this->inputIndex =& $context->get('InputIndex');
78 return false;
81 /**
82 * Tests if the context node allows a certain element
83 * @param $name Name of element to test for
84 * @return True if element is allowed, false if it is not
86 function allowsElement($name) {
87 if (!empty($this->currentNesting)) {
88 $parent_token = array_pop($this->currentNesting);
89 $this->currentNesting[] = $parent_token;
90 $parent = $this->htmlDefinition->info[$parent_token->name];
91 } else {
92 $parent = $this->htmlDefinition->info_parent_def;
94 if (!isset($parent->child->elements[$name]) || isset($parent->excludes[$name])) {
95 return false;
97 return true;
101 * Handler that is called when a text token is processed
103 function handleText(&$token) {}
106 * Handler that is called when a start or empty token is processed
108 function handleElement(&$token) {}