ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
RemoveEmpty.php
Go to the documentation of this file.
1 <?php
2 
4 {
5 
7 
8  // TODO: make me configurable
9  private $_exclude = array('colgroup' => 1, 'th' => 1, 'td' => 1, 'iframe' => 1);
10 
11  public function prepare($config, $context) {
13  $this->config = $config;
14  $this->context = $context;
15  $this->removeNbsp = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp');
16  $this->removeNbspExceptions = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp.Exceptions');
17  $this->attrValidator = new HTMLPurifier_AttrValidator();
18  }
19 
20  public function handleElement(&$token) {
21  if (!$token instanceof HTMLPurifier_Token_Start) return;
22  $next = false;
23  for ($i = $this->inputIndex + 1, $c = count($this->inputTokens); $i < $c; $i++) {
24  $next = $this->inputTokens[$i];
25  if ($next instanceof HTMLPurifier_Token_Text) {
26  if ($next->is_whitespace) continue;
27  if ($this->removeNbsp && !isset($this->removeNbspExceptions[$token->name])) {
28  $plain = str_replace("\xC2\xA0", "", $next->data);
29  $isWsOrNbsp = $plain === '' || ctype_space($plain);
30  if ($isWsOrNbsp) continue;
31  }
32  }
33  break;
34  }
35  if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {
36  if (isset($this->_exclude[$token->name])) return;
37  $this->attrValidator->validateToken($token, $this->config, $this->context);
38  $token->armor['ValidateAttributes'] = true;
39  if (isset($token->attr['id']) || isset($token->attr['name'])) return;
40  $token = $i - $this->inputIndex + 1;
41  for ($b = $this->inputIndex - 1; $b > 0; $b--) {
42  $prev = $this->inputTokens[$b];
43  if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) continue;
44  break;
45  }
46  // This is safe because we removed the token that triggered this.
47  $this->rewind($b - 1);
48  return;
49  }
50  }
51 
52 }
53 
54 // vim: et sw=4 sts=4