PSR-2 reformatting PHPDoc corrections
[htmlpurifier.git] / library / HTMLPurifier / Injector / RemoveEmpty.php
blob32a45ede58b94b27ad17a5cadbe9c8ebcca77c0d
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 * @type array
32 * TODO: make me configurable
34 private $_exclude = array('colgroup' => 1, 'th' => 1, 'td' => 1, 'iframe' => 1);
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->attrValidator = new HTMLPurifier_AttrValidator();
51 /**
52 * @param HTMLPurifier_Token $token
54 public function handleElement(&$token)
56 if (!$token instanceof HTMLPurifier_Token_Start) {
57 return;
59 $next = false;
60 for ($i = $this->inputIndex + 1, $c = count($this->inputTokens); $i < $c; $i++) {
61 $next = $this->inputTokens[$i];
62 if ($next instanceof HTMLPurifier_Token_Text) {
63 if ($next->is_whitespace) {
64 continue;
66 if ($this->removeNbsp && !isset($this->removeNbspExceptions[$token->name])) {
67 $plain = str_replace("\xC2\xA0", "", $next->data);
68 $isWsOrNbsp = $plain === '' || ctype_space($plain);
69 if ($isWsOrNbsp) {
70 continue;
74 break;
76 if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {
77 if (isset($this->_exclude[$token->name])) {
78 return;
80 $this->attrValidator->validateToken($token, $this->config, $this->context);
81 $token->armor['ValidateAttributes'] = true;
82 if (isset($token->attr['id']) || isset($token->attr['name'])) {
83 return;
85 $token = $i - $this->inputIndex + 1;
86 for ($b = $this->inputIndex - 1; $b > 0; $b--) {
87 $prev = $this->inputTokens[$b];
88 if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) {
89 continue;
91 break;
93 // This is safe because we removed the token that triggered this.
94 $this->rewind($b - 1);
95 return;
100 // vim: et sw=4 sts=4