Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / Strategy / FixNesting.php
blob08f907562f4c9b4f51ed47c85a9380a794505455
1 <?php
3 require_once 'HTMLPurifier/Strategy.php';
4 require_once 'HTMLPurifier/HTMLDefinition.php';
6 /**
7 * Takes a well formed list of tokens and fixes their nesting.
8 *
9 * HTML elements dictate which elements are allowed to be their children,
10 * for example, you can't have a p tag in a span tag. Other elements have
11 * much more rigorous definitions: tables, for instance, require a specific
12 * order for their elements. There are also constraints not expressible by
13 * document type definitions, such as the chameleon nature of ins/del
14 * tags and global child exclusions.
16 * The first major objective of this strategy is to iterate through all the
17 * nodes (not tokens) of the list of tokens and determine whether or not
18 * their children conform to the element's definition. If they do not, the
19 * child definition may optionally supply an amended list of elements that
20 * is valid or require that the entire node be deleted (and the previous
21 * node rescanned).
23 * The second objective is to ensure that explicitly excluded elements of
24 * an element do not appear in its children. Code that accomplishes this
25 * task is pervasive through the strategy, though the two are distinct tasks
26 * and could, theoretically, be seperated (although it's not recommended).
28 * @note Whether or not unrecognized children are silently dropped or
29 * translated into text depends on the child definitions.
31 * @todo Enable nodes to be bubbled out of the structure.
34 class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
37 function execute($tokens, $config, &$context) {
38 //####################################################################//
39 // Pre-processing
41 // get a copy of the HTML definition
42 $definition = $config->getHTMLDefinition();
44 // insert implicit "parent" node, will be removed at end.
45 // ! we might want to move this to configuration
46 // DEFINITION CALL
47 $parent_name = $definition->info_parent;
48 array_unshift($tokens, new HTMLPurifier_Token_Start($parent_name));
49 $tokens[] = new HTMLPurifier_Token_End($parent_name);
51 // setup the context variables
52 $is_inline = false; // reference var that we alter
53 $context->register('IsInline', $is_inline);
55 //####################################################################//
56 // Loop initialization
58 // stack that contains the indexes of all parents,
59 // $stack[count($stack)-1] being the current parent
60 $stack = array();
62 // stack that contains all elements that are excluded
63 // same structure as $stack, but it is only populated when an element
64 // with exclusions is processed, i.e. there won't be empty exclusions.
65 $exclude_stack = array();
67 //####################################################################//
68 // Loop
70 // iterate through all start nodes. Determining the start node
71 // is complicated so it has been omitted from the loop construct
72 for ($i = 0, $size = count($tokens) ; $i < $size; ) {
74 //################################################################//
75 // Gather information on children
77 // child token accumulator
78 $child_tokens = array();
80 // scroll to the end of this node, report number, and collect
81 // all children
82 for ($j = $i, $depth = 0; ; $j++) {
83 if ($tokens[$j]->type == 'start') {
84 $depth++;
85 // skip token assignment on first iteration, this is the
86 // token we currently are on
87 if ($depth == 1) continue;
88 } elseif ($tokens[$j]->type == 'end') {
89 $depth--;
90 // skip token assignment on last iteration, this is the
91 // end token of the token we're currently on
92 if ($depth == 0) break;
94 $child_tokens[] = $tokens[$j];
97 // $i is index of start token
98 // $j is index of end token
100 //################################################################//
101 // Gather information on parent
103 // calculate parent information
104 if ($count = count($stack)) {
105 $parent_index = $stack[$count-1];
106 $parent_name = $tokens[$parent_index]->name;
107 if ($parent_index == 0) {
108 $parent_def = $definition->info_parent_def;
109 } else {
110 $parent_def = $definition->info[$parent_name];
112 } else {
113 // unknown info, it won't be used anyway
114 $parent_index = $parent_name = $parent_def = null;
117 // calculate context
118 if ($is_inline === false) {
119 // check if conditions make it inline
120 if (!empty($parent_def) && $parent_def->descendants_are_inline) {
121 $is_inline = $count - 1;
123 } else {
124 // check if we're out of inline
125 if ($count === $is_inline) {
126 $is_inline = false;
130 //################################################################//
131 // Determine whether element is explicitly excluded SGML-style
133 // determine whether or not element is excluded by checking all
134 // parent exclusions. The array should not be very large, two
135 // elements at most.
136 $excluded = false;
137 if (!empty($exclude_stack)) {
138 foreach ($exclude_stack as $lookup) {
139 if (isset($lookup[$tokens[$i]->name])) {
140 $excluded = true;
141 // no need to continue processing
142 break;
147 //################################################################//
148 // Perform child validation
150 if ($excluded) {
151 // there is an exclusion, remove the entire node
152 $result = false;
153 $excludes = array(); // not used, but good to initialize anyway
154 } else {
155 // DEFINITION CALL
156 if ($i === 0) {
157 // special processing for the first node
158 $def = $definition->info_parent_def;
159 } else {
160 $def = $definition->info[$tokens[$i]->name];
164 if (!empty($def->child)) {
165 // have DTD child def validate children
166 $result = $def->child->validateChildren(
167 $child_tokens, $config, $context);
168 } else {
169 // weird, no child definition, get rid of everything
170 $result = false;
173 // determine whether or not this element has any exclusions
174 $excludes = $def->excludes;
177 // $result is now a bool or array
179 //################################################################//
180 // Process result by interpreting $result
182 if ($result === true) {
183 // leave the node as is
185 // register start token as a parental node start
186 $stack[] = $i;
188 // register exclusions if there are any
189 if (!empty($excludes)) $exclude_stack[] = $excludes;
191 // move cursor to next possible start node
192 $i++;
194 } elseif($result === false) {
195 // remove entire node
197 // calculate length of inner tokens and current tokens
198 $length = $j - $i + 1;
200 // perform removal
201 array_splice($tokens, $i, $length);
203 // update size
204 $size -= $length;
206 // there is no start token to register,
207 // current node is now the next possible start node
208 // unless it turns out that we need to do a double-check
210 if (!$parent_def->child->allow_empty) {
211 // we need to do a double-check
212 $i = $parent_index;
213 array_pop($stack);
216 // PROJECTED OPTIMIZATION: Process all children elements before
217 // reprocessing parent node.
219 } else {
220 // replace node with $result
222 // calculate length of inner tokens
223 $length = $j - $i - 1;
225 // perform replacement
226 array_splice($tokens, $i + 1, $length, $result);
228 // update size
229 $size -= $length;
230 $size += count($result);
232 // register start token as a parental node start
233 $stack[] = $i;
235 // register exclusions if there are any
236 if (!empty($excludes)) $exclude_stack[] = $excludes;
238 // move cursor to next possible start node
239 $i++;
243 //################################################################//
244 // Scroll to next start node
246 // We assume, at this point, that $i is the index of the token
247 // that is the first possible new start point for a node.
249 // Test if the token indeed is a start tag, if not, move forward
250 // and test again.
251 $size = count($tokens);
252 while ($i < $size and $tokens[$i]->type != 'start') {
253 if ($tokens[$i]->type == 'end') {
254 // pop a token index off the stack if we ended a node
255 array_pop($stack);
256 // pop an exclusion lookup off exclusion stack if
257 // we ended node and that node had exclusions
258 if ($i == 0 || $i == $size - 1) {
259 // use specialized var if it's the super-parent
260 $s_excludes = $definition->info_parent_def->excludes;
261 } else {
262 $s_excludes = $definition->info[$tokens[$i]->name]->excludes;
264 if ($s_excludes) {
265 array_pop($exclude_stack);
268 $i++;
273 //####################################################################//
274 // Post-processing
276 // remove implicit parent tokens at the beginning and end
277 array_shift($tokens);
278 array_pop($tokens);
280 // remove context variables
281 $context->destroy('IsInline');
283 //####################################################################//
284 // Return
286 return $tokens;