PSR-2 reformatting PHPDoc corrections
[htmlpurifier.git] / library / HTMLPurifier / Strategy / MakeWellFormed.php
blob899d675086c900a849385376e0da5e260ee24b91
1 <?php
3 /**
4 * Takes tokens makes them well-formed (balance end tags, etc.)
6 * Specification of the armor attributes this strategy uses:
8 * - MakeWellFormed_TagClosedError: This armor field is used to
9 * suppress tag closed errors for certain tokens [TagClosedSuppress],
10 * in particular, if a tag was generated automatically by HTML
11 * Purifier, we may rely on our infrastructure to close it for us
12 * and shouldn't report an error to the user [TagClosedAuto].
14 class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
17 /**
18 * Array stream of tokens being processed.
19 * @type HTMLPurifier_Token[]
21 protected $tokens;
23 /**
24 * Current index in $tokens.
25 * @type int
27 protected $t;
29 /**
30 * Current nesting of elements.
31 * @type array
33 protected $stack;
35 /**
36 * Injectors active in this stream processing.
37 * @type HTMLPurifier_Injector[]
39 protected $injectors;
41 /**
42 * Current instance of HTMLPurifier_Config.
43 * @type HTMLPurifier_Config
45 protected $config;
47 /**
48 * Current instance of HTMLPurifier_Context.
49 * @type HTMLPurifier_Context
51 protected $context;
53 /**
54 * @param HTMLPurifier_Token[] $tokens
55 * @param HTMLPurifier_Config $config
56 * @param HTMLPurifier_Context $context
57 * @return HTMLPurifier_Token[]
58 * @throws HTMLPurifier_Exception
60 public function execute($tokens, $config, $context)
62 $definition = $config->getHTMLDefinition();
64 // local variables
65 $generator = new HTMLPurifier_Generator($config, $context);
66 $escape_invalid_tags = $config->get('Core.EscapeInvalidTags');
67 // used for autoclose early abortion
68 $global_parent_allowed_elements = $definition->info_parent_def->child->getAllowedElements($config);
69 $e = $context->get('ErrorCollector', true);
70 $t = false; // token index
71 $i = false; // injector index
72 $token = false; // the current token
73 $reprocess = false; // whether or not to reprocess the same token
74 $stack = array();
76 // member variables
77 $this->stack =& $stack;
78 $this->t =& $t;
79 $this->tokens =& $tokens;
80 $this->config = $config;
81 $this->context = $context;
83 // context variables
84 $context->register('CurrentNesting', $stack);
85 $context->register('InputIndex', $t);
86 $context->register('InputTokens', $tokens);
87 $context->register('CurrentToken', $token);
89 // -- begin INJECTOR --
91 $this->injectors = array();
93 $injectors = $config->getBatch('AutoFormat');
94 $def_injectors = $definition->info_injector;
95 $custom_injectors = $injectors['Custom'];
96 unset($injectors['Custom']); // special case
97 foreach ($injectors as $injector => $b) {
98 // XXX: Fix with a legitimate lookup table of enabled filters
99 if (strpos($injector, '.') !== false) {
100 continue;
102 $injector = "HTMLPurifier_Injector_$injector";
103 if (!$b) {
104 continue;
106 $this->injectors[] = new $injector;
108 foreach ($def_injectors as $injector) {
109 // assumed to be objects
110 $this->injectors[] = $injector;
112 foreach ($custom_injectors as $injector) {
113 if (!$injector) {
114 continue;
116 if (is_string($injector)) {
117 $injector = "HTMLPurifier_Injector_$injector";
118 $injector = new $injector;
120 $this->injectors[] = $injector;
123 // give the injectors references to the definition and context
124 // variables for performance reasons
125 foreach ($this->injectors as $ix => $injector) {
126 $error = $injector->prepare($config, $context);
127 if (!$error) {
128 continue;
130 array_splice($this->injectors, $ix, 1); // rm the injector
131 trigger_error("Cannot enable {$injector->name} injector because $error is not allowed", E_USER_WARNING);
134 // -- end INJECTOR --
136 // a note on reprocessing:
137 // In order to reduce code duplication, whenever some code needs
138 // to make HTML changes in order to make things "correct", the
139 // new HTML gets sent through the purifier, regardless of its
140 // status. This means that if we add a start token, because it
141 // was totally necessary, we don't have to update nesting; we just
142 // punt ($reprocess = true; continue;) and it does that for us.
144 // isset is in loop because $tokens size changes during loop exec
145 for ($t = 0;
146 $t == 0 || isset($tokens[$t - 1]);
147 // only increment if we don't need to reprocess
148 $reprocess ? $reprocess = false : $t++) {
150 // check for a rewind
151 if (is_int($i) && $i >= 0) {
152 // possibility: disable rewinding if the current token has a
153 // rewind set on it already. This would offer protection from
154 // infinite loop, but might hinder some advanced rewinding.
155 $rewind_to = $this->injectors[$i]->getRewind();
156 if (is_int($rewind_to) && $rewind_to < $t) {
157 if ($rewind_to < 0) {
158 $rewind_to = 0;
160 while ($t > $rewind_to) {
161 $t--;
162 $prev = $tokens[$t];
163 // indicate that other injectors should not process this token,
164 // but we need to reprocess it
165 unset($prev->skip[$i]);
166 $prev->rewind = $i;
167 if ($prev instanceof HTMLPurifier_Token_Start) {
168 array_pop($this->stack);
169 } elseif ($prev instanceof HTMLPurifier_Token_End) {
170 $this->stack[] = $prev->start;
174 $i = false;
177 // handle case of document end
178 if (!isset($tokens[$t])) {
179 // kill processing if stack is empty
180 if (empty($this->stack)) {
181 break;
184 // peek
185 $top_nesting = array_pop($this->stack);
186 $this->stack[] = $top_nesting;
188 // send error [TagClosedSuppress]
189 if ($e && !isset($top_nesting->armor['MakeWellFormed_TagClosedError'])) {
190 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by document end', $top_nesting);
193 // append, don't splice, since this is the end
194 $tokens[] = new HTMLPurifier_Token_End($top_nesting->name);
196 // punt!
197 $reprocess = true;
198 continue;
201 $token = $tokens[$t];
203 //echo '<br>'; printTokens($tokens, $t); printTokens($this->stack);
204 //flush();
206 // quick-check: if it's not a tag, no need to process
207 if (empty($token->is_tag)) {
208 if ($token instanceof HTMLPurifier_Token_Text) {
209 foreach ($this->injectors as $i => $injector) {
210 if (isset($token->skip[$i])) {
211 continue;
213 if ($token->rewind !== null && $token->rewind !== $i) {
214 continue;
216 $injector->handleText($token);
217 $this->processToken($token, $i);
218 $reprocess = true;
219 break;
222 // another possibility is a comment
223 continue;
226 if (isset($definition->info[$token->name])) {
227 $type = $definition->info[$token->name]->child->type;
228 } else {
229 $type = false; // Type is unknown, treat accordingly
232 // quick tag checks: anything that's *not* an end tag
233 $ok = false;
234 if ($type === 'empty' && $token instanceof HTMLPurifier_Token_Start) {
235 // claims to be a start tag but is empty
236 $token = new HTMLPurifier_Token_Empty(
237 $token->name,
238 $token->attr,
239 $token->line,
240 $token->col,
241 $token->armor
243 $ok = true;
244 } elseif ($type && $type !== 'empty' && $token instanceof HTMLPurifier_Token_Empty) {
245 // claims to be empty but really is a start tag
246 $this->swap(new HTMLPurifier_Token_End($token->name));
247 $this->insertBefore(
248 new HTMLPurifier_Token_Start($token->name, $token->attr, $token->line, $token->col, $token->armor)
250 // punt (since we had to modify the input stream in a non-trivial way)
251 $reprocess = true;
252 continue;
253 } elseif ($token instanceof HTMLPurifier_Token_Empty) {
254 // real empty token
255 $ok = true;
256 } elseif ($token instanceof HTMLPurifier_Token_Start) {
257 // start tag
259 // ...unless they also have to close their parent
260 if (!empty($this->stack)) {
262 // Performance note: you might think that it's rather
263 // inefficient, recalculating the autoclose information
264 // for every tag that a token closes (since when we
265 // do an autoclose, we push a new token into the
266 // stream and then /process/ that, before
267 // re-processing this token.) But this is
268 // necessary, because an injector can make an
269 // arbitrary transformations to the autoclosing
270 // tokens we introduce, so things may have changed
271 // in the meantime. Also, doing the inefficient thing is
272 // "easy" to reason about (for certain perverse definitions
273 // of "easy")
275 $parent = array_pop($this->stack);
276 $this->stack[] = $parent;
278 $parent_def = null;
279 $parent_elements = null;
280 $autoclose = false;
281 if (isset($definition->info[$parent->name])) {
282 $parent_def = $definition->info[$parent->name];
283 $parent_elements = $parent_def->child->getAllowedElements($config);
284 $autoclose = !isset($parent_elements[$token->name]);
287 if ($autoclose && $definition->info[$token->name]->wrap) {
288 // Check if an element can be wrapped by another
289 // element to make it valid in a context (for
290 // example, <ul><ul> needs a <li> in between)
291 $wrapname = $definition->info[$token->name]->wrap;
292 $wrapdef = $definition->info[$wrapname];
293 $elements = $wrapdef->child->getAllowedElements($config);
294 if (isset($elements[$token->name]) && isset($parent_elements[$wrapname])) {
295 $newtoken = new HTMLPurifier_Token_Start($wrapname);
296 $this->insertBefore($newtoken);
297 $reprocess = true;
298 continue;
302 $carryover = false;
303 if ($autoclose && $parent_def->formatting) {
304 $carryover = true;
307 if ($autoclose) {
308 // check if this autoclose is doomed to fail
309 // (this rechecks $parent, which his harmless)
310 $autoclose_ok = isset($global_parent_allowed_elements[$token->name]);
311 if (!$autoclose_ok) {
312 foreach ($this->stack as $ancestor) {
313 $elements = $definition->info[$ancestor->name]->child->getAllowedElements($config);
314 if (isset($elements[$token->name])) {
315 $autoclose_ok = true;
316 break;
318 if ($definition->info[$token->name]->wrap) {
319 $wrapname = $definition->info[$token->name]->wrap;
320 $wrapdef = $definition->info[$wrapname];
321 $wrap_elements = $wrapdef->child->getAllowedElements($config);
322 if (isset($wrap_elements[$token->name]) && isset($elements[$wrapname])) {
323 $autoclose_ok = true;
324 break;
329 if ($autoclose_ok) {
330 // errors need to be updated
331 $new_token = new HTMLPurifier_Token_End($parent->name);
332 $new_token->start = $parent;
333 if ($carryover) {
334 $element = clone $parent;
335 // [TagClosedAuto]
336 $element->armor['MakeWellFormed_TagClosedError'] = true;
337 $element->carryover = true;
338 $this->processToken(array($new_token, $token, $element));
339 } else {
340 $this->insertBefore($new_token);
342 // [TagClosedSuppress]
343 if ($e && !isset($parent->armor['MakeWellFormed_TagClosedError'])) {
344 if (!$carryover) {
345 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag auto closed', $parent);
346 } else {
347 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag carryover', $parent);
350 } else {
351 $this->remove();
353 $reprocess = true;
354 continue;
358 $ok = true;
361 if ($ok) {
362 foreach ($this->injectors as $i => $injector) {
363 if (isset($token->skip[$i])) {
364 continue;
366 if ($token->rewind !== null && $token->rewind !== $i) {
367 continue;
369 $injector->handleElement($token);
370 $this->processToken($token, $i);
371 $reprocess = true;
372 break;
374 if (!$reprocess) {
375 // ah, nothing interesting happened; do normal processing
376 $this->swap($token);
377 if ($token instanceof HTMLPurifier_Token_Start) {
378 $this->stack[] = $token;
379 } elseif ($token instanceof HTMLPurifier_Token_End) {
380 throw new HTMLPurifier_Exception(
381 'Improper handling of end tag in start code; possible error in MakeWellFormed'
385 continue;
388 // sanity check: we should be dealing with a closing tag
389 if (!$token instanceof HTMLPurifier_Token_End) {
390 throw new HTMLPurifier_Exception('Unaccounted for tag token in input stream, bug in HTML Purifier');
393 // make sure that we have something open
394 if (empty($this->stack)) {
395 if ($escape_invalid_tags) {
396 if ($e) {
397 $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag to text');
399 $this->swap(
400 new HTMLPurifier_Token_Text(
401 $generator->generateFromToken($token)
404 } else {
405 $this->remove();
406 if ($e) {
407 $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag removed');
410 $reprocess = true;
411 continue;
414 // first, check for the simplest case: everything closes neatly.
415 // Eventually, everything passes through here; if there are problems
416 // we modify the input stream accordingly and then punt, so that
417 // the tokens get processed again.
418 $current_parent = array_pop($this->stack);
419 if ($current_parent->name == $token->name) {
420 $token->start = $current_parent;
421 foreach ($this->injectors as $i => $injector) {
422 if (isset($token->skip[$i])) {
423 continue;
425 if ($token->rewind !== null && $token->rewind !== $i) {
426 continue;
428 $injector->handleEnd($token);
429 $this->processToken($token, $i);
430 $this->stack[] = $current_parent;
431 $reprocess = true;
432 break;
434 continue;
437 // okay, so we're trying to close the wrong tag
439 // undo the pop previous pop
440 $this->stack[] = $current_parent;
442 // scroll back the entire nest, trying to find our tag.
443 // (feature could be to specify how far you'd like to go)
444 $size = count($this->stack);
445 // -2 because -1 is the last element, but we already checked that
446 $skipped_tags = false;
447 for ($j = $size - 2; $j >= 0; $j--) {
448 if ($this->stack[$j]->name == $token->name) {
449 $skipped_tags = array_slice($this->stack, $j);
450 break;
454 // we didn't find the tag, so remove
455 if ($skipped_tags === false) {
456 if ($escape_invalid_tags) {
457 $this->swap(
458 new HTMLPurifier_Token_Text(
459 $generator->generateFromToken($token)
462 if ($e) {
463 $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag to text');
465 } else {
466 $this->remove();
467 if ($e) {
468 $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag removed');
471 $reprocess = true;
472 continue;
475 // do errors, in REVERSE $j order: a,b,c with </a></b></c>
476 $c = count($skipped_tags);
477 if ($e) {
478 for ($j = $c - 1; $j > 0; $j--) {
479 // notice we exclude $j == 0, i.e. the current ending tag, from
480 // the errors... [TagClosedSuppress]
481 if (!isset($skipped_tags[$j]->armor['MakeWellFormed_TagClosedError'])) {
482 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by element end', $skipped_tags[$j]);
487 // insert tags, in FORWARD $j order: c,b,a with </a></b></c>
488 $replace = array($token);
489 for ($j = 1; $j < $c; $j++) {
490 // ...as well as from the insertions
491 $new_token = new HTMLPurifier_Token_End($skipped_tags[$j]->name);
492 $new_token->start = $skipped_tags[$j];
493 array_unshift($replace, $new_token);
494 if (isset($definition->info[$new_token->name]) && $definition->info[$new_token->name]->formatting) {
495 // [TagClosedAuto]
496 $element = clone $skipped_tags[$j];
497 $element->carryover = true;
498 $element->armor['MakeWellFormed_TagClosedError'] = true;
499 $replace[] = $element;
502 $this->processToken($replace);
503 $reprocess = true;
504 continue;
507 $context->destroy('CurrentNesting');
508 $context->destroy('InputTokens');
509 $context->destroy('InputIndex');
510 $context->destroy('CurrentToken');
512 unset($this->injectors, $this->stack, $this->tokens, $this->t);
513 return $tokens;
517 * Processes arbitrary token values for complicated substitution patterns.
518 * In general:
520 * If $token is an array, it is a list of tokens to substitute for the
521 * current token. These tokens then get individually processed. If there
522 * is a leading integer in the list, that integer determines how many
523 * tokens from the stream should be removed.
525 * If $token is a regular token, it is swapped with the current token.
527 * If $token is false, the current token is deleted.
529 * If $token is an integer, that number of tokens (with the first token
530 * being the current one) will be deleted.
532 * @param HTMLPurifier_Token|array|int|bool $token Token substitution value
533 * @param HTMLPurifier_Injector|int $injector Injector that performed the substitution; default is if
534 * this is not an injector related operation.
535 * @throws HTMLPurifier_Exception
537 protected function processToken($token, $injector = -1)
539 // normalize forms of token
540 if (is_object($token)) {
541 $token = array(1, $token);
543 if (is_int($token)) {
544 $token = array($token);
546 if ($token === false) {
547 $token = array(1);
549 if (!is_array($token)) {
550 throw new HTMLPurifier_Exception('Invalid token type from injector');
552 if (!is_int($token[0])) {
553 array_unshift($token, 1);
555 if ($token[0] === 0) {
556 throw new HTMLPurifier_Exception('Deleting zero tokens is not valid');
559 // $token is now an array with the following form:
560 // array(number nodes to delete, new node 1, new node 2, ...)
562 $delete = array_shift($token);
563 $old = array_splice($this->tokens, $this->t, $delete, $token);
565 if ($injector > -1) {
566 // determine appropriate skips
567 $oldskip = isset($old[0]) ? $old[0]->skip : array();
568 foreach ($token as $object) {
569 $object->skip = $oldskip;
570 $object->skip[$injector] = true;
577 * Inserts a token before the current token. Cursor now points to
578 * this token. You must reprocess after this.
579 * @param HTMLPurifier_Token $token
581 private function insertBefore($token)
583 array_splice($this->tokens, $this->t, 0, array($token));
587 * Removes current token. Cursor now points to new token occupying previously
588 * occupied space. You must reprocess after this.
590 private function remove()
592 array_splice($this->tokens, $this->t, 1);
596 * Swap current token with new token. Cursor points to new token (no
597 * change). You must reprocess after this.
598 * @param HTMLPurifier_Token $token
600 private function swap($token)
602 $this->tokens[$this->t] = $token;
606 // vim: et sw=4 sts=4