3 class HTMLPurifier_Injector_RemoveEmpty
extends HTMLPurifier_Injector
6 * @type HTMLPurifier_Context
11 * @type HTMLPurifier_Config
16 * @type HTMLPurifier_AttrValidator
18 private $attrValidator;
28 private $removeNbspExceptions;
31 * Cached contents of %AutoFormat.RemoveEmpty.Predicate
37 * @param HTMLPurifier_Config $config
38 * @param HTMLPurifier_Context $context
41 public function prepare($config, $context)
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();
53 * @param HTMLPurifier_Token $token
55 public function handleElement(&$token)
57 if (!$token instanceof HTMLPurifier_Token_Start
) {
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
) {
68 if ($this->removeNbsp
&& !isset($this->removeNbspExceptions
[$token->name
])) {
69 $plain = str_replace("\xC2\xA0", "", $next->data
);
70 $isWsOrNbsp = $plain === '' ||
ctype_space($plain);
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
])) {
83 foreach ($this->exclude
[$token->name
] as $elem) {
84 if (!isset($token->attr
[$elem])) $r = false;
88 if (isset($token->attr
['id']) ||
isset($token->attr
['name'])) {
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
) {
99 // This is safe because we removed the token that triggered this.
100 $this->rewindOffset($b+
$deleted);
106 // vim: et sw=4 sts=4