ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
RemoveEmpty.php
Go to the documentation of this file.
1<?php
2
4{
8 private $context;
9
13 private $config;
14
19
23 private $removeNbsp;
24
29
34 private $exclude;
35
41 public function prepare($config, $context)
42 {
43 parent::prepare($config, $context);
44 $this->config = $config;
45 $this->context = $context;
46 $this->removeNbsp = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp');
47 $this->removeNbspExceptions = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp.Exceptions');
48 $this->exclude = $config->get('AutoFormat.RemoveEmpty.Predicate');
49 foreach ($this->exclude as $key => $attrs) {
50 if (!is_array($attrs)) {
51 // HACK, see HTMLPurifier/Printer/ConfigForm.php
52 $this->exclude[$key] = explode(';', $attrs);
53 }
54 }
55 $this->attrValidator = new HTMLPurifier_AttrValidator();
56 }
57
61 public function handleElement(&$token)
62 {
63 if (!$token instanceof HTMLPurifier_Token_Start) {
64 return;
65 }
66 $next = false;
67 $deleted = 1; // the current tag
68 for ($i = count($this->inputZipper->back) - 1; $i >= 0; $i--, $deleted++) {
69 $next = $this->inputZipper->back[$i];
70 if ($next instanceof HTMLPurifier_Token_Text) {
71 if ($next->is_whitespace) {
72 continue;
73 }
74 if ($this->removeNbsp && !isset($this->removeNbspExceptions[$token->name])) {
75 $plain = str_replace("\xC2\xA0", "", $next->data);
76 $isWsOrNbsp = $plain === '' || ctype_space($plain);
77 if ($isWsOrNbsp) {
78 continue;
79 }
80 }
81 }
82 break;
83 }
84 if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {
85 $this->attrValidator->validateToken($token, $this->config, $this->context);
86 $token->armor['ValidateAttributes'] = true;
87 if (isset($this->exclude[$token->name])) {
88 $r = true;
89 foreach ($this->exclude[$token->name] as $elem) {
90 if (!isset($token->attr[$elem])) $r = false;
91 }
92 if ($r) return;
93 }
94 if (isset($token->attr['id']) || isset($token->attr['name'])) {
95 return;
96 }
97 $token = $deleted + 1;
98 for ($b = 0, $c = count($this->inputZipper->front); $b < $c; $b++) {
99 $prev = $this->inputZipper->front[$b];
100 if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) {
101 continue;
102 }
103 break;
104 }
105 // This is safe because we removed the token that triggered this.
106 $this->rewindOffset($b+$deleted);
107 return;
108 }
109 }
110}
111
112// vim: et sw=4 sts=4
An exception for terminatinating execution or to throw for unit testing.
Validates the attributes of a token.
$exclude
Cached contents of AutoFormat.RemoveEmpty.Predicate @type array.
Definition: RemoveEmpty.php:34
$attrValidator
@type HTMLPurifier_AttrValidator
Definition: RemoveEmpty.php:18
$config
@type HTMLPurifier_Config
Definition: RemoveEmpty.php:13
$context
@type HTMLPurifier_Context
Definition: RemoveEmpty.php:8
Injects tokens into the document while parsing for well-formedness.
Definition: Injector.php:17
rewindOffset($offset)
Rewind to a spot to re-perform processing.
Definition: Injector.php:72
Concrete end token class.
Definition: End.php:11
Concrete start token class.
Definition: Start.php:7
Concrete text token class.
Definition: Text.php:13
$key
Definition: croninfo.php:18
$i
Definition: disco.tpl.php:19
$r
Definition: example_031.php:79