Implement %AutoFormat.RemoveEmpty.RemoveNbsp, by popular demand.
[htmlpurifier/darkodev.git] / library / HTMLPurifier / Injector / RemoveEmpty.php
blob638bfca03b251b5a30ffcabf297f6addede383b3
1 <?php
3 class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
6 private $context, $config, $attrValidator, $removeNbsp, $removeNbspExceptions;
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();
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;
30 break;
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;
43 // This is safe because we removed the token that triggered this.
44 $this->rewind($b - 1);
45 return;
51 // vim: et sw=4 sts=4