@@ -14,518 +14,518 @@ |
||
14 | 14 | class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * Array stream of tokens being processed. |
|
19 | - */ |
|
20 | - protected $tokens; |
|
21 | - |
|
22 | - /** |
|
23 | - * Current index in $tokens. |
|
24 | - */ |
|
25 | - protected $t; |
|
26 | - |
|
27 | - /** |
|
28 | - * Current nesting of elements. |
|
29 | - */ |
|
30 | - protected $stack; |
|
31 | - |
|
32 | - /** |
|
33 | - * Injectors active in this stream processing. |
|
34 | - */ |
|
35 | - protected $injectors; |
|
36 | - |
|
37 | - /** |
|
38 | - * Current instance of HTMLPurifier_Config. |
|
39 | - */ |
|
40 | - protected $config; |
|
41 | - |
|
42 | - /** |
|
43 | - * Current instance of HTMLPurifier_Context. |
|
44 | - */ |
|
45 | - protected $context; |
|
46 | - |
|
47 | - public function execute($tokens, $config, $context) { |
|
48 | - |
|
49 | - $definition = $config->getHTMLDefinition(); |
|
50 | - |
|
51 | - // local variables |
|
52 | - $generator = new HTMLPurifier_Generator($config, $context); |
|
53 | - $escape_invalid_tags = $config->get('Core.EscapeInvalidTags'); |
|
54 | - // used for autoclose early abortion |
|
55 | - $global_parent_allowed_elements = array(); |
|
56 | - if (isset($definition->info[$definition->info_parent])) { |
|
57 | - // may be unset under testing circumstances |
|
58 | - $global_parent_allowed_elements = $definition->info[$definition->info_parent]->child->getAllowedElements($config); |
|
59 | - } |
|
60 | - $e = $context->get('ErrorCollector', true); |
|
61 | - $t = false; // token index |
|
62 | - $i = false; // injector index |
|
63 | - $token = false; // the current token |
|
64 | - $reprocess = false; // whether or not to reprocess the same token |
|
65 | - $stack = array(); |
|
66 | - |
|
67 | - // member variables |
|
68 | - $this->stack =& $stack; |
|
69 | - $this->t =& $t; |
|
70 | - $this->tokens =& $tokens; |
|
71 | - $this->config = $config; |
|
72 | - $this->context = $context; |
|
73 | - |
|
74 | - // context variables |
|
75 | - $context->register('CurrentNesting', $stack); |
|
76 | - $context->register('InputIndex', $t); |
|
77 | - $context->register('InputTokens', $tokens); |
|
78 | - $context->register('CurrentToken', $token); |
|
79 | - |
|
80 | - // -- begin INJECTOR -- |
|
81 | - |
|
82 | - $this->injectors = array(); |
|
83 | - |
|
84 | - $injectors = $config->getBatch('AutoFormat'); |
|
85 | - $def_injectors = $definition->info_injector; |
|
86 | - $custom_injectors = $injectors['Custom']; |
|
87 | - unset($injectors['Custom']); // special case |
|
88 | - foreach ($injectors as $injector => $b) { |
|
89 | - // XXX: Fix with a legitimate lookup table of enabled filters |
|
90 | - if (strpos($injector, '.') !== false) continue; |
|
91 | - $injector = "HTMLPurifier_Injector_$injector"; |
|
92 | - if (!$b) continue; |
|
93 | - $this->injectors[] = new $injector; |
|
94 | - } |
|
95 | - foreach ($def_injectors as $injector) { |
|
96 | - // assumed to be objects |
|
97 | - $this->injectors[] = $injector; |
|
98 | - } |
|
99 | - foreach ($custom_injectors as $injector) { |
|
100 | - if (!$injector) continue; |
|
101 | - if (is_string($injector)) { |
|
102 | - $injector = "HTMLPurifier_Injector_$injector"; |
|
103 | - $injector = new $injector; |
|
104 | - } |
|
105 | - $this->injectors[] = $injector; |
|
106 | - } |
|
107 | - |
|
108 | - // give the injectors references to the definition and context |
|
109 | - // variables for performance reasons |
|
110 | - foreach ($this->injectors as $ix => $injector) { |
|
111 | - $error = $injector->prepare($config, $context); |
|
112 | - if (!$error) continue; |
|
113 | - array_splice($this->injectors, $ix, 1); // rm the injector |
|
114 | - trigger_error("Cannot enable {$injector->name} injector because $error is not allowed", E_USER_WARNING); |
|
115 | - } |
|
116 | - |
|
117 | - // -- end INJECTOR -- |
|
118 | - |
|
119 | - // a note on reprocessing: |
|
120 | - // In order to reduce code duplication, whenever some code needs |
|
121 | - // to make HTML changes in order to make things "correct", the |
|
122 | - // new HTML gets sent through the purifier, regardless of its |
|
123 | - // status. This means that if we add a start token, because it |
|
124 | - // was totally necessary, we don't have to update nesting; we just |
|
125 | - // punt ($reprocess = true; continue;) and it does that for us. |
|
126 | - |
|
127 | - // isset is in loop because $tokens size changes during loop exec |
|
128 | - for ( |
|
129 | - $t = 0; |
|
130 | - $t == 0 || isset($tokens[$t - 1]); |
|
131 | - // only increment if we don't need to reprocess |
|
132 | - $reprocess ? $reprocess = false : $t++ |
|
133 | - ) { |
|
134 | - |
|
135 | - // check for a rewind |
|
136 | - if (is_int($i) && $i >= 0) { |
|
137 | - // possibility: disable rewinding if the current token has a |
|
138 | - // rewind set on it already. This would offer protection from |
|
139 | - // infinite loop, but might hinder some advanced rewinding. |
|
140 | - $rewind_to = $this->injectors[$i]->getRewind(); |
|
141 | - if (is_int($rewind_to) && $rewind_to < $t) { |
|
142 | - if ($rewind_to < 0) $rewind_to = 0; |
|
143 | - while ($t > $rewind_to) { |
|
144 | - $t--; |
|
145 | - $prev = $tokens[$t]; |
|
146 | - // indicate that other injectors should not process this token, |
|
147 | - // but we need to reprocess it |
|
148 | - unset($prev->skip[$i]); |
|
149 | - $prev->rewind = $i; |
|
150 | - if ($prev instanceof HTMLPurifier_Token_Start) array_pop($this->stack); |
|
151 | - elseif ($prev instanceof HTMLPurifier_Token_End) $this->stack[] = $prev->start; |
|
152 | - } |
|
153 | - } |
|
154 | - $i = false; |
|
155 | - } |
|
156 | - |
|
157 | - // handle case of document end |
|
158 | - if (!isset($tokens[$t])) { |
|
159 | - // kill processing if stack is empty |
|
160 | - if (empty($this->stack)) break; |
|
161 | - |
|
162 | - // peek |
|
163 | - $top_nesting = array_pop($this->stack); |
|
164 | - $this->stack[] = $top_nesting; |
|
165 | - |
|
166 | - // send error [TagClosedSuppress] |
|
167 | - if ($e && !isset($top_nesting->armor['MakeWellFormed_TagClosedError'])) { |
|
168 | - $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by document end', $top_nesting); |
|
169 | - } |
|
170 | - |
|
171 | - // append, don't splice, since this is the end |
|
172 | - $tokens[] = new HTMLPurifier_Token_End($top_nesting->name); |
|
173 | - |
|
174 | - // punt! |
|
175 | - $reprocess = true; |
|
176 | - continue; |
|
177 | - } |
|
178 | - |
|
179 | - $token = $tokens[$t]; |
|
180 | - |
|
181 | - //echo '<br>'; printTokens($tokens, $t); printTokens($this->stack); |
|
182 | - //flush(); |
|
183 | - |
|
184 | - // quick-check: if it's not a tag, no need to process |
|
185 | - if (empty($token->is_tag)) { |
|
186 | - if ($token instanceof HTMLPurifier_Token_Text) { |
|
187 | - foreach ($this->injectors as $i => $injector) { |
|
188 | - if (isset($token->skip[$i])) continue; |
|
189 | - if ($token->rewind !== null && $token->rewind !== $i) continue; |
|
190 | - $injector->handleText($token); |
|
191 | - $this->processToken($token, $i); |
|
192 | - $reprocess = true; |
|
193 | - break; |
|
194 | - } |
|
195 | - } |
|
196 | - // another possibility is a comment |
|
197 | - continue; |
|
198 | - } |
|
199 | - |
|
200 | - if (isset($definition->info[$token->name])) { |
|
201 | - $type = $definition->info[$token->name]->child->type; |
|
202 | - } else { |
|
203 | - $type = false; // Type is unknown, treat accordingly |
|
204 | - } |
|
205 | - |
|
206 | - // quick tag checks: anything that's *not* an end tag |
|
207 | - $ok = false; |
|
208 | - if ($type === 'empty' && $token instanceof HTMLPurifier_Token_Start) { |
|
209 | - // claims to be a start tag but is empty |
|
210 | - $token = new HTMLPurifier_Token_Empty($token->name, $token->attr, $token->line, $token->col, $token->armor); |
|
211 | - $ok = true; |
|
212 | - } elseif ($type && $type !== 'empty' && $token instanceof HTMLPurifier_Token_Empty) { |
|
213 | - // claims to be empty but really is a start tag |
|
214 | - $this->swap(new HTMLPurifier_Token_End($token->name)); |
|
215 | - $this->insertBefore(new HTMLPurifier_Token_Start($token->name, $token->attr, $token->line, $token->col, $token->armor)); |
|
216 | - // punt (since we had to modify the input stream in a non-trivial way) |
|
217 | - $reprocess = true; |
|
218 | - continue; |
|
219 | - } elseif ($token instanceof HTMLPurifier_Token_Empty) { |
|
220 | - // real empty token |
|
221 | - $ok = true; |
|
222 | - } elseif ($token instanceof HTMLPurifier_Token_Start) { |
|
223 | - // start tag |
|
224 | - |
|
225 | - // ...unless they also have to close their parent |
|
226 | - if (!empty($this->stack)) { |
|
227 | - |
|
228 | - // Performance note: you might think that it's rather |
|
229 | - // inefficient, recalculating the autoclose information |
|
230 | - // for every tag that a token closes (since when we |
|
231 | - // do an autoclose, we push a new token into the |
|
232 | - // stream and then /process/ that, before |
|
233 | - // re-processing this token.) But this is |
|
234 | - // necessary, because an injector can make an |
|
235 | - // arbitrary transformations to the autoclosing |
|
236 | - // tokens we introduce, so things may have changed |
|
237 | - // in the meantime. Also, doing the inefficient thing is |
|
238 | - // "easy" to reason about (for certain perverse definitions |
|
239 | - // of "easy") |
|
240 | - |
|
241 | - $parent = array_pop($this->stack); |
|
242 | - $this->stack[] = $parent; |
|
243 | - |
|
244 | - if (isset($definition->info[$parent->name])) { |
|
245 | - $elements = $definition->info[$parent->name]->child->getAllowedElements($config); |
|
246 | - $autoclose = !isset($elements[$token->name]); |
|
247 | - } else { |
|
248 | - $autoclose = false; |
|
249 | - } |
|
250 | - |
|
251 | - if ($autoclose && $definition->info[$token->name]->wrap) { |
|
252 | - // Check if an element can be wrapped by another |
|
253 | - // element to make it valid in a context (for |
|
254 | - // example, <ul><ul> needs a <li> in between) |
|
255 | - $wrapname = $definition->info[$token->name]->wrap; |
|
256 | - $wrapdef = $definition->info[$wrapname]; |
|
257 | - $elements = $wrapdef->child->getAllowedElements($config); |
|
258 | - $parent_elements = $definition->info[$parent->name]->child->getAllowedElements($config); |
|
259 | - if (isset($elements[$token->name]) && isset($parent_elements[$wrapname])) { |
|
260 | - $newtoken = new HTMLPurifier_Token_Start($wrapname); |
|
261 | - $this->insertBefore($newtoken); |
|
262 | - $reprocess = true; |
|
263 | - continue; |
|
264 | - } |
|
265 | - } |
|
266 | - |
|
267 | - $carryover = false; |
|
268 | - if ($autoclose && $definition->info[$parent->name]->formatting) { |
|
269 | - $carryover = true; |
|
270 | - } |
|
271 | - |
|
272 | - if ($autoclose) { |
|
273 | - // check if this autoclose is doomed to fail |
|
274 | - // (this rechecks $parent, which his harmless) |
|
275 | - $autoclose_ok = isset($global_parent_allowed_elements[$token->name]); |
|
276 | - if (!$autoclose_ok) { |
|
277 | - foreach ($this->stack as $ancestor) { |
|
278 | - $elements = $definition->info[$ancestor->name]->child->getAllowedElements($config); |
|
279 | - if (isset($elements[$token->name])) { |
|
280 | - $autoclose_ok = true; |
|
281 | - break; |
|
282 | - } |
|
283 | - if ($definition->info[$token->name]->wrap) { |
|
284 | - $wrapname = $definition->info[$token->name]->wrap; |
|
285 | - $wrapdef = $definition->info[$wrapname]; |
|
286 | - $wrap_elements = $wrapdef->child->getAllowedElements($config); |
|
287 | - if (isset($wrap_elements[$token->name]) && isset($elements[$wrapname])) { |
|
288 | - $autoclose_ok = true; |
|
289 | - break; |
|
290 | - } |
|
291 | - } |
|
292 | - } |
|
293 | - } |
|
294 | - if ($autoclose_ok) { |
|
295 | - // errors need to be updated |
|
296 | - $new_token = new HTMLPurifier_Token_End($parent->name); |
|
297 | - $new_token->start = $parent; |
|
298 | - if ($carryover) { |
|
299 | - $element = clone $parent; |
|
300 | - // [TagClosedAuto] |
|
301 | - $element->armor['MakeWellFormed_TagClosedError'] = true; |
|
302 | - $element->carryover = true; |
|
303 | - $this->processToken(array($new_token, $token, $element)); |
|
304 | - } else { |
|
305 | - $this->insertBefore($new_token); |
|
306 | - } |
|
307 | - // [TagClosedSuppress] |
|
308 | - if ($e && !isset($parent->armor['MakeWellFormed_TagClosedError'])) { |
|
309 | - if (!$carryover) { |
|
310 | - $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag auto closed', $parent); |
|
311 | - } else { |
|
312 | - $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag carryover', $parent); |
|
313 | - } |
|
314 | - } |
|
315 | - } else { |
|
316 | - $this->remove(); |
|
317 | - } |
|
318 | - $reprocess = true; |
|
319 | - continue; |
|
320 | - } |
|
321 | - |
|
322 | - } |
|
323 | - $ok = true; |
|
324 | - } |
|
325 | - |
|
326 | - if ($ok) { |
|
327 | - foreach ($this->injectors as $i => $injector) { |
|
328 | - if (isset($token->skip[$i])) continue; |
|
329 | - if ($token->rewind !== null && $token->rewind !== $i) continue; |
|
330 | - $injector->handleElement($token); |
|
331 | - $this->processToken($token, $i); |
|
332 | - $reprocess = true; |
|
333 | - break; |
|
334 | - } |
|
335 | - if (!$reprocess) { |
|
336 | - // ah, nothing interesting happened; do normal processing |
|
337 | - $this->swap($token); |
|
338 | - if ($token instanceof HTMLPurifier_Token_Start) { |
|
339 | - $this->stack[] = $token; |
|
340 | - } elseif ($token instanceof HTMLPurifier_Token_End) { |
|
341 | - throw new HTMLPurifier_Exception('Improper handling of end tag in start code; possible error in MakeWellFormed'); |
|
342 | - } |
|
343 | - } |
|
344 | - continue; |
|
345 | - } |
|
346 | - |
|
347 | - // sanity check: we should be dealing with a closing tag |
|
348 | - if (!$token instanceof HTMLPurifier_Token_End) { |
|
349 | - throw new HTMLPurifier_Exception('Unaccounted for tag token in input stream, bug in HTML Purifier'); |
|
350 | - } |
|
351 | - |
|
352 | - // make sure that we have something open |
|
353 | - if (empty($this->stack)) { |
|
354 | - if ($escape_invalid_tags) { |
|
355 | - if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag to text'); |
|
356 | - $this->swap(new HTMLPurifier_Token_Text( |
|
357 | - $generator->generateFromToken($token) |
|
358 | - )); |
|
359 | - } else { |
|
360 | - $this->remove(); |
|
361 | - if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag removed'); |
|
362 | - } |
|
363 | - $reprocess = true; |
|
364 | - continue; |
|
365 | - } |
|
366 | - |
|
367 | - // first, check for the simplest case: everything closes neatly. |
|
368 | - // Eventually, everything passes through here; if there are problems |
|
369 | - // we modify the input stream accordingly and then punt, so that |
|
370 | - // the tokens get processed again. |
|
371 | - $current_parent = array_pop($this->stack); |
|
372 | - if ($current_parent->name == $token->name) { |
|
373 | - $token->start = $current_parent; |
|
374 | - foreach ($this->injectors as $i => $injector) { |
|
375 | - if (isset($token->skip[$i])) continue; |
|
376 | - if ($token->rewind !== null && $token->rewind !== $i) continue; |
|
377 | - $injector->handleEnd($token); |
|
378 | - $this->processToken($token, $i); |
|
379 | - $this->stack[] = $current_parent; |
|
380 | - $reprocess = true; |
|
381 | - break; |
|
382 | - } |
|
383 | - continue; |
|
384 | - } |
|
385 | - |
|
386 | - // okay, so we're trying to close the wrong tag |
|
387 | - |
|
388 | - // undo the pop previous pop |
|
389 | - $this->stack[] = $current_parent; |
|
390 | - |
|
391 | - // scroll back the entire nest, trying to find our tag. |
|
392 | - // (feature could be to specify how far you'd like to go) |
|
393 | - $size = count($this->stack); |
|
394 | - // -2 because -1 is the last element, but we already checked that |
|
395 | - $skipped_tags = false; |
|
396 | - for ($j = $size - 2; $j >= 0; $j--) { |
|
397 | - if ($this->stack[$j]->name == $token->name) { |
|
398 | - $skipped_tags = array_slice($this->stack, $j); |
|
399 | - break; |
|
400 | - } |
|
401 | - } |
|
402 | - |
|
403 | - // we didn't find the tag, so remove |
|
404 | - if ($skipped_tags === false) { |
|
405 | - if ($escape_invalid_tags) { |
|
406 | - $this->swap(new HTMLPurifier_Token_Text( |
|
407 | - $generator->generateFromToken($token) |
|
408 | - )); |
|
409 | - if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag to text'); |
|
410 | - } else { |
|
411 | - $this->remove(); |
|
412 | - if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag removed'); |
|
413 | - } |
|
414 | - $reprocess = true; |
|
415 | - continue; |
|
416 | - } |
|
417 | - |
|
418 | - // do errors, in REVERSE $j order: a,b,c with </a></b></c> |
|
419 | - $c = count($skipped_tags); |
|
420 | - if ($e) { |
|
421 | - for ($j = $c - 1; $j > 0; $j--) { |
|
422 | - // notice we exclude $j == 0, i.e. the current ending tag, from |
|
423 | - // the errors... [TagClosedSuppress] |
|
424 | - if (!isset($skipped_tags[$j]->armor['MakeWellFormed_TagClosedError'])) { |
|
425 | - $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by element end', $skipped_tags[$j]); |
|
426 | - } |
|
427 | - } |
|
428 | - } |
|
429 | - |
|
430 | - // insert tags, in FORWARD $j order: c,b,a with </a></b></c> |
|
431 | - $replace = array($token); |
|
432 | - for ($j = 1; $j < $c; $j++) { |
|
433 | - // ...as well as from the insertions |
|
434 | - $new_token = new HTMLPurifier_Token_End($skipped_tags[$j]->name); |
|
435 | - $new_token->start = $skipped_tags[$j]; |
|
436 | - array_unshift($replace, $new_token); |
|
437 | - if (isset($definition->info[$new_token->name]) && $definition->info[$new_token->name]->formatting) { |
|
438 | - // [TagClosedAuto] |
|
439 | - $element = clone $skipped_tags[$j]; |
|
440 | - $element->carryover = true; |
|
441 | - $element->armor['MakeWellFormed_TagClosedError'] = true; |
|
442 | - $replace[] = $element; |
|
443 | - } |
|
444 | - } |
|
445 | - $this->processToken($replace); |
|
446 | - $reprocess = true; |
|
447 | - continue; |
|
448 | - } |
|
449 | - |
|
450 | - $context->destroy('CurrentNesting'); |
|
451 | - $context->destroy('InputTokens'); |
|
452 | - $context->destroy('InputIndex'); |
|
453 | - $context->destroy('CurrentToken'); |
|
454 | - |
|
455 | - unset($this->injectors, $this->stack, $this->tokens, $this->t); |
|
456 | - return $tokens; |
|
457 | - } |
|
458 | - |
|
459 | - /** |
|
460 | - * Processes arbitrary token values for complicated substitution patterns. |
|
461 | - * In general: |
|
462 | - * |
|
463 | - * If $token is an array, it is a list of tokens to substitute for the |
|
464 | - * current token. These tokens then get individually processed. If there |
|
465 | - * is a leading integer in the list, that integer determines how many |
|
466 | - * tokens from the stream should be removed. |
|
467 | - * |
|
468 | - * If $token is a regular token, it is swapped with the current token. |
|
469 | - * |
|
470 | - * If $token is false, the current token is deleted. |
|
471 | - * |
|
472 | - * If $token is an integer, that number of tokens (with the first token |
|
473 | - * being the current one) will be deleted. |
|
474 | - * |
|
475 | - * @param $token Token substitution value |
|
476 | - * @param $injector Injector that performed the substitution; default is if |
|
477 | - * this is not an injector related operation. |
|
478 | - */ |
|
479 | - protected function processToken($token, $injector = -1) { |
|
480 | - |
|
481 | - // normalize forms of token |
|
482 | - if (is_object($token)) $token = array(1, $token); |
|
483 | - if (is_int($token)) $token = array($token); |
|
484 | - if ($token === false) $token = array(1); |
|
485 | - if (!is_array($token)) throw new HTMLPurifier_Exception('Invalid token type from injector'); |
|
486 | - if (!is_int($token[0])) array_unshift($token, 1); |
|
487 | - if ($token[0] === 0) throw new HTMLPurifier_Exception('Deleting zero tokens is not valid'); |
|
488 | - |
|
489 | - // $token is now an array with the following form: |
|
490 | - // array(number nodes to delete, new node 1, new node 2, ...) |
|
491 | - |
|
492 | - $delete = array_shift($token); |
|
493 | - $old = array_splice($this->tokens, $this->t, $delete, $token); |
|
494 | - |
|
495 | - if ($injector > -1) { |
|
496 | - // determine appropriate skips |
|
497 | - $oldskip = isset($old[0]) ? $old[0]->skip : array(); |
|
498 | - foreach ($token as $object) { |
|
499 | - $object->skip = $oldskip; |
|
500 | - $object->skip[$injector] = true; |
|
501 | - } |
|
502 | - } |
|
503 | - |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * Inserts a token before the current token. Cursor now points to |
|
508 | - * this token. You must reprocess after this. |
|
509 | - */ |
|
510 | - private function insertBefore($token) { |
|
511 | - array_splice($this->tokens, $this->t, 0, array($token)); |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * Removes current token. Cursor now points to new token occupying previously |
|
516 | - * occupied space. You must reprocess after this. |
|
517 | - */ |
|
518 | - private function remove() { |
|
519 | - array_splice($this->tokens, $this->t, 1); |
|
520 | - } |
|
521 | - |
|
522 | - /** |
|
523 | - * Swap current token with new token. Cursor points to new token (no |
|
524 | - * change). You must reprocess after this. |
|
525 | - */ |
|
526 | - private function swap($token) { |
|
527 | - $this->tokens[$this->t] = $token; |
|
528 | - } |
|
17 | + /** |
|
18 | + * Array stream of tokens being processed. |
|
19 | + */ |
|
20 | + protected $tokens; |
|
21 | + |
|
22 | + /** |
|
23 | + * Current index in $tokens. |
|
24 | + */ |
|
25 | + protected $t; |
|
26 | + |
|
27 | + /** |
|
28 | + * Current nesting of elements. |
|
29 | + */ |
|
30 | + protected $stack; |
|
31 | + |
|
32 | + /** |
|
33 | + * Injectors active in this stream processing. |
|
34 | + */ |
|
35 | + protected $injectors; |
|
36 | + |
|
37 | + /** |
|
38 | + * Current instance of HTMLPurifier_Config. |
|
39 | + */ |
|
40 | + protected $config; |
|
41 | + |
|
42 | + /** |
|
43 | + * Current instance of HTMLPurifier_Context. |
|
44 | + */ |
|
45 | + protected $context; |
|
46 | + |
|
47 | + public function execute($tokens, $config, $context) { |
|
48 | + |
|
49 | + $definition = $config->getHTMLDefinition(); |
|
50 | + |
|
51 | + // local variables |
|
52 | + $generator = new HTMLPurifier_Generator($config, $context); |
|
53 | + $escape_invalid_tags = $config->get('Core.EscapeInvalidTags'); |
|
54 | + // used for autoclose early abortion |
|
55 | + $global_parent_allowed_elements = array(); |
|
56 | + if (isset($definition->info[$definition->info_parent])) { |
|
57 | + // may be unset under testing circumstances |
|
58 | + $global_parent_allowed_elements = $definition->info[$definition->info_parent]->child->getAllowedElements($config); |
|
59 | + } |
|
60 | + $e = $context->get('ErrorCollector', true); |
|
61 | + $t = false; // token index |
|
62 | + $i = false; // injector index |
|
63 | + $token = false; // the current token |
|
64 | + $reprocess = false; // whether or not to reprocess the same token |
|
65 | + $stack = array(); |
|
66 | + |
|
67 | + // member variables |
|
68 | + $this->stack =& $stack; |
|
69 | + $this->t =& $t; |
|
70 | + $this->tokens =& $tokens; |
|
71 | + $this->config = $config; |
|
72 | + $this->context = $context; |
|
73 | + |
|
74 | + // context variables |
|
75 | + $context->register('CurrentNesting', $stack); |
|
76 | + $context->register('InputIndex', $t); |
|
77 | + $context->register('InputTokens', $tokens); |
|
78 | + $context->register('CurrentToken', $token); |
|
79 | + |
|
80 | + // -- begin INJECTOR -- |
|
81 | + |
|
82 | + $this->injectors = array(); |
|
83 | + |
|
84 | + $injectors = $config->getBatch('AutoFormat'); |
|
85 | + $def_injectors = $definition->info_injector; |
|
86 | + $custom_injectors = $injectors['Custom']; |
|
87 | + unset($injectors['Custom']); // special case |
|
88 | + foreach ($injectors as $injector => $b) { |
|
89 | + // XXX: Fix with a legitimate lookup table of enabled filters |
|
90 | + if (strpos($injector, '.') !== false) continue; |
|
91 | + $injector = "HTMLPurifier_Injector_$injector"; |
|
92 | + if (!$b) continue; |
|
93 | + $this->injectors[] = new $injector; |
|
94 | + } |
|
95 | + foreach ($def_injectors as $injector) { |
|
96 | + // assumed to be objects |
|
97 | + $this->injectors[] = $injector; |
|
98 | + } |
|
99 | + foreach ($custom_injectors as $injector) { |
|
100 | + if (!$injector) continue; |
|
101 | + if (is_string($injector)) { |
|
102 | + $injector = "HTMLPurifier_Injector_$injector"; |
|
103 | + $injector = new $injector; |
|
104 | + } |
|
105 | + $this->injectors[] = $injector; |
|
106 | + } |
|
107 | + |
|
108 | + // give the injectors references to the definition and context |
|
109 | + // variables for performance reasons |
|
110 | + foreach ($this->injectors as $ix => $injector) { |
|
111 | + $error = $injector->prepare($config, $context); |
|
112 | + if (!$error) continue; |
|
113 | + array_splice($this->injectors, $ix, 1); // rm the injector |
|
114 | + trigger_error("Cannot enable {$injector->name} injector because $error is not allowed", E_USER_WARNING); |
|
115 | + } |
|
116 | + |
|
117 | + // -- end INJECTOR -- |
|
118 | + |
|
119 | + // a note on reprocessing: |
|
120 | + // In order to reduce code duplication, whenever some code needs |
|
121 | + // to make HTML changes in order to make things "correct", the |
|
122 | + // new HTML gets sent through the purifier, regardless of its |
|
123 | + // status. This means that if we add a start token, because it |
|
124 | + // was totally necessary, we don't have to update nesting; we just |
|
125 | + // punt ($reprocess = true; continue;) and it does that for us. |
|
126 | + |
|
127 | + // isset is in loop because $tokens size changes during loop exec |
|
128 | + for ( |
|
129 | + $t = 0; |
|
130 | + $t == 0 || isset($tokens[$t - 1]); |
|
131 | + // only increment if we don't need to reprocess |
|
132 | + $reprocess ? $reprocess = false : $t++ |
|
133 | + ) { |
|
134 | + |
|
135 | + // check for a rewind |
|
136 | + if (is_int($i) && $i >= 0) { |
|
137 | + // possibility: disable rewinding if the current token has a |
|
138 | + // rewind set on it already. This would offer protection from |
|
139 | + // infinite loop, but might hinder some advanced rewinding. |
|
140 | + $rewind_to = $this->injectors[$i]->getRewind(); |
|
141 | + if (is_int($rewind_to) && $rewind_to < $t) { |
|
142 | + if ($rewind_to < 0) $rewind_to = 0; |
|
143 | + while ($t > $rewind_to) { |
|
144 | + $t--; |
|
145 | + $prev = $tokens[$t]; |
|
146 | + // indicate that other injectors should not process this token, |
|
147 | + // but we need to reprocess it |
|
148 | + unset($prev->skip[$i]); |
|
149 | + $prev->rewind = $i; |
|
150 | + if ($prev instanceof HTMLPurifier_Token_Start) array_pop($this->stack); |
|
151 | + elseif ($prev instanceof HTMLPurifier_Token_End) $this->stack[] = $prev->start; |
|
152 | + } |
|
153 | + } |
|
154 | + $i = false; |
|
155 | + } |
|
156 | + |
|
157 | + // handle case of document end |
|
158 | + if (!isset($tokens[$t])) { |
|
159 | + // kill processing if stack is empty |
|
160 | + if (empty($this->stack)) break; |
|
161 | + |
|
162 | + // peek |
|
163 | + $top_nesting = array_pop($this->stack); |
|
164 | + $this->stack[] = $top_nesting; |
|
165 | + |
|
166 | + // send error [TagClosedSuppress] |
|
167 | + if ($e && !isset($top_nesting->armor['MakeWellFormed_TagClosedError'])) { |
|
168 | + $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by document end', $top_nesting); |
|
169 | + } |
|
170 | + |
|
171 | + // append, don't splice, since this is the end |
|
172 | + $tokens[] = new HTMLPurifier_Token_End($top_nesting->name); |
|
173 | + |
|
174 | + // punt! |
|
175 | + $reprocess = true; |
|
176 | + continue; |
|
177 | + } |
|
178 | + |
|
179 | + $token = $tokens[$t]; |
|
180 | + |
|
181 | + //echo '<br>'; printTokens($tokens, $t); printTokens($this->stack); |
|
182 | + //flush(); |
|
183 | + |
|
184 | + // quick-check: if it's not a tag, no need to process |
|
185 | + if (empty($token->is_tag)) { |
|
186 | + if ($token instanceof HTMLPurifier_Token_Text) { |
|
187 | + foreach ($this->injectors as $i => $injector) { |
|
188 | + if (isset($token->skip[$i])) continue; |
|
189 | + if ($token->rewind !== null && $token->rewind !== $i) continue; |
|
190 | + $injector->handleText($token); |
|
191 | + $this->processToken($token, $i); |
|
192 | + $reprocess = true; |
|
193 | + break; |
|
194 | + } |
|
195 | + } |
|
196 | + // another possibility is a comment |
|
197 | + continue; |
|
198 | + } |
|
199 | + |
|
200 | + if (isset($definition->info[$token->name])) { |
|
201 | + $type = $definition->info[$token->name]->child->type; |
|
202 | + } else { |
|
203 | + $type = false; // Type is unknown, treat accordingly |
|
204 | + } |
|
205 | + |
|
206 | + // quick tag checks: anything that's *not* an end tag |
|
207 | + $ok = false; |
|
208 | + if ($type === 'empty' && $token instanceof HTMLPurifier_Token_Start) { |
|
209 | + // claims to be a start tag but is empty |
|
210 | + $token = new HTMLPurifier_Token_Empty($token->name, $token->attr, $token->line, $token->col, $token->armor); |
|
211 | + $ok = true; |
|
212 | + } elseif ($type && $type !== 'empty' && $token instanceof HTMLPurifier_Token_Empty) { |
|
213 | + // claims to be empty but really is a start tag |
|
214 | + $this->swap(new HTMLPurifier_Token_End($token->name)); |
|
215 | + $this->insertBefore(new HTMLPurifier_Token_Start($token->name, $token->attr, $token->line, $token->col, $token->armor)); |
|
216 | + // punt (since we had to modify the input stream in a non-trivial way) |
|
217 | + $reprocess = true; |
|
218 | + continue; |
|
219 | + } elseif ($token instanceof HTMLPurifier_Token_Empty) { |
|
220 | + // real empty token |
|
221 | + $ok = true; |
|
222 | + } elseif ($token instanceof HTMLPurifier_Token_Start) { |
|
223 | + // start tag |
|
224 | + |
|
225 | + // ...unless they also have to close their parent |
|
226 | + if (!empty($this->stack)) { |
|
227 | + |
|
228 | + // Performance note: you might think that it's rather |
|
229 | + // inefficient, recalculating the autoclose information |
|
230 | + // for every tag that a token closes (since when we |
|
231 | + // do an autoclose, we push a new token into the |
|
232 | + // stream and then /process/ that, before |
|
233 | + // re-processing this token.) But this is |
|
234 | + // necessary, because an injector can make an |
|
235 | + // arbitrary transformations to the autoclosing |
|
236 | + // tokens we introduce, so things may have changed |
|
237 | + // in the meantime. Also, doing the inefficient thing is |
|
238 | + // "easy" to reason about (for certain perverse definitions |
|
239 | + // of "easy") |
|
240 | + |
|
241 | + $parent = array_pop($this->stack); |
|
242 | + $this->stack[] = $parent; |
|
243 | + |
|
244 | + if (isset($definition->info[$parent->name])) { |
|
245 | + $elements = $definition->info[$parent->name]->child->getAllowedElements($config); |
|
246 | + $autoclose = !isset($elements[$token->name]); |
|
247 | + } else { |
|
248 | + $autoclose = false; |
|
249 | + } |
|
250 | + |
|
251 | + if ($autoclose && $definition->info[$token->name]->wrap) { |
|
252 | + // Check if an element can be wrapped by another |
|
253 | + // element to make it valid in a context (for |
|
254 | + // example, <ul><ul> needs a <li> in between) |
|
255 | + $wrapname = $definition->info[$token->name]->wrap; |
|
256 | + $wrapdef = $definition->info[$wrapname]; |
|
257 | + $elements = $wrapdef->child->getAllowedElements($config); |
|
258 | + $parent_elements = $definition->info[$parent->name]->child->getAllowedElements($config); |
|
259 | + if (isset($elements[$token->name]) && isset($parent_elements[$wrapname])) { |
|
260 | + $newtoken = new HTMLPurifier_Token_Start($wrapname); |
|
261 | + $this->insertBefore($newtoken); |
|
262 | + $reprocess = true; |
|
263 | + continue; |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + $carryover = false; |
|
268 | + if ($autoclose && $definition->info[$parent->name]->formatting) { |
|
269 | + $carryover = true; |
|
270 | + } |
|
271 | + |
|
272 | + if ($autoclose) { |
|
273 | + // check if this autoclose is doomed to fail |
|
274 | + // (this rechecks $parent, which his harmless) |
|
275 | + $autoclose_ok = isset($global_parent_allowed_elements[$token->name]); |
|
276 | + if (!$autoclose_ok) { |
|
277 | + foreach ($this->stack as $ancestor) { |
|
278 | + $elements = $definition->info[$ancestor->name]->child->getAllowedElements($config); |
|
279 | + if (isset($elements[$token->name])) { |
|
280 | + $autoclose_ok = true; |
|
281 | + break; |
|
282 | + } |
|
283 | + if ($definition->info[$token->name]->wrap) { |
|
284 | + $wrapname = $definition->info[$token->name]->wrap; |
|
285 | + $wrapdef = $definition->info[$wrapname]; |
|
286 | + $wrap_elements = $wrapdef->child->getAllowedElements($config); |
|
287 | + if (isset($wrap_elements[$token->name]) && isset($elements[$wrapname])) { |
|
288 | + $autoclose_ok = true; |
|
289 | + break; |
|
290 | + } |
|
291 | + } |
|
292 | + } |
|
293 | + } |
|
294 | + if ($autoclose_ok) { |
|
295 | + // errors need to be updated |
|
296 | + $new_token = new HTMLPurifier_Token_End($parent->name); |
|
297 | + $new_token->start = $parent; |
|
298 | + if ($carryover) { |
|
299 | + $element = clone $parent; |
|
300 | + // [TagClosedAuto] |
|
301 | + $element->armor['MakeWellFormed_TagClosedError'] = true; |
|
302 | + $element->carryover = true; |
|
303 | + $this->processToken(array($new_token, $token, $element)); |
|
304 | + } else { |
|
305 | + $this->insertBefore($new_token); |
|
306 | + } |
|
307 | + // [TagClosedSuppress] |
|
308 | + if ($e && !isset($parent->armor['MakeWellFormed_TagClosedError'])) { |
|
309 | + if (!$carryover) { |
|
310 | + $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag auto closed', $parent); |
|
311 | + } else { |
|
312 | + $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag carryover', $parent); |
|
313 | + } |
|
314 | + } |
|
315 | + } else { |
|
316 | + $this->remove(); |
|
317 | + } |
|
318 | + $reprocess = true; |
|
319 | + continue; |
|
320 | + } |
|
321 | + |
|
322 | + } |
|
323 | + $ok = true; |
|
324 | + } |
|
325 | + |
|
326 | + if ($ok) { |
|
327 | + foreach ($this->injectors as $i => $injector) { |
|
328 | + if (isset($token->skip[$i])) continue; |
|
329 | + if ($token->rewind !== null && $token->rewind !== $i) continue; |
|
330 | + $injector->handleElement($token); |
|
331 | + $this->processToken($token, $i); |
|
332 | + $reprocess = true; |
|
333 | + break; |
|
334 | + } |
|
335 | + if (!$reprocess) { |
|
336 | + // ah, nothing interesting happened; do normal processing |
|
337 | + $this->swap($token); |
|
338 | + if ($token instanceof HTMLPurifier_Token_Start) { |
|
339 | + $this->stack[] = $token; |
|
340 | + } elseif ($token instanceof HTMLPurifier_Token_End) { |
|
341 | + throw new HTMLPurifier_Exception('Improper handling of end tag in start code; possible error in MakeWellFormed'); |
|
342 | + } |
|
343 | + } |
|
344 | + continue; |
|
345 | + } |
|
346 | + |
|
347 | + // sanity check: we should be dealing with a closing tag |
|
348 | + if (!$token instanceof HTMLPurifier_Token_End) { |
|
349 | + throw new HTMLPurifier_Exception('Unaccounted for tag token in input stream, bug in HTML Purifier'); |
|
350 | + } |
|
351 | + |
|
352 | + // make sure that we have something open |
|
353 | + if (empty($this->stack)) { |
|
354 | + if ($escape_invalid_tags) { |
|
355 | + if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag to text'); |
|
356 | + $this->swap(new HTMLPurifier_Token_Text( |
|
357 | + $generator->generateFromToken($token) |
|
358 | + )); |
|
359 | + } else { |
|
360 | + $this->remove(); |
|
361 | + if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag removed'); |
|
362 | + } |
|
363 | + $reprocess = true; |
|
364 | + continue; |
|
365 | + } |
|
366 | + |
|
367 | + // first, check for the simplest case: everything closes neatly. |
|
368 | + // Eventually, everything passes through here; if there are problems |
|
369 | + // we modify the input stream accordingly and then punt, so that |
|
370 | + // the tokens get processed again. |
|
371 | + $current_parent = array_pop($this->stack); |
|
372 | + if ($current_parent->name == $token->name) { |
|
373 | + $token->start = $current_parent; |
|
374 | + foreach ($this->injectors as $i => $injector) { |
|
375 | + if (isset($token->skip[$i])) continue; |
|
376 | + if ($token->rewind !== null && $token->rewind !== $i) continue; |
|
377 | + $injector->handleEnd($token); |
|
378 | + $this->processToken($token, $i); |
|
379 | + $this->stack[] = $current_parent; |
|
380 | + $reprocess = true; |
|
381 | + break; |
|
382 | + } |
|
383 | + continue; |
|
384 | + } |
|
385 | + |
|
386 | + // okay, so we're trying to close the wrong tag |
|
387 | + |
|
388 | + // undo the pop previous pop |
|
389 | + $this->stack[] = $current_parent; |
|
390 | + |
|
391 | + // scroll back the entire nest, trying to find our tag. |
|
392 | + // (feature could be to specify how far you'd like to go) |
|
393 | + $size = count($this->stack); |
|
394 | + // -2 because -1 is the last element, but we already checked that |
|
395 | + $skipped_tags = false; |
|
396 | + for ($j = $size - 2; $j >= 0; $j--) { |
|
397 | + if ($this->stack[$j]->name == $token->name) { |
|
398 | + $skipped_tags = array_slice($this->stack, $j); |
|
399 | + break; |
|
400 | + } |
|
401 | + } |
|
402 | + |
|
403 | + // we didn't find the tag, so remove |
|
404 | + if ($skipped_tags === false) { |
|
405 | + if ($escape_invalid_tags) { |
|
406 | + $this->swap(new HTMLPurifier_Token_Text( |
|
407 | + $generator->generateFromToken($token) |
|
408 | + )); |
|
409 | + if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag to text'); |
|
410 | + } else { |
|
411 | + $this->remove(); |
|
412 | + if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag removed'); |
|
413 | + } |
|
414 | + $reprocess = true; |
|
415 | + continue; |
|
416 | + } |
|
417 | + |
|
418 | + // do errors, in REVERSE $j order: a,b,c with </a></b></c> |
|
419 | + $c = count($skipped_tags); |
|
420 | + if ($e) { |
|
421 | + for ($j = $c - 1; $j > 0; $j--) { |
|
422 | + // notice we exclude $j == 0, i.e. the current ending tag, from |
|
423 | + // the errors... [TagClosedSuppress] |
|
424 | + if (!isset($skipped_tags[$j]->armor['MakeWellFormed_TagClosedError'])) { |
|
425 | + $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by element end', $skipped_tags[$j]); |
|
426 | + } |
|
427 | + } |
|
428 | + } |
|
429 | + |
|
430 | + // insert tags, in FORWARD $j order: c,b,a with </a></b></c> |
|
431 | + $replace = array($token); |
|
432 | + for ($j = 1; $j < $c; $j++) { |
|
433 | + // ...as well as from the insertions |
|
434 | + $new_token = new HTMLPurifier_Token_End($skipped_tags[$j]->name); |
|
435 | + $new_token->start = $skipped_tags[$j]; |
|
436 | + array_unshift($replace, $new_token); |
|
437 | + if (isset($definition->info[$new_token->name]) && $definition->info[$new_token->name]->formatting) { |
|
438 | + // [TagClosedAuto] |
|
439 | + $element = clone $skipped_tags[$j]; |
|
440 | + $element->carryover = true; |
|
441 | + $element->armor['MakeWellFormed_TagClosedError'] = true; |
|
442 | + $replace[] = $element; |
|
443 | + } |
|
444 | + } |
|
445 | + $this->processToken($replace); |
|
446 | + $reprocess = true; |
|
447 | + continue; |
|
448 | + } |
|
449 | + |
|
450 | + $context->destroy('CurrentNesting'); |
|
451 | + $context->destroy('InputTokens'); |
|
452 | + $context->destroy('InputIndex'); |
|
453 | + $context->destroy('CurrentToken'); |
|
454 | + |
|
455 | + unset($this->injectors, $this->stack, $this->tokens, $this->t); |
|
456 | + return $tokens; |
|
457 | + } |
|
458 | + |
|
459 | + /** |
|
460 | + * Processes arbitrary token values for complicated substitution patterns. |
|
461 | + * In general: |
|
462 | + * |
|
463 | + * If $token is an array, it is a list of tokens to substitute for the |
|
464 | + * current token. These tokens then get individually processed. If there |
|
465 | + * is a leading integer in the list, that integer determines how many |
|
466 | + * tokens from the stream should be removed. |
|
467 | + * |
|
468 | + * If $token is a regular token, it is swapped with the current token. |
|
469 | + * |
|
470 | + * If $token is false, the current token is deleted. |
|
471 | + * |
|
472 | + * If $token is an integer, that number of tokens (with the first token |
|
473 | + * being the current one) will be deleted. |
|
474 | + * |
|
475 | + * @param $token Token substitution value |
|
476 | + * @param $injector Injector that performed the substitution; default is if |
|
477 | + * this is not an injector related operation. |
|
478 | + */ |
|
479 | + protected function processToken($token, $injector = -1) { |
|
480 | + |
|
481 | + // normalize forms of token |
|
482 | + if (is_object($token)) $token = array(1, $token); |
|
483 | + if (is_int($token)) $token = array($token); |
|
484 | + if ($token === false) $token = array(1); |
|
485 | + if (!is_array($token)) throw new HTMLPurifier_Exception('Invalid token type from injector'); |
|
486 | + if (!is_int($token[0])) array_unshift($token, 1); |
|
487 | + if ($token[0] === 0) throw new HTMLPurifier_Exception('Deleting zero tokens is not valid'); |
|
488 | + |
|
489 | + // $token is now an array with the following form: |
|
490 | + // array(number nodes to delete, new node 1, new node 2, ...) |
|
491 | + |
|
492 | + $delete = array_shift($token); |
|
493 | + $old = array_splice($this->tokens, $this->t, $delete, $token); |
|
494 | + |
|
495 | + if ($injector > -1) { |
|
496 | + // determine appropriate skips |
|
497 | + $oldskip = isset($old[0]) ? $old[0]->skip : array(); |
|
498 | + foreach ($token as $object) { |
|
499 | + $object->skip = $oldskip; |
|
500 | + $object->skip[$injector] = true; |
|
501 | + } |
|
502 | + } |
|
503 | + |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * Inserts a token before the current token. Cursor now points to |
|
508 | + * this token. You must reprocess after this. |
|
509 | + */ |
|
510 | + private function insertBefore($token) { |
|
511 | + array_splice($this->tokens, $this->t, 0, array($token)); |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * Removes current token. Cursor now points to new token occupying previously |
|
516 | + * occupied space. You must reprocess after this. |
|
517 | + */ |
|
518 | + private function remove() { |
|
519 | + array_splice($this->tokens, $this->t, 1); |
|
520 | + } |
|
521 | + |
|
522 | + /** |
|
523 | + * Swap current token with new token. Cursor points to new token (no |
|
524 | + * change). You must reprocess after this. |
|
525 | + */ |
|
526 | + private function swap($token) { |
|
527 | + $this->tokens[$this->t] = $token; |
|
528 | + } |
|
529 | 529 | |
530 | 530 | } |
531 | 531 |
@@ -65,17 +65,17 @@ |
||
65 | 65 | $stack = array(); |
66 | 66 | |
67 | 67 | // member variables |
68 | - $this->stack =& $stack; |
|
69 | - $this->t =& $t; |
|
70 | - $this->tokens =& $tokens; |
|
68 | + $this->stack = & $stack; |
|
69 | + $this->t = & $t; |
|
70 | + $this->tokens = & $tokens; |
|
71 | 71 | $this->config = $config; |
72 | 72 | $this->context = $context; |
73 | 73 | |
74 | 74 | // context variables |
75 | 75 | $context->register('CurrentNesting', $stack); |
76 | - $context->register('InputIndex', $t); |
|
77 | - $context->register('InputTokens', $tokens); |
|
78 | - $context->register('CurrentToken', $token); |
|
76 | + $context->register('InputIndex', $t); |
|
77 | + $context->register('InputTokens', $tokens); |
|
78 | + $context->register('CurrentToken', $token); |
|
79 | 79 | |
80 | 80 | // -- begin INJECTOR -- |
81 | 81 |
@@ -87,9 +87,13 @@ discard block |
||
87 | 87 | unset($injectors['Custom']); // special case |
88 | 88 | foreach ($injectors as $injector => $b) { |
89 | 89 | // XXX: Fix with a legitimate lookup table of enabled filters |
90 | - if (strpos($injector, '.') !== false) continue; |
|
90 | + if (strpos($injector, '.') !== false) { |
|
91 | + continue; |
|
92 | + } |
|
91 | 93 | $injector = "HTMLPurifier_Injector_$injector"; |
92 | - if (!$b) continue; |
|
94 | + if (!$b) { |
|
95 | + continue; |
|
96 | + } |
|
93 | 97 | $this->injectors[] = new $injector; |
94 | 98 | } |
95 | 99 | foreach ($def_injectors as $injector) { |
@@ -97,7 +101,9 @@ discard block |
||
97 | 101 | $this->injectors[] = $injector; |
98 | 102 | } |
99 | 103 | foreach ($custom_injectors as $injector) { |
100 | - if (!$injector) continue; |
|
104 | + if (!$injector) { |
|
105 | + continue; |
|
106 | + } |
|
101 | 107 | if (is_string($injector)) { |
102 | 108 | $injector = "HTMLPurifier_Injector_$injector"; |
103 | 109 | $injector = new $injector; |
@@ -109,7 +115,9 @@ discard block |
||
109 | 115 | // variables for performance reasons |
110 | 116 | foreach ($this->injectors as $ix => $injector) { |
111 | 117 | $error = $injector->prepare($config, $context); |
112 | - if (!$error) continue; |
|
118 | + if (!$error) { |
|
119 | + continue; |
|
120 | + } |
|
113 | 121 | array_splice($this->injectors, $ix, 1); // rm the injector |
114 | 122 | trigger_error("Cannot enable {$injector->name} injector because $error is not allowed", E_USER_WARNING); |
115 | 123 | } |
@@ -139,7 +147,9 @@ discard block |
||
139 | 147 | // infinite loop, but might hinder some advanced rewinding. |
140 | 148 | $rewind_to = $this->injectors[$i]->getRewind(); |
141 | 149 | if (is_int($rewind_to) && $rewind_to < $t) { |
142 | - if ($rewind_to < 0) $rewind_to = 0; |
|
150 | + if ($rewind_to < 0) { |
|
151 | + $rewind_to = 0; |
|
152 | + } |
|
143 | 153 | while ($t > $rewind_to) { |
144 | 154 | $t--; |
145 | 155 | $prev = $tokens[$t]; |
@@ -147,8 +157,11 @@ discard block |
||
147 | 157 | // but we need to reprocess it |
148 | 158 | unset($prev->skip[$i]); |
149 | 159 | $prev->rewind = $i; |
150 | - if ($prev instanceof HTMLPurifier_Token_Start) array_pop($this->stack); |
|
151 | - elseif ($prev instanceof HTMLPurifier_Token_End) $this->stack[] = $prev->start; |
|
160 | + if ($prev instanceof HTMLPurifier_Token_Start) { |
|
161 | + array_pop($this->stack); |
|
162 | + } elseif ($prev instanceof HTMLPurifier_Token_End) { |
|
163 | + $this->stack[] = $prev->start; |
|
164 | + } |
|
152 | 165 | } |
153 | 166 | } |
154 | 167 | $i = false; |
@@ -157,7 +170,9 @@ discard block |
||
157 | 170 | // handle case of document end |
158 | 171 | if (!isset($tokens[$t])) { |
159 | 172 | // kill processing if stack is empty |
160 | - if (empty($this->stack)) break; |
|
173 | + if (empty($this->stack)) { |
|
174 | + break; |
|
175 | + } |
|
161 | 176 | |
162 | 177 | // peek |
163 | 178 | $top_nesting = array_pop($this->stack); |
@@ -185,8 +200,12 @@ discard block |
||
185 | 200 | if (empty($token->is_tag)) { |
186 | 201 | if ($token instanceof HTMLPurifier_Token_Text) { |
187 | 202 | foreach ($this->injectors as $i => $injector) { |
188 | - if (isset($token->skip[$i])) continue; |
|
189 | - if ($token->rewind !== null && $token->rewind !== $i) continue; |
|
203 | + if (isset($token->skip[$i])) { |
|
204 | + continue; |
|
205 | + } |
|
206 | + if ($token->rewind !== null && $token->rewind !== $i) { |
|
207 | + continue; |
|
208 | + } |
|
190 | 209 | $injector->handleText($token); |
191 | 210 | $this->processToken($token, $i); |
192 | 211 | $reprocess = true; |
@@ -325,8 +344,12 @@ discard block |
||
325 | 344 | |
326 | 345 | if ($ok) { |
327 | 346 | foreach ($this->injectors as $i => $injector) { |
328 | - if (isset($token->skip[$i])) continue; |
|
329 | - if ($token->rewind !== null && $token->rewind !== $i) continue; |
|
347 | + if (isset($token->skip[$i])) { |
|
348 | + continue; |
|
349 | + } |
|
350 | + if ($token->rewind !== null && $token->rewind !== $i) { |
|
351 | + continue; |
|
352 | + } |
|
330 | 353 | $injector->handleElement($token); |
331 | 354 | $this->processToken($token, $i); |
332 | 355 | $reprocess = true; |
@@ -352,13 +375,17 @@ discard block |
||
352 | 375 | // make sure that we have something open |
353 | 376 | if (empty($this->stack)) { |
354 | 377 | if ($escape_invalid_tags) { |
355 | - if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag to text'); |
|
378 | + if ($e) { |
|
379 | + $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag to text'); |
|
380 | + } |
|
356 | 381 | $this->swap(new HTMLPurifier_Token_Text( |
357 | 382 | $generator->generateFromToken($token) |
358 | 383 | )); |
359 | 384 | } else { |
360 | 385 | $this->remove(); |
361 | - if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag removed'); |
|
386 | + if ($e) { |
|
387 | + $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag removed'); |
|
388 | + } |
|
362 | 389 | } |
363 | 390 | $reprocess = true; |
364 | 391 | continue; |
@@ -372,8 +399,12 @@ discard block |
||
372 | 399 | if ($current_parent->name == $token->name) { |
373 | 400 | $token->start = $current_parent; |
374 | 401 | foreach ($this->injectors as $i => $injector) { |
375 | - if (isset($token->skip[$i])) continue; |
|
376 | - if ($token->rewind !== null && $token->rewind !== $i) continue; |
|
402 | + if (isset($token->skip[$i])) { |
|
403 | + continue; |
|
404 | + } |
|
405 | + if ($token->rewind !== null && $token->rewind !== $i) { |
|
406 | + continue; |
|
407 | + } |
|
377 | 408 | $injector->handleEnd($token); |
378 | 409 | $this->processToken($token, $i); |
379 | 410 | $this->stack[] = $current_parent; |
@@ -406,10 +437,14 @@ discard block |
||
406 | 437 | $this->swap(new HTMLPurifier_Token_Text( |
407 | 438 | $generator->generateFromToken($token) |
408 | 439 | )); |
409 | - if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag to text'); |
|
440 | + if ($e) { |
|
441 | + $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag to text'); |
|
442 | + } |
|
410 | 443 | } else { |
411 | 444 | $this->remove(); |
412 | - if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag removed'); |
|
445 | + if ($e) { |
|
446 | + $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag removed'); |
|
447 | + } |
|
413 | 448 | } |
414 | 449 | $reprocess = true; |
415 | 450 | continue; |
@@ -479,12 +514,24 @@ discard block |
||
479 | 514 | protected function processToken($token, $injector = -1) { |
480 | 515 | |
481 | 516 | // normalize forms of token |
482 | - if (is_object($token)) $token = array(1, $token); |
|
483 | - if (is_int($token)) $token = array($token); |
|
484 | - if ($token === false) $token = array(1); |
|
485 | - if (!is_array($token)) throw new HTMLPurifier_Exception('Invalid token type from injector'); |
|
486 | - if (!is_int($token[0])) array_unshift($token, 1); |
|
487 | - if ($token[0] === 0) throw new HTMLPurifier_Exception('Deleting zero tokens is not valid'); |
|
517 | + if (is_object($token)) { |
|
518 | + $token = array(1, $token); |
|
519 | + } |
|
520 | + if (is_int($token)) { |
|
521 | + $token = array($token); |
|
522 | + } |
|
523 | + if ($token === false) { |
|
524 | + $token = array(1); |
|
525 | + } |
|
526 | + if (!is_array($token)) { |
|
527 | + throw new HTMLPurifier_Exception('Invalid token type from injector'); |
|
528 | + } |
|
529 | + if (!is_int($token[0])) { |
|
530 | + array_unshift($token, 1); |
|
531 | + } |
|
532 | + if ($token[0] === 0) { |
|
533 | + throw new HTMLPurifier_Exception('Deleting zero tokens is not valid'); |
|
534 | + } |
|
488 | 535 | |
489 | 536 | // $token is now an array with the following form: |
490 | 537 | // array(number nodes to delete, new node 1, new node 2, ...) |
@@ -11,177 +11,177 @@ |
||
11 | 11 | class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy |
12 | 12 | { |
13 | 13 | |
14 | - public function execute($tokens, $config, $context) { |
|
15 | - $definition = $config->getHTMLDefinition(); |
|
16 | - $generator = new HTMLPurifier_Generator($config, $context); |
|
17 | - $result = array(); |
|
18 | - |
|
19 | - $escape_invalid_tags = $config->get('Core.EscapeInvalidTags'); |
|
20 | - $remove_invalid_img = $config->get('Core.RemoveInvalidImg'); |
|
21 | - |
|
22 | - // currently only used to determine if comments should be kept |
|
23 | - $trusted = $config->get('HTML.Trusted'); |
|
24 | - $comment_lookup = $config->get('HTML.AllowedComments'); |
|
25 | - $comment_regexp = $config->get('HTML.AllowedCommentsRegexp'); |
|
26 | - $check_comments = $comment_lookup !== array() || $comment_regexp !== null; |
|
27 | - |
|
28 | - $remove_script_contents = $config->get('Core.RemoveScriptContents'); |
|
29 | - $hidden_elements = $config->get('Core.HiddenElements'); |
|
30 | - |
|
31 | - // remove script contents compatibility |
|
32 | - if ($remove_script_contents === true) { |
|
33 | - $hidden_elements['script'] = true; |
|
34 | - } elseif ($remove_script_contents === false && isset($hidden_elements['script'])) { |
|
35 | - unset($hidden_elements['script']); |
|
36 | - } |
|
37 | - |
|
38 | - $attr_validator = new HTMLPurifier_AttrValidator(); |
|
39 | - |
|
40 | - // removes tokens until it reaches a closing tag with its value |
|
41 | - $remove_until = false; |
|
42 | - |
|
43 | - // converts comments into text tokens when this is equal to a tag name |
|
44 | - $textify_comments = false; |
|
45 | - |
|
46 | - $token = false; |
|
47 | - $context->register('CurrentToken', $token); |
|
48 | - |
|
49 | - $e = false; |
|
50 | - if ($config->get('Core.CollectErrors')) { |
|
51 | - $e =& $context->get('ErrorCollector'); |
|
52 | - } |
|
53 | - |
|
54 | - foreach($tokens as $token) { |
|
55 | - if ($remove_until) { |
|
56 | - if (empty($token->is_tag) || $token->name !== $remove_until) { |
|
57 | - continue; |
|
58 | - } |
|
59 | - } |
|
60 | - if (!empty( $token->is_tag )) { |
|
61 | - // DEFINITION CALL |
|
62 | - |
|
63 | - // before any processing, try to transform the element |
|
64 | - if ( |
|
65 | - isset($definition->info_tag_transform[$token->name]) |
|
66 | - ) { |
|
67 | - $original_name = $token->name; |
|
68 | - // there is a transformation for this tag |
|
69 | - // DEFINITION CALL |
|
70 | - $token = $definition-> |
|
71 | - info_tag_transform[$token->name]-> |
|
72 | - transform($token, $config, $context); |
|
73 | - if ($e) $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Tag transform', $original_name); |
|
74 | - } |
|
75 | - |
|
76 | - if (isset($definition->info[$token->name])) { |
|
77 | - |
|
78 | - // mostly everything's good, but |
|
79 | - // we need to make sure required attributes are in order |
|
80 | - if ( |
|
81 | - ($token instanceof HTMLPurifier_Token_Start || $token instanceof HTMLPurifier_Token_Empty) && |
|
82 | - $definition->info[$token->name]->required_attr && |
|
83 | - ($token->name != 'img' || $remove_invalid_img) // ensure config option still works |
|
84 | - ) { |
|
85 | - $attr_validator->validateToken($token, $config, $context); |
|
86 | - $ok = true; |
|
87 | - foreach ($definition->info[$token->name]->required_attr as $name) { |
|
88 | - if (!isset($token->attr[$name])) { |
|
89 | - $ok = false; |
|
90 | - break; |
|
91 | - } |
|
92 | - } |
|
93 | - if (!$ok) { |
|
94 | - if ($e) $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Missing required attribute', $name); |
|
95 | - continue; |
|
96 | - } |
|
97 | - $token->armor['ValidateAttributes'] = true; |
|
98 | - } |
|
99 | - |
|
100 | - if (isset($hidden_elements[$token->name]) && $token instanceof HTMLPurifier_Token_Start) { |
|
101 | - $textify_comments = $token->name; |
|
102 | - } elseif ($token->name === $textify_comments && $token instanceof HTMLPurifier_Token_End) { |
|
103 | - $textify_comments = false; |
|
104 | - } |
|
105 | - |
|
106 | - } elseif ($escape_invalid_tags) { |
|
107 | - // invalid tag, generate HTML representation and insert in |
|
108 | - if ($e) $e->send(E_WARNING, 'Strategy_RemoveForeignElements: Foreign element to text'); |
|
109 | - $token = new HTMLPurifier_Token_Text( |
|
110 | - $generator->generateFromToken($token) |
|
111 | - ); |
|
112 | - } else { |
|
113 | - // check if we need to destroy all of the tag's children |
|
114 | - // CAN BE GENERICIZED |
|
115 | - if (isset($hidden_elements[$token->name])) { |
|
116 | - if ($token instanceof HTMLPurifier_Token_Start) { |
|
117 | - $remove_until = $token->name; |
|
118 | - } elseif ($token instanceof HTMLPurifier_Token_Empty) { |
|
119 | - // do nothing: we're still looking |
|
120 | - } else { |
|
121 | - $remove_until = false; |
|
122 | - } |
|
123 | - if ($e) $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign meta element removed'); |
|
124 | - } else { |
|
125 | - if ($e) $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign element removed'); |
|
126 | - } |
|
127 | - continue; |
|
128 | - } |
|
129 | - } elseif ($token instanceof HTMLPurifier_Token_Comment) { |
|
130 | - // textify comments in script tags when they are allowed |
|
131 | - if ($textify_comments !== false) { |
|
132 | - $data = $token->data; |
|
133 | - $token = new HTMLPurifier_Token_Text($data); |
|
134 | - } elseif ($trusted || $check_comments) { |
|
135 | - // always cleanup comments |
|
136 | - $trailing_hyphen = false; |
|
137 | - if ($e) { |
|
138 | - // perform check whether or not there's a trailing hyphen |
|
139 | - if (substr($token->data, -1) == '-') { |
|
140 | - $trailing_hyphen = true; |
|
141 | - } |
|
142 | - } |
|
143 | - $token->data = rtrim($token->data, '-'); |
|
144 | - $found_double_hyphen = false; |
|
145 | - while (strpos($token->data, '--') !== false) { |
|
146 | - $found_double_hyphen = true; |
|
147 | - $token->data = str_replace('--', '-', $token->data); |
|
148 | - } |
|
149 | - if ($trusted || !empty($comment_lookup[trim($token->data)]) || ($comment_regexp !== NULL && preg_match($comment_regexp, trim($token->data)))) { |
|
150 | - // OK good |
|
151 | - if ($e) { |
|
152 | - if ($trailing_hyphen) { |
|
153 | - $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Trailing hyphen in comment removed'); |
|
154 | - } |
|
155 | - if ($found_double_hyphen) { |
|
156 | - $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Hyphens in comment collapsed'); |
|
157 | - } |
|
158 | - } |
|
159 | - } else { |
|
160 | - if ($e) { |
|
161 | - $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed'); |
|
162 | - } |
|
163 | - continue; |
|
164 | - } |
|
165 | - } else { |
|
166 | - // strip comments |
|
167 | - if ($e) $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed'); |
|
168 | - continue; |
|
169 | - } |
|
170 | - } elseif ($token instanceof HTMLPurifier_Token_Text) { |
|
171 | - } else { |
|
172 | - continue; |
|
173 | - } |
|
174 | - $result[] = $token; |
|
175 | - } |
|
176 | - if ($remove_until && $e) { |
|
177 | - // we removed tokens until the end, throw error |
|
178 | - $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Token removed to end', $remove_until); |
|
179 | - } |
|
180 | - |
|
181 | - $context->destroy('CurrentToken'); |
|
182 | - |
|
183 | - return $result; |
|
184 | - } |
|
14 | + public function execute($tokens, $config, $context) { |
|
15 | + $definition = $config->getHTMLDefinition(); |
|
16 | + $generator = new HTMLPurifier_Generator($config, $context); |
|
17 | + $result = array(); |
|
18 | + |
|
19 | + $escape_invalid_tags = $config->get('Core.EscapeInvalidTags'); |
|
20 | + $remove_invalid_img = $config->get('Core.RemoveInvalidImg'); |
|
21 | + |
|
22 | + // currently only used to determine if comments should be kept |
|
23 | + $trusted = $config->get('HTML.Trusted'); |
|
24 | + $comment_lookup = $config->get('HTML.AllowedComments'); |
|
25 | + $comment_regexp = $config->get('HTML.AllowedCommentsRegexp'); |
|
26 | + $check_comments = $comment_lookup !== array() || $comment_regexp !== null; |
|
27 | + |
|
28 | + $remove_script_contents = $config->get('Core.RemoveScriptContents'); |
|
29 | + $hidden_elements = $config->get('Core.HiddenElements'); |
|
30 | + |
|
31 | + // remove script contents compatibility |
|
32 | + if ($remove_script_contents === true) { |
|
33 | + $hidden_elements['script'] = true; |
|
34 | + } elseif ($remove_script_contents === false && isset($hidden_elements['script'])) { |
|
35 | + unset($hidden_elements['script']); |
|
36 | + } |
|
37 | + |
|
38 | + $attr_validator = new HTMLPurifier_AttrValidator(); |
|
39 | + |
|
40 | + // removes tokens until it reaches a closing tag with its value |
|
41 | + $remove_until = false; |
|
42 | + |
|
43 | + // converts comments into text tokens when this is equal to a tag name |
|
44 | + $textify_comments = false; |
|
45 | + |
|
46 | + $token = false; |
|
47 | + $context->register('CurrentToken', $token); |
|
48 | + |
|
49 | + $e = false; |
|
50 | + if ($config->get('Core.CollectErrors')) { |
|
51 | + $e =& $context->get('ErrorCollector'); |
|
52 | + } |
|
53 | + |
|
54 | + foreach($tokens as $token) { |
|
55 | + if ($remove_until) { |
|
56 | + if (empty($token->is_tag) || $token->name !== $remove_until) { |
|
57 | + continue; |
|
58 | + } |
|
59 | + } |
|
60 | + if (!empty( $token->is_tag )) { |
|
61 | + // DEFINITION CALL |
|
62 | + |
|
63 | + // before any processing, try to transform the element |
|
64 | + if ( |
|
65 | + isset($definition->info_tag_transform[$token->name]) |
|
66 | + ) { |
|
67 | + $original_name = $token->name; |
|
68 | + // there is a transformation for this tag |
|
69 | + // DEFINITION CALL |
|
70 | + $token = $definition-> |
|
71 | + info_tag_transform[$token->name]-> |
|
72 | + transform($token, $config, $context); |
|
73 | + if ($e) $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Tag transform', $original_name); |
|
74 | + } |
|
75 | + |
|
76 | + if (isset($definition->info[$token->name])) { |
|
77 | + |
|
78 | + // mostly everything's good, but |
|
79 | + // we need to make sure required attributes are in order |
|
80 | + if ( |
|
81 | + ($token instanceof HTMLPurifier_Token_Start || $token instanceof HTMLPurifier_Token_Empty) && |
|
82 | + $definition->info[$token->name]->required_attr && |
|
83 | + ($token->name != 'img' || $remove_invalid_img) // ensure config option still works |
|
84 | + ) { |
|
85 | + $attr_validator->validateToken($token, $config, $context); |
|
86 | + $ok = true; |
|
87 | + foreach ($definition->info[$token->name]->required_attr as $name) { |
|
88 | + if (!isset($token->attr[$name])) { |
|
89 | + $ok = false; |
|
90 | + break; |
|
91 | + } |
|
92 | + } |
|
93 | + if (!$ok) { |
|
94 | + if ($e) $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Missing required attribute', $name); |
|
95 | + continue; |
|
96 | + } |
|
97 | + $token->armor['ValidateAttributes'] = true; |
|
98 | + } |
|
99 | + |
|
100 | + if (isset($hidden_elements[$token->name]) && $token instanceof HTMLPurifier_Token_Start) { |
|
101 | + $textify_comments = $token->name; |
|
102 | + } elseif ($token->name === $textify_comments && $token instanceof HTMLPurifier_Token_End) { |
|
103 | + $textify_comments = false; |
|
104 | + } |
|
105 | + |
|
106 | + } elseif ($escape_invalid_tags) { |
|
107 | + // invalid tag, generate HTML representation and insert in |
|
108 | + if ($e) $e->send(E_WARNING, 'Strategy_RemoveForeignElements: Foreign element to text'); |
|
109 | + $token = new HTMLPurifier_Token_Text( |
|
110 | + $generator->generateFromToken($token) |
|
111 | + ); |
|
112 | + } else { |
|
113 | + // check if we need to destroy all of the tag's children |
|
114 | + // CAN BE GENERICIZED |
|
115 | + if (isset($hidden_elements[$token->name])) { |
|
116 | + if ($token instanceof HTMLPurifier_Token_Start) { |
|
117 | + $remove_until = $token->name; |
|
118 | + } elseif ($token instanceof HTMLPurifier_Token_Empty) { |
|
119 | + // do nothing: we're still looking |
|
120 | + } else { |
|
121 | + $remove_until = false; |
|
122 | + } |
|
123 | + if ($e) $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign meta element removed'); |
|
124 | + } else { |
|
125 | + if ($e) $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign element removed'); |
|
126 | + } |
|
127 | + continue; |
|
128 | + } |
|
129 | + } elseif ($token instanceof HTMLPurifier_Token_Comment) { |
|
130 | + // textify comments in script tags when they are allowed |
|
131 | + if ($textify_comments !== false) { |
|
132 | + $data = $token->data; |
|
133 | + $token = new HTMLPurifier_Token_Text($data); |
|
134 | + } elseif ($trusted || $check_comments) { |
|
135 | + // always cleanup comments |
|
136 | + $trailing_hyphen = false; |
|
137 | + if ($e) { |
|
138 | + // perform check whether or not there's a trailing hyphen |
|
139 | + if (substr($token->data, -1) == '-') { |
|
140 | + $trailing_hyphen = true; |
|
141 | + } |
|
142 | + } |
|
143 | + $token->data = rtrim($token->data, '-'); |
|
144 | + $found_double_hyphen = false; |
|
145 | + while (strpos($token->data, '--') !== false) { |
|
146 | + $found_double_hyphen = true; |
|
147 | + $token->data = str_replace('--', '-', $token->data); |
|
148 | + } |
|
149 | + if ($trusted || !empty($comment_lookup[trim($token->data)]) || ($comment_regexp !== NULL && preg_match($comment_regexp, trim($token->data)))) { |
|
150 | + // OK good |
|
151 | + if ($e) { |
|
152 | + if ($trailing_hyphen) { |
|
153 | + $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Trailing hyphen in comment removed'); |
|
154 | + } |
|
155 | + if ($found_double_hyphen) { |
|
156 | + $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Hyphens in comment collapsed'); |
|
157 | + } |
|
158 | + } |
|
159 | + } else { |
|
160 | + if ($e) { |
|
161 | + $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed'); |
|
162 | + } |
|
163 | + continue; |
|
164 | + } |
|
165 | + } else { |
|
166 | + // strip comments |
|
167 | + if ($e) $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed'); |
|
168 | + continue; |
|
169 | + } |
|
170 | + } elseif ($token instanceof HTMLPurifier_Token_Text) { |
|
171 | + } else { |
|
172 | + continue; |
|
173 | + } |
|
174 | + $result[] = $token; |
|
175 | + } |
|
176 | + if ($remove_until && $e) { |
|
177 | + // we removed tokens until the end, throw error |
|
178 | + $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Token removed to end', $remove_until); |
|
179 | + } |
|
180 | + |
|
181 | + $context->destroy('CurrentToken'); |
|
182 | + |
|
183 | + return $result; |
|
184 | + } |
|
185 | 185 | |
186 | 186 | } |
187 | 187 |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | $check_comments = $comment_lookup !== array() || $comment_regexp !== null; |
27 | 27 | |
28 | 28 | $remove_script_contents = $config->get('Core.RemoveScriptContents'); |
29 | - $hidden_elements = $config->get('Core.HiddenElements'); |
|
29 | + $hidden_elements = $config->get('Core.HiddenElements'); |
|
30 | 30 | |
31 | 31 | // remove script contents compatibility |
32 | 32 | if ($remove_script_contents === true) { |
@@ -48,16 +48,16 @@ discard block |
||
48 | 48 | |
49 | 49 | $e = false; |
50 | 50 | if ($config->get('Core.CollectErrors')) { |
51 | - $e =& $context->get('ErrorCollector'); |
|
51 | + $e = & $context->get('ErrorCollector'); |
|
52 | 52 | } |
53 | 53 | |
54 | - foreach($tokens as $token) { |
|
54 | + foreach ($tokens as $token) { |
|
55 | 55 | if ($remove_until) { |
56 | 56 | if (empty($token->is_tag) || $token->name !== $remove_until) { |
57 | 57 | continue; |
58 | 58 | } |
59 | 59 | } |
60 | - if (!empty( $token->is_tag )) { |
|
60 | + if (!empty($token->is_tag)) { |
|
61 | 61 | // DEFINITION CALL |
62 | 62 | |
63 | 63 | // before any processing, try to transform the element |
@@ -70,7 +70,9 @@ discard block |
||
70 | 70 | $token = $definition-> |
71 | 71 | info_tag_transform[$token->name]-> |
72 | 72 | transform($token, $config, $context); |
73 | - if ($e) $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Tag transform', $original_name); |
|
73 | + if ($e) { |
|
74 | + $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Tag transform', $original_name); |
|
75 | + } |
|
74 | 76 | } |
75 | 77 | |
76 | 78 | if (isset($definition->info[$token->name])) { |
@@ -91,7 +93,9 @@ discard block |
||
91 | 93 | } |
92 | 94 | } |
93 | 95 | if (!$ok) { |
94 | - if ($e) $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Missing required attribute', $name); |
|
96 | + if ($e) { |
|
97 | + $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Missing required attribute', $name); |
|
98 | + } |
|
95 | 99 | continue; |
96 | 100 | } |
97 | 101 | $token->armor['ValidateAttributes'] = true; |
@@ -105,7 +109,9 @@ discard block |
||
105 | 109 | |
106 | 110 | } elseif ($escape_invalid_tags) { |
107 | 111 | // invalid tag, generate HTML representation and insert in |
108 | - if ($e) $e->send(E_WARNING, 'Strategy_RemoveForeignElements: Foreign element to text'); |
|
112 | + if ($e) { |
|
113 | + $e->send(E_WARNING, 'Strategy_RemoveForeignElements: Foreign element to text'); |
|
114 | + } |
|
109 | 115 | $token = new HTMLPurifier_Token_Text( |
110 | 116 | $generator->generateFromToken($token) |
111 | 117 | ); |
@@ -120,9 +126,13 @@ discard block |
||
120 | 126 | } else { |
121 | 127 | $remove_until = false; |
122 | 128 | } |
123 | - if ($e) $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign meta element removed'); |
|
129 | + if ($e) { |
|
130 | + $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign meta element removed'); |
|
131 | + } |
|
124 | 132 | } else { |
125 | - if ($e) $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign element removed'); |
|
133 | + if ($e) { |
|
134 | + $e->send(E_ERROR, 'Strategy_RemoveForeignElements: Foreign element removed'); |
|
135 | + } |
|
126 | 136 | } |
127 | 137 | continue; |
128 | 138 | } |
@@ -164,7 +174,9 @@ discard block |
||
164 | 174 | } |
165 | 175 | } else { |
166 | 176 | // strip comments |
167 | - if ($e) $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed'); |
|
177 | + if ($e) { |
|
178 | + $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed'); |
|
179 | + } |
|
168 | 180 | continue; |
169 | 181 | } |
170 | 182 | } elseif ($token instanceof HTMLPurifier_Token_Text) { |
@@ -7,32 +7,32 @@ |
||
7 | 7 | class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy |
8 | 8 | { |
9 | 9 | |
10 | - public function execute($tokens, $config, $context) { |
|
10 | + public function execute($tokens, $config, $context) { |
|
11 | 11 | |
12 | - // setup validator |
|
13 | - $validator = new HTMLPurifier_AttrValidator(); |
|
12 | + // setup validator |
|
13 | + $validator = new HTMLPurifier_AttrValidator(); |
|
14 | 14 | |
15 | - $token = false; |
|
16 | - $context->register('CurrentToken', $token); |
|
15 | + $token = false; |
|
16 | + $context->register('CurrentToken', $token); |
|
17 | 17 | |
18 | - foreach ($tokens as $key => $token) { |
|
18 | + foreach ($tokens as $key => $token) { |
|
19 | 19 | |
20 | - // only process tokens that have attributes, |
|
21 | - // namely start and empty tags |
|
22 | - if (!$token instanceof HTMLPurifier_Token_Start && !$token instanceof HTMLPurifier_Token_Empty) continue; |
|
20 | + // only process tokens that have attributes, |
|
21 | + // namely start and empty tags |
|
22 | + if (!$token instanceof HTMLPurifier_Token_Start && !$token instanceof HTMLPurifier_Token_Empty) continue; |
|
23 | 23 | |
24 | - // skip tokens that are armored |
|
25 | - if (!empty($token->armor['ValidateAttributes'])) continue; |
|
24 | + // skip tokens that are armored |
|
25 | + if (!empty($token->armor['ValidateAttributes'])) continue; |
|
26 | 26 | |
27 | - // note that we have no facilities here for removing tokens |
|
28 | - $validator->validateToken($token, $config, $context); |
|
27 | + // note that we have no facilities here for removing tokens |
|
28 | + $validator->validateToken($token, $config, $context); |
|
29 | 29 | |
30 | - $tokens[$key] = $token; // for PHP 4 |
|
31 | - } |
|
32 | - $context->destroy('CurrentToken'); |
|
30 | + $tokens[$key] = $token; // for PHP 4 |
|
31 | + } |
|
32 | + $context->destroy('CurrentToken'); |
|
33 | 33 | |
34 | - return $tokens; |
|
35 | - } |
|
34 | + return $tokens; |
|
35 | + } |
|
36 | 36 | |
37 | 37 | } |
38 | 38 |
@@ -19,10 +19,14 @@ |
||
19 | 19 | |
20 | 20 | // only process tokens that have attributes, |
21 | 21 | // namely start and empty tags |
22 | - if (!$token instanceof HTMLPurifier_Token_Start && !$token instanceof HTMLPurifier_Token_Empty) continue; |
|
22 | + if (!$token instanceof HTMLPurifier_Token_Start && !$token instanceof HTMLPurifier_Token_Empty) { |
|
23 | + continue; |
|
24 | + } |
|
23 | 25 | |
24 | 26 | // skip tokens that are armored |
25 | - if (!empty($token->armor['ValidateAttributes'])) continue; |
|
27 | + if (!empty($token->armor['ValidateAttributes'])) { |
|
28 | + continue; |
|
29 | + } |
|
26 | 30 | |
27 | 31 | // note that we have no facilities here for removing tokens |
28 | 32 | $validator->validateToken($token, $config, $context); |
@@ -10,30 +10,30 @@ |
||
10 | 10 | */ |
11 | 11 | class HTMLPurifier_StringHash extends ArrayObject |
12 | 12 | { |
13 | - protected $accessed = array(); |
|
13 | + protected $accessed = array(); |
|
14 | 14 | |
15 | - /** |
|
16 | - * Retrieves a value, and logs the access. |
|
17 | - */ |
|
18 | - public function offsetGet($index) { |
|
19 | - $this->accessed[$index] = true; |
|
20 | - return parent::offsetGet($index); |
|
21 | - } |
|
15 | + /** |
|
16 | + * Retrieves a value, and logs the access. |
|
17 | + */ |
|
18 | + public function offsetGet($index) { |
|
19 | + $this->accessed[$index] = true; |
|
20 | + return parent::offsetGet($index); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * Returns a lookup array of all array indexes that have been accessed. |
|
25 | - * @return Array in form array($index => true). |
|
26 | - */ |
|
27 | - public function getAccessed() { |
|
28 | - return $this->accessed; |
|
29 | - } |
|
23 | + /** |
|
24 | + * Returns a lookup array of all array indexes that have been accessed. |
|
25 | + * @return Array in form array($index => true). |
|
26 | + */ |
|
27 | + public function getAccessed() { |
|
28 | + return $this->accessed; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * Resets the access array. |
|
33 | - */ |
|
34 | - public function resetAccessed() { |
|
35 | - $this->accessed = array(); |
|
36 | - } |
|
31 | + /** |
|
32 | + * Resets the access array. |
|
33 | + */ |
|
34 | + public function resetAccessed() { |
|
35 | + $this->accessed = array(); |
|
36 | + } |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | // vim: et sw=4 sts=4 |
@@ -28,82 +28,82 @@ |
||
28 | 28 | class HTMLPurifier_StringHashParser |
29 | 29 | { |
30 | 30 | |
31 | - public $default = 'ID'; |
|
31 | + public $default = 'ID'; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Parses a file that contains a single string-hash. |
|
35 | - */ |
|
36 | - public function parseFile($file) { |
|
37 | - if (!file_exists($file)) return false; |
|
38 | - $fh = fopen($file, 'r'); |
|
39 | - if (!$fh) return false; |
|
40 | - $ret = $this->parseHandle($fh); |
|
41 | - fclose($fh); |
|
42 | - return $ret; |
|
43 | - } |
|
33 | + /** |
|
34 | + * Parses a file that contains a single string-hash. |
|
35 | + */ |
|
36 | + public function parseFile($file) { |
|
37 | + if (!file_exists($file)) return false; |
|
38 | + $fh = fopen($file, 'r'); |
|
39 | + if (!$fh) return false; |
|
40 | + $ret = $this->parseHandle($fh); |
|
41 | + fclose($fh); |
|
42 | + return $ret; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Parses a file that contains multiple string-hashes delimited by '----' |
|
47 | - */ |
|
48 | - public function parseMultiFile($file) { |
|
49 | - if (!file_exists($file)) return false; |
|
50 | - $ret = array(); |
|
51 | - $fh = fopen($file, 'r'); |
|
52 | - if (!$fh) return false; |
|
53 | - while (!feof($fh)) { |
|
54 | - $ret[] = $this->parseHandle($fh); |
|
55 | - } |
|
56 | - fclose($fh); |
|
57 | - return $ret; |
|
58 | - } |
|
45 | + /** |
|
46 | + * Parses a file that contains multiple string-hashes delimited by '----' |
|
47 | + */ |
|
48 | + public function parseMultiFile($file) { |
|
49 | + if (!file_exists($file)) return false; |
|
50 | + $ret = array(); |
|
51 | + $fh = fopen($file, 'r'); |
|
52 | + if (!$fh) return false; |
|
53 | + while (!feof($fh)) { |
|
54 | + $ret[] = $this->parseHandle($fh); |
|
55 | + } |
|
56 | + fclose($fh); |
|
57 | + return $ret; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Internal parser that acepts a file handle. |
|
62 | - * @note While it's possible to simulate in-memory parsing by using |
|
63 | - * custom stream wrappers, if such a use-case arises we should |
|
64 | - * factor out the file handle into its own class. |
|
65 | - * @param $fh File handle with pointer at start of valid string-hash |
|
66 | - * block. |
|
67 | - */ |
|
68 | - protected function parseHandle($fh) { |
|
69 | - $state = false; |
|
70 | - $single = false; |
|
71 | - $ret = array(); |
|
72 | - do { |
|
73 | - $line = fgets($fh); |
|
74 | - if ($line === false) break; |
|
75 | - $line = rtrim($line, "\n\r"); |
|
76 | - if (!$state && $line === '') continue; |
|
77 | - if ($line === '----') break; |
|
78 | - if (strncmp('--#', $line, 3) === 0) { |
|
79 | - // Comment |
|
80 | - continue; |
|
81 | - } elseif (strncmp('--', $line, 2) === 0) { |
|
82 | - // Multiline declaration |
|
83 | - $state = trim($line, '- '); |
|
84 | - if (!isset($ret[$state])) $ret[$state] = ''; |
|
85 | - continue; |
|
86 | - } elseif (!$state) { |
|
87 | - $single = true; |
|
88 | - if (strpos($line, ':') !== false) { |
|
89 | - // Single-line declaration |
|
90 | - list($state, $line) = explode(':', $line, 2); |
|
91 | - $line = trim($line); |
|
92 | - } else { |
|
93 | - // Use default declaration |
|
94 | - $state = $this->default; |
|
95 | - } |
|
96 | - } |
|
97 | - if ($single) { |
|
98 | - $ret[$state] = $line; |
|
99 | - $single = false; |
|
100 | - $state = false; |
|
101 | - } else { |
|
102 | - $ret[$state] .= "$line\n"; |
|
103 | - } |
|
104 | - } while (!feof($fh)); |
|
105 | - return $ret; |
|
106 | - } |
|
60 | + /** |
|
61 | + * Internal parser that acepts a file handle. |
|
62 | + * @note While it's possible to simulate in-memory parsing by using |
|
63 | + * custom stream wrappers, if such a use-case arises we should |
|
64 | + * factor out the file handle into its own class. |
|
65 | + * @param $fh File handle with pointer at start of valid string-hash |
|
66 | + * block. |
|
67 | + */ |
|
68 | + protected function parseHandle($fh) { |
|
69 | + $state = false; |
|
70 | + $single = false; |
|
71 | + $ret = array(); |
|
72 | + do { |
|
73 | + $line = fgets($fh); |
|
74 | + if ($line === false) break; |
|
75 | + $line = rtrim($line, "\n\r"); |
|
76 | + if (!$state && $line === '') continue; |
|
77 | + if ($line === '----') break; |
|
78 | + if (strncmp('--#', $line, 3) === 0) { |
|
79 | + // Comment |
|
80 | + continue; |
|
81 | + } elseif (strncmp('--', $line, 2) === 0) { |
|
82 | + // Multiline declaration |
|
83 | + $state = trim($line, '- '); |
|
84 | + if (!isset($ret[$state])) $ret[$state] = ''; |
|
85 | + continue; |
|
86 | + } elseif (!$state) { |
|
87 | + $single = true; |
|
88 | + if (strpos($line, ':') !== false) { |
|
89 | + // Single-line declaration |
|
90 | + list($state, $line) = explode(':', $line, 2); |
|
91 | + $line = trim($line); |
|
92 | + } else { |
|
93 | + // Use default declaration |
|
94 | + $state = $this->default; |
|
95 | + } |
|
96 | + } |
|
97 | + if ($single) { |
|
98 | + $ret[$state] = $line; |
|
99 | + $single = false; |
|
100 | + $state = false; |
|
101 | + } else { |
|
102 | + $ret[$state] .= "$line\n"; |
|
103 | + } |
|
104 | + } while (!feof($fh)); |
|
105 | + return $ret; |
|
106 | + } |
|
107 | 107 | |
108 | 108 | } |
109 | 109 |
@@ -91,7 +91,7 @@ |
||
91 | 91 | $line = trim($line); |
92 | 92 | } else { |
93 | 93 | // Use default declaration |
94 | - $state = $this->default; |
|
94 | + $state = $this->default; |
|
95 | 95 | } |
96 | 96 | } |
97 | 97 | if ($single) { |
@@ -34,9 +34,13 @@ discard block |
||
34 | 34 | * Parses a file that contains a single string-hash. |
35 | 35 | */ |
36 | 36 | public function parseFile($file) { |
37 | - if (!file_exists($file)) return false; |
|
37 | + if (!file_exists($file)) { |
|
38 | + return false; |
|
39 | + } |
|
38 | 40 | $fh = fopen($file, 'r'); |
39 | - if (!$fh) return false; |
|
41 | + if (!$fh) { |
|
42 | + return false; |
|
43 | + } |
|
40 | 44 | $ret = $this->parseHandle($fh); |
41 | 45 | fclose($fh); |
42 | 46 | return $ret; |
@@ -46,10 +50,14 @@ discard block |
||
46 | 50 | * Parses a file that contains multiple string-hashes delimited by '----' |
47 | 51 | */ |
48 | 52 | public function parseMultiFile($file) { |
49 | - if (!file_exists($file)) return false; |
|
53 | + if (!file_exists($file)) { |
|
54 | + return false; |
|
55 | + } |
|
50 | 56 | $ret = array(); |
51 | 57 | $fh = fopen($file, 'r'); |
52 | - if (!$fh) return false; |
|
58 | + if (!$fh) { |
|
59 | + return false; |
|
60 | + } |
|
53 | 61 | while (!feof($fh)) { |
54 | 62 | $ret[] = $this->parseHandle($fh); |
55 | 63 | } |
@@ -71,17 +79,25 @@ discard block |
||
71 | 79 | $ret = array(); |
72 | 80 | do { |
73 | 81 | $line = fgets($fh); |
74 | - if ($line === false) break; |
|
82 | + if ($line === false) { |
|
83 | + break; |
|
84 | + } |
|
75 | 85 | $line = rtrim($line, "\n\r"); |
76 | - if (!$state && $line === '') continue; |
|
77 | - if ($line === '----') break; |
|
86 | + if (!$state && $line === '') { |
|
87 | + continue; |
|
88 | + } |
|
89 | + if ($line === '----') { |
|
90 | + break; |
|
91 | + } |
|
78 | 92 | if (strncmp('--#', $line, 3) === 0) { |
79 | 93 | // Comment |
80 | 94 | continue; |
81 | 95 | } elseif (strncmp('--', $line, 2) === 0) { |
82 | 96 | // Multiline declaration |
83 | 97 | $state = trim($line, '- '); |
84 | - if (!isset($ret[$state])) $ret[$state] = ''; |
|
98 | + if (!isset($ret[$state])) { |
|
99 | + $ret[$state] = ''; |
|
100 | + } |
|
85 | 101 | continue; |
86 | 102 | } elseif (!$state) { |
87 | 103 | $single = true; |
@@ -6,30 +6,30 @@ |
||
6 | 6 | abstract class HTMLPurifier_TagTransform |
7 | 7 | { |
8 | 8 | |
9 | - /** |
|
10 | - * Tag name to transform the tag to. |
|
11 | - */ |
|
12 | - public $transform_to; |
|
9 | + /** |
|
10 | + * Tag name to transform the tag to. |
|
11 | + */ |
|
12 | + public $transform_to; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Transforms the obsolete tag into the valid tag. |
|
16 | - * @param $tag Tag to be transformed. |
|
17 | - * @param $config Mandatory HTMLPurifier_Config object |
|
18 | - * @param $context Mandatory HTMLPurifier_Context object |
|
19 | - */ |
|
20 | - abstract public function transform($tag, $config, $context); |
|
14 | + /** |
|
15 | + * Transforms the obsolete tag into the valid tag. |
|
16 | + * @param $tag Tag to be transformed. |
|
17 | + * @param $config Mandatory HTMLPurifier_Config object |
|
18 | + * @param $context Mandatory HTMLPurifier_Context object |
|
19 | + */ |
|
20 | + abstract public function transform($tag, $config, $context); |
|
21 | 21 | |
22 | - /** |
|
23 | - * Prepends CSS properties to the style attribute, creating the |
|
24 | - * attribute if it doesn't exist. |
|
25 | - * @warning Copied over from AttrTransform, be sure to keep in sync |
|
26 | - * @param $attr Attribute array to process (passed by reference) |
|
27 | - * @param $css CSS to prepend |
|
28 | - */ |
|
29 | - protected function prependCSS(&$attr, $css) { |
|
30 | - $attr['style'] = isset($attr['style']) ? $attr['style'] : ''; |
|
31 | - $attr['style'] = $css . $attr['style']; |
|
32 | - } |
|
22 | + /** |
|
23 | + * Prepends CSS properties to the style attribute, creating the |
|
24 | + * attribute if it doesn't exist. |
|
25 | + * @warning Copied over from AttrTransform, be sure to keep in sync |
|
26 | + * @param $attr Attribute array to process (passed by reference) |
|
27 | + * @param $css CSS to prepend |
|
28 | + */ |
|
29 | + protected function prependCSS(&$attr, $css) { |
|
30 | + $attr['style'] = isset($attr['style']) ? $attr['style'] : ''; |
|
31 | + $attr['style'] = $css . $attr['style']; |
|
32 | + } |
|
33 | 33 | |
34 | 34 | } |
35 | 35 |
@@ -28,7 +28,7 @@ |
||
28 | 28 | */ |
29 | 29 | protected function prependCSS(&$attr, $css) { |
30 | 30 | $attr['style'] = isset($attr['style']) ? $attr['style'] : ''; |
31 | - $attr['style'] = $css . $attr['style']; |
|
31 | + $attr['style'] = $css.$attr['style']; |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | } |
@@ -18,81 +18,81 @@ |
||
18 | 18 | class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform |
19 | 19 | { |
20 | 20 | |
21 | - public $transform_to = 'span'; |
|
21 | + public $transform_to = 'span'; |
|
22 | 22 | |
23 | - protected $_size_lookup = array( |
|
24 | - '0' => 'xx-small', |
|
25 | - '1' => 'xx-small', |
|
26 | - '2' => 'small', |
|
27 | - '3' => 'medium', |
|
28 | - '4' => 'large', |
|
29 | - '5' => 'x-large', |
|
30 | - '6' => 'xx-large', |
|
31 | - '7' => '300%', |
|
32 | - '-1' => 'smaller', |
|
33 | - '-2' => '60%', |
|
34 | - '+1' => 'larger', |
|
35 | - '+2' => '150%', |
|
36 | - '+3' => '200%', |
|
37 | - '+4' => '300%' |
|
38 | - ); |
|
23 | + protected $_size_lookup = array( |
|
24 | + '0' => 'xx-small', |
|
25 | + '1' => 'xx-small', |
|
26 | + '2' => 'small', |
|
27 | + '3' => 'medium', |
|
28 | + '4' => 'large', |
|
29 | + '5' => 'x-large', |
|
30 | + '6' => 'xx-large', |
|
31 | + '7' => '300%', |
|
32 | + '-1' => 'smaller', |
|
33 | + '-2' => '60%', |
|
34 | + '+1' => 'larger', |
|
35 | + '+2' => '150%', |
|
36 | + '+3' => '200%', |
|
37 | + '+4' => '300%' |
|
38 | + ); |
|
39 | 39 | |
40 | - public function transform($tag, $config, $context) { |
|
40 | + public function transform($tag, $config, $context) { |
|
41 | 41 | |
42 | - if ($tag instanceof HTMLPurifier_Token_End) { |
|
43 | - $new_tag = clone $tag; |
|
44 | - $new_tag->name = $this->transform_to; |
|
45 | - return $new_tag; |
|
46 | - } |
|
42 | + if ($tag instanceof HTMLPurifier_Token_End) { |
|
43 | + $new_tag = clone $tag; |
|
44 | + $new_tag->name = $this->transform_to; |
|
45 | + return $new_tag; |
|
46 | + } |
|
47 | 47 | |
48 | - $attr = $tag->attr; |
|
49 | - $prepend_style = ''; |
|
48 | + $attr = $tag->attr; |
|
49 | + $prepend_style = ''; |
|
50 | 50 | |
51 | - // handle color transform |
|
52 | - if (isset($attr['color'])) { |
|
53 | - $prepend_style .= 'color:' . $attr['color'] . ';'; |
|
54 | - unset($attr['color']); |
|
55 | - } |
|
51 | + // handle color transform |
|
52 | + if (isset($attr['color'])) { |
|
53 | + $prepend_style .= 'color:' . $attr['color'] . ';'; |
|
54 | + unset($attr['color']); |
|
55 | + } |
|
56 | 56 | |
57 | - // handle face transform |
|
58 | - if (isset($attr['face'])) { |
|
59 | - $prepend_style .= 'font-family:' . $attr['face'] . ';'; |
|
60 | - unset($attr['face']); |
|
61 | - } |
|
57 | + // handle face transform |
|
58 | + if (isset($attr['face'])) { |
|
59 | + $prepend_style .= 'font-family:' . $attr['face'] . ';'; |
|
60 | + unset($attr['face']); |
|
61 | + } |
|
62 | 62 | |
63 | - // handle size transform |
|
64 | - if (isset($attr['size'])) { |
|
65 | - // normalize large numbers |
|
66 | - if ($attr['size'] !== '') { |
|
67 | - if ($attr['size']{0} == '+' || $attr['size']{0} == '-') { |
|
68 | - $size = (int) $attr['size']; |
|
69 | - if ($size < -2) $attr['size'] = '-2'; |
|
70 | - if ($size > 4) $attr['size'] = '+4'; |
|
71 | - } else { |
|
72 | - $size = (int) $attr['size']; |
|
73 | - if ($size > 7) $attr['size'] = '7'; |
|
74 | - } |
|
75 | - } |
|
76 | - if (isset($this->_size_lookup[$attr['size']])) { |
|
77 | - $prepend_style .= 'font-size:' . |
|
78 | - $this->_size_lookup[$attr['size']] . ';'; |
|
79 | - } |
|
80 | - unset($attr['size']); |
|
81 | - } |
|
63 | + // handle size transform |
|
64 | + if (isset($attr['size'])) { |
|
65 | + // normalize large numbers |
|
66 | + if ($attr['size'] !== '') { |
|
67 | + if ($attr['size']{0} == '+' || $attr['size']{0} == '-') { |
|
68 | + $size = (int) $attr['size']; |
|
69 | + if ($size < -2) $attr['size'] = '-2'; |
|
70 | + if ($size > 4) $attr['size'] = '+4'; |
|
71 | + } else { |
|
72 | + $size = (int) $attr['size']; |
|
73 | + if ($size > 7) $attr['size'] = '7'; |
|
74 | + } |
|
75 | + } |
|
76 | + if (isset($this->_size_lookup[$attr['size']])) { |
|
77 | + $prepend_style .= 'font-size:' . |
|
78 | + $this->_size_lookup[$attr['size']] . ';'; |
|
79 | + } |
|
80 | + unset($attr['size']); |
|
81 | + } |
|
82 | 82 | |
83 | - if ($prepend_style) { |
|
84 | - $attr['style'] = isset($attr['style']) ? |
|
85 | - $prepend_style . $attr['style'] : |
|
86 | - $prepend_style; |
|
87 | - } |
|
83 | + if ($prepend_style) { |
|
84 | + $attr['style'] = isset($attr['style']) ? |
|
85 | + $prepend_style . $attr['style'] : |
|
86 | + $prepend_style; |
|
87 | + } |
|
88 | 88 | |
89 | - $new_tag = clone $tag; |
|
90 | - $new_tag->name = $this->transform_to; |
|
91 | - $new_tag->attr = $attr; |
|
89 | + $new_tag = clone $tag; |
|
90 | + $new_tag->name = $this->transform_to; |
|
91 | + $new_tag->attr = $attr; |
|
92 | 92 | |
93 | - return $new_tag; |
|
93 | + return $new_tag; |
|
94 | 94 | |
95 | - } |
|
95 | + } |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | // vim: et sw=4 sts=4 |
@@ -66,11 +66,17 @@ |
||
66 | 66 | if ($attr['size'] !== '') { |
67 | 67 | if ($attr['size']{0} == '+' || $attr['size']{0} == '-') { |
68 | 68 | $size = (int) $attr['size']; |
69 | - if ($size < -2) $attr['size'] = '-2'; |
|
70 | - if ($size > 4) $attr['size'] = '+4'; |
|
69 | + if ($size < -2) { |
|
70 | + $attr['size'] = '-2'; |
|
71 | + } |
|
72 | + if ($size > 4) { |
|
73 | + $attr['size'] = '+4'; |
|
74 | + } |
|
71 | 75 | } else { |
72 | 76 | $size = (int) $attr['size']; |
73 | - if ($size > 7) $attr['size'] = '7'; |
|
77 | + if ($size > 7) { |
|
78 | + $attr['size'] = '7'; |
|
79 | + } |
|
74 | 80 | } |
75 | 81 | } |
76 | 82 | if (isset($this->_size_lookup[$attr['size']])) { |
@@ -50,13 +50,13 @@ discard block |
||
50 | 50 | |
51 | 51 | // handle color transform |
52 | 52 | if (isset($attr['color'])) { |
53 | - $prepend_style .= 'color:' . $attr['color'] . ';'; |
|
53 | + $prepend_style .= 'color:'.$attr['color'].';'; |
|
54 | 54 | unset($attr['color']); |
55 | 55 | } |
56 | 56 | |
57 | 57 | // handle face transform |
58 | 58 | if (isset($attr['face'])) { |
59 | - $prepend_style .= 'font-family:' . $attr['face'] . ';'; |
|
59 | + $prepend_style .= 'font-family:'.$attr['face'].';'; |
|
60 | 60 | unset($attr['face']); |
61 | 61 | } |
62 | 62 | |
@@ -74,16 +74,15 @@ discard block |
||
74 | 74 | } |
75 | 75 | } |
76 | 76 | if (isset($this->_size_lookup[$attr['size']])) { |
77 | - $prepend_style .= 'font-size:' . |
|
78 | - $this->_size_lookup[$attr['size']] . ';'; |
|
77 | + $prepend_style .= 'font-size:'. |
|
78 | + $this->_size_lookup[$attr['size']].';'; |
|
79 | 79 | } |
80 | 80 | unset($attr['size']); |
81 | 81 | } |
82 | 82 | |
83 | 83 | if ($prepend_style) { |
84 | 84 | $attr['style'] = isset($attr['style']) ? |
85 | - $prepend_style . $attr['style'] : |
|
86 | - $prepend_style; |
|
85 | + $prepend_style.$attr['style'] : $prepend_style; |
|
87 | 86 | } |
88 | 87 | |
89 | 88 | $new_tag = clone $tag; |
@@ -8,27 +8,27 @@ |
||
8 | 8 | class HTMLPurifier_TagTransform_Simple extends HTMLPurifier_TagTransform |
9 | 9 | { |
10 | 10 | |
11 | - protected $style; |
|
11 | + protected $style; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @param $transform_to Tag name to transform to. |
|
15 | - * @param $style CSS style to add to the tag |
|
16 | - */ |
|
17 | - public function __construct($transform_to, $style = null) { |
|
18 | - $this->transform_to = $transform_to; |
|
19 | - $this->style = $style; |
|
20 | - } |
|
13 | + /** |
|
14 | + * @param $transform_to Tag name to transform to. |
|
15 | + * @param $style CSS style to add to the tag |
|
16 | + */ |
|
17 | + public function __construct($transform_to, $style = null) { |
|
18 | + $this->transform_to = $transform_to; |
|
19 | + $this->style = $style; |
|
20 | + } |
|
21 | 21 | |
22 | - public function transform($tag, $config, $context) { |
|
23 | - $new_tag = clone $tag; |
|
24 | - $new_tag->name = $this->transform_to; |
|
25 | - if (!is_null($this->style) && |
|
26 | - ($new_tag instanceof HTMLPurifier_Token_Start || $new_tag instanceof HTMLPurifier_Token_Empty) |
|
27 | - ) { |
|
28 | - $this->prependCSS($new_tag->attr, $this->style); |
|
29 | - } |
|
30 | - return $new_tag; |
|
31 | - } |
|
22 | + public function transform($tag, $config, $context) { |
|
23 | + $new_tag = clone $tag; |
|
24 | + $new_tag->name = $this->transform_to; |
|
25 | + if (!is_null($this->style) && |
|
26 | + ($new_tag instanceof HTMLPurifier_Token_Start || $new_tag instanceof HTMLPurifier_Token_Empty) |
|
27 | + ) { |
|
28 | + $this->prependCSS($new_tag->attr, $this->style); |
|
29 | + } |
|
30 | + return $new_tag; |
|
31 | + } |
|
32 | 32 | |
33 | 33 | } |
34 | 34 |
@@ -4,53 +4,53 @@ |
||
4 | 4 | * Abstract base token class that all others inherit from. |
5 | 5 | */ |
6 | 6 | class HTMLPurifier_Token { |
7 | - public $line; /**< Line number node was on in source document. Null if unknown. */ |
|
8 | - public $col; /**< Column of line node was on in source document. Null if unknown. */ |
|
9 | - |
|
10 | - /** |
|
11 | - * Lookup array of processing that this token is exempt from. |
|
12 | - * Currently, valid values are "ValidateAttributes" and |
|
13 | - * "MakeWellFormed_TagClosedError" |
|
14 | - */ |
|
15 | - public $armor = array(); |
|
16 | - |
|
17 | - /** |
|
18 | - * Used during MakeWellFormed. |
|
19 | - */ |
|
20 | - public $skip; |
|
21 | - public $rewind; |
|
22 | - public $carryover; |
|
23 | - |
|
24 | - public function __get($n) { |
|
25 | - if ($n === 'type') { |
|
26 | - trigger_error('Deprecated type property called; use instanceof', E_USER_NOTICE); |
|
27 | - switch (get_class($this)) { |
|
28 | - case 'HTMLPurifier_Token_Start': return 'start'; |
|
29 | - case 'HTMLPurifier_Token_Empty': return 'empty'; |
|
30 | - case 'HTMLPurifier_Token_End': return 'end'; |
|
31 | - case 'HTMLPurifier_Token_Text': return 'text'; |
|
32 | - case 'HTMLPurifier_Token_Comment': return 'comment'; |
|
33 | - default: return null; |
|
34 | - } |
|
35 | - } |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Sets the position of the token in the source document. |
|
40 | - */ |
|
41 | - public function position($l = null, $c = null) { |
|
42 | - $this->line = $l; |
|
43 | - $this->col = $c; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Convenience function for DirectLex settings line/col position. |
|
48 | - */ |
|
49 | - public function rawPosition($l, $c) { |
|
50 | - if ($c === -1) $l++; |
|
51 | - $this->line = $l; |
|
52 | - $this->col = $c; |
|
53 | - } |
|
7 | + public $line; /**< Line number node was on in source document. Null if unknown. */ |
|
8 | + public $col; /**< Column of line node was on in source document. Null if unknown. */ |
|
9 | + |
|
10 | + /** |
|
11 | + * Lookup array of processing that this token is exempt from. |
|
12 | + * Currently, valid values are "ValidateAttributes" and |
|
13 | + * "MakeWellFormed_TagClosedError" |
|
14 | + */ |
|
15 | + public $armor = array(); |
|
16 | + |
|
17 | + /** |
|
18 | + * Used during MakeWellFormed. |
|
19 | + */ |
|
20 | + public $skip; |
|
21 | + public $rewind; |
|
22 | + public $carryover; |
|
23 | + |
|
24 | + public function __get($n) { |
|
25 | + if ($n === 'type') { |
|
26 | + trigger_error('Deprecated type property called; use instanceof', E_USER_NOTICE); |
|
27 | + switch (get_class($this)) { |
|
28 | + case 'HTMLPurifier_Token_Start': return 'start'; |
|
29 | + case 'HTMLPurifier_Token_Empty': return 'empty'; |
|
30 | + case 'HTMLPurifier_Token_End': return 'end'; |
|
31 | + case 'HTMLPurifier_Token_Text': return 'text'; |
|
32 | + case 'HTMLPurifier_Token_Comment': return 'comment'; |
|
33 | + default: return null; |
|
34 | + } |
|
35 | + } |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Sets the position of the token in the source document. |
|
40 | + */ |
|
41 | + public function position($l = null, $c = null) { |
|
42 | + $this->line = $l; |
|
43 | + $this->col = $c; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Convenience function for DirectLex settings line/col position. |
|
48 | + */ |
|
49 | + public function rawPosition($l, $c) { |
|
50 | + if ($c === -1) $l++; |
|
51 | + $this->line = $l; |
|
52 | + $this->col = $c; |
|
53 | + } |
|
54 | 54 | |
55 | 55 | } |
56 | 56 |
@@ -5,7 +5,7 @@ |
||
5 | 5 | */ |
6 | 6 | class HTMLPurifier_Token { |
7 | 7 | public $line; /**< Line number node was on in source document. Null if unknown. */ |
8 | - public $col; /**< Column of line node was on in source document. Null if unknown. */ |
|
8 | + public $col; /**< Column of line node was on in source document. Null if unknown. */ |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * Lookup array of processing that this token is exempt from. |
@@ -47,7 +47,9 @@ |
||
47 | 47 | * Convenience function for DirectLex settings line/col position. |
48 | 48 | */ |
49 | 49 | public function rawPosition($l, $c) { |
50 | - if ($c === -1) $l++; |
|
50 | + if ($c === -1) { |
|
51 | + $l++; |
|
52 | + } |
|
51 | 53 | $this->line = $l; |
52 | 54 | $this->col = $c; |
53 | 55 | } |