Hack to fix #85
[htmlpurifier.git] / library / HTMLPurifier / Injector / RemoveEmpty.php
blob0ebc477c68e47c4b28d0d213985b06fc4550c96b
1 <?php
3 class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
5 /**
6 * @type HTMLPurifier_Context
7 */
8 private $context;
10 /**
11 * @type HTMLPurifier_Config
13 private $config;
15 /**
16 * @type HTMLPurifier_AttrValidator
18 private $attrValidator;
20 /**
21 * @type bool
23 private $removeNbsp;
25 /**
26 * @type bool
28 private $removeNbspExceptions;
30 /**
31 * Cached contents of %AutoFormat.RemoveEmpty.Predicate
32 * @type array
34 private $exclude;
36 /**
37 * @param HTMLPurifier_Config $config
38 * @param HTMLPurifier_Context $context
39 * @return void
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();
58 /**
59 * @param HTMLPurifier_Token $token
61 public function handleElement(&$token)
63 if (!$token instanceof HTMLPurifier_Token_Start) {
64 return;
66 $next = false;
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) {
72 continue;
74 if ($this->removeNbsp && !isset($this->removeNbspExceptions[$token->name])) {
75 $plain = str_replace("\xC2\xA0", "", $next->data);
76 $isWsOrNbsp = $plain === '' || ctype_space($plain);
77 if ($isWsOrNbsp) {
78 continue;
82 break;
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])) {
88 $r = true;
89 foreach ($this->exclude[$token->name] as $elem) {
90 if (!isset($token->attr[$elem])) $r = false;
92 if ($r) return;
94 if (isset($token->attr['id']) || isset($token->attr['name'])) {
95 return;
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) {
101 continue;
103 break;
105 // This is safe because we removed the token that triggered this.
106 $this->rewindOffset($b+$deleted);
107 return;
112 // vim: et sw=4 sts=4