]>
Commit | Line | Data |
---|---|---|
d4949327 NL |
1 | <?php\r |
2 | \r | |
3 | class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector\r | |
4 | {\r | |
5 | /**\r | |
6 | * @type HTMLPurifier_Context\r | |
7 | */\r | |
8 | private $context;\r | |
9 | \r | |
10 | /**\r | |
11 | * @type HTMLPurifier_Config\r | |
12 | */\r | |
13 | private $config;\r | |
14 | \r | |
15 | /**\r | |
16 | * @type HTMLPurifier_AttrValidator\r | |
17 | */\r | |
18 | private $attrValidator;\r | |
19 | \r | |
20 | /**\r | |
21 | * @type bool\r | |
22 | */\r | |
23 | private $removeNbsp;\r | |
24 | \r | |
25 | /**\r | |
26 | * @type bool\r | |
27 | */\r | |
28 | private $removeNbspExceptions;\r | |
29 | \r | |
30 | /**\r | |
31 | * @type array\r | |
32 | * TODO: make me configurable\r | |
33 | */\r | |
34 | private $_exclude = array('colgroup' => 1, 'th' => 1, 'td' => 1, 'iframe' => 1);\r | |
35 | \r | |
36 | /**\r | |
37 | * @param HTMLPurifier_Config $config\r | |
38 | * @param HTMLPurifier_Context $context\r | |
39 | * @return void\r | |
40 | */\r | |
41 | public function prepare($config, $context)\r | |
42 | {\r | |
43 | parent::prepare($config, $context);\r | |
44 | $this->config = $config;\r | |
45 | $this->context = $context;\r | |
46 | $this->removeNbsp = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp');\r | |
47 | $this->removeNbspExceptions = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp.Exceptions');\r | |
48 | $this->attrValidator = new HTMLPurifier_AttrValidator();\r | |
49 | }\r | |
50 | \r | |
51 | /**\r | |
52 | * @param HTMLPurifier_Token $token\r | |
53 | */\r | |
54 | public function handleElement(&$token)\r | |
55 | {\r | |
56 | if (!$token instanceof HTMLPurifier_Token_Start) {\r | |
57 | return;\r | |
58 | }\r | |
59 | $next = false;\r | |
60 | $deleted = 1; // the current tag\r | |
61 | for ($i = count($this->inputZipper->back) - 1; $i >= 0; $i--, $deleted++) {\r | |
62 | $next = $this->inputZipper->back[$i];\r | |
63 | if ($next instanceof HTMLPurifier_Token_Text) {\r | |
64 | if ($next->is_whitespace) {\r | |
65 | continue;\r | |
66 | }\r | |
67 | if ($this->removeNbsp && !isset($this->removeNbspExceptions[$token->name])) {\r | |
68 | $plain = str_replace("\xC2\xA0", "", $next->data);\r | |
69 | $isWsOrNbsp = $plain === '' || ctype_space($plain);\r | |
70 | if ($isWsOrNbsp) {\r | |
71 | continue;\r | |
72 | }\r | |
73 | }\r | |
74 | }\r | |
75 | break;\r | |
76 | }\r | |
77 | if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {\r | |
78 | if (isset($this->_exclude[$token->name])) {\r | |
79 | return;\r | |
80 | }\r | |
81 | $this->attrValidator->validateToken($token, $this->config, $this->context);\r | |
82 | $token->armor['ValidateAttributes'] = true;\r | |
83 | if (isset($token->attr['id']) || isset($token->attr['name'])) {\r | |
84 | return;\r | |
85 | }\r | |
86 | $token = $deleted + 1;\r | |
87 | for ($b = 0, $c = count($this->inputZipper->front); $b < $c; $b++) {\r | |
88 | $prev = $this->inputZipper->front[$b];\r | |
89 | if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) {\r | |
90 | continue;\r | |
91 | }\r | |
92 | break;\r | |
93 | }\r | |
94 | // This is safe because we removed the token that triggered this.\r | |
95 | $this->rewindOffset($b+$deleted);\r | |
96 | return;\r | |
97 | }\r | |
98 | }\r | |
99 | }\r | |
100 | \r | |
101 | // vim: et sw=4 sts=4\r |