]> git.mxchange.org Git - friendica.git/blob - library/HTMLPurifier/Injector/RemoveEmpty.php
Merge pull request #482 from pixelroot/master
[friendica.git] / library / HTMLPurifier / Injector / RemoveEmpty.php
1 <?php
2
3 class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
4 {
5
6     private $context, $config, $attrValidator, $removeNbsp, $removeNbspExceptions;
7
8     public function prepare($config, $context) {
9         parent::prepare($config, $context);
10         $this->config = $config;
11         $this->context = $context;
12         $this->removeNbsp = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp');
13         $this->removeNbspExceptions = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp.Exceptions');
14         $this->attrValidator = new HTMLPurifier_AttrValidator();
15     }
16
17     public function handleElement(&$token) {
18         if (!$token instanceof HTMLPurifier_Token_Start) return;
19         $next = false;
20         for ($i = $this->inputIndex + 1, $c = count($this->inputTokens); $i < $c; $i++) {
21             $next = $this->inputTokens[$i];
22             if ($next instanceof HTMLPurifier_Token_Text) {
23                 if ($next->is_whitespace) continue;
24                 if ($this->removeNbsp && !isset($this->removeNbspExceptions[$token->name])) {
25                     $plain = str_replace("\xC2\xA0", "", $next->data);
26                     $isWsOrNbsp = $plain === '' || ctype_space($plain);
27                     if ($isWsOrNbsp) continue;
28                 }
29             }
30             break;
31         }
32         if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {
33             if ($token->name == 'colgroup') return;
34             $this->attrValidator->validateToken($token, $this->config, $this->context);
35             $token->armor['ValidateAttributes'] = true;
36             if (isset($token->attr['id']) || isset($token->attr['name'])) return;
37             $token = $i - $this->inputIndex + 1;
38             for ($b = $this->inputIndex - 1; $b > 0; $b--) {
39                 $prev = $this->inputTokens[$b];
40                 if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) continue;
41                 break;
42             }
43             // This is safe because we removed the token that triggered this.
44             $this->rewind($b - 1);
45             return;
46         }
47     }
48
49 }
50
51 // vim: et sw=4 sts=4