ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
RemoveEmpty.php
Go to the documentation of this file.
1 <?php
2 
4 {
8  private $context;
9 
13  private $config;
14 
18  private $attrValidator;
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
Concrete end token class.
Definition: End.php:10
$context
HTMLPurifier_Context
Definition: RemoveEmpty.php:8
Validates the attributes of a token.
Concrete start token class.
Definition: Start.php:6
$r
Definition: example_031.php:79
$attrValidator
HTMLPurifier_AttrValidator
Definition: RemoveEmpty.php:18
Injects tokens into the document while parsing for well-formedness.
Definition: Injector.php:16
$exclude
Cached contents of AutoFormat.RemoveEmpty.Predicate array.
Definition: RemoveEmpty.php:34
$i
Definition: disco.tpl.php:19
rewindOffset($offset)
Rewind to a spot to re-perform processing.
Definition: Injector.php:72
Concrete text token class.
Definition: Text.php:12
$key
Definition: croninfo.php:18