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 foreach ($this->exclude
as $key => $attrs) {
50 if (!is_array($attrs)) {
51 // HACK, see HTMLPurifier/Printer/ConfigForm.php
52 $this->exclude
[$key] = explode(';', $attrs);
55 $this->attrValidator
= new HTMLPurifier_AttrValidator();
59 * @param HTMLPurifier_Token $token
61 public function handleElement(&$token)
63 if (!$token instanceof HTMLPurifier_Token_Start
) {
67 $deleted = 1; // the current tag
68 for ($i = count($this->inputZipper
->back
) - 1; $i >= 0; $i--, $deleted++
) {
69 $next = $this->inputZipper
->back
[$i];
70 if ($next instanceof HTMLPurifier_Token_Text
) {
71 if ($next->is_whitespace
) {
74 if ($this->removeNbsp
&& !isset($this->removeNbspExceptions
[$token->name
])) {
75 $plain = str_replace("\xC2\xA0", "", $next->data
);
76 $isWsOrNbsp = $plain === '' ||
ctype_space($plain);
84 if (!$next ||
($next instanceof HTMLPurifier_Token_End
&& $next->name
== $token->name
)) {
85 $this->attrValidator
->validateToken($token, $this->config
, $this->context
);
86 $token->armor
['ValidateAttributes'] = true;
87 if (isset($this->exclude
[$token->name
])) {
89 foreach ($this->exclude
[$token->name
] as $elem) {
90 if (!isset($token->attr
[$elem])) $r = false;
94 if (isset($token->attr
['id']) ||
isset($token->attr
['name'])) {
97 $token = $deleted +
1;
98 for ($b = 0, $c = count($this->inputZipper
->front
); $b < $c; $b++
) {
99 $prev = $this->inputZipper
->front
[$b];
100 if ($prev instanceof HTMLPurifier_Token_Text
&& $prev->is_whitespace
) {
105 // This is safe because we removed the token that triggered this.
106 $this->rewindOffset($b+
$deleted);
112 // vim: et sw=4 sts=4