ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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  $this->attrValidator = new HTMLPurifier_AttrValidator();
50  }
51 
55  public function handleElement(&$token)
56  {
57  if (!$token instanceof HTMLPurifier_Token_Start) {
58  return;
59  }
60  $next = false;
61  $deleted = 1; // the current tag
62  for ($i = count($this->inputZipper->back) - 1; $i >= 0; $i--, $deleted++) {
63  $next = $this->inputZipper->back[$i];
64  if ($next instanceof HTMLPurifier_Token_Text) {
65  if ($next->is_whitespace) {
66  continue;
67  }
68  if ($this->removeNbsp && !isset($this->removeNbspExceptions[$token->name])) {
69  $plain = str_replace("\xC2\xA0", "", $next->data);
70  $isWsOrNbsp = $plain === '' || ctype_space($plain);
71  if ($isWsOrNbsp) {
72  continue;
73  }
74  }
75  }
76  break;
77  }
78  if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {
79  $this->attrValidator->validateToken($token, $this->config, $this->context);
80  $token->armor['ValidateAttributes'] = true;
81  if (isset($this->exclude[$token->name])) {
82  $r = true;
83  foreach ($this->exclude[$token->name] as $elem) {
84  if (!isset($token->attr[$elem])) $r = false;
85  }
86  if ($r) return;
87  }
88  if (isset($token->attr['id']) || isset($token->attr['name'])) {
89  return;
90  }
91  $token = $deleted + 1;
92  for ($b = 0, $c = count($this->inputZipper->front); $b < $c; $b++) {
93  $prev = $this->inputZipper->front[$b];
94  if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) {
95  continue;
96  }
97  break;
98  }
99  // This is safe because we removed the token that triggered this.
100  $this->rewindOffset($b+$deleted);
101  return;
102  }
103  }
104 }
105 
106 // 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
rewindOffset($offset)
Rewind to a spot to re-perform processing.
Definition: Injector.php:72
Concrete text token class.
Definition: Text.php:12