RemoveEmpty.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
  3. {
  4. private $context, $config, $attrValidator, $removeNbsp, $removeNbspExceptions;
  5. // TODO: make me configurable
  6. private $_exclude = array('colgroup' => 1, 'th' => 1, 'td' => 1, 'iframe' => 1);
  7. public function prepare($config, $context) {
  8. parent::prepare($config, $context);
  9. $this->config = $config;
  10. $this->context = $context;
  11. $this->removeNbsp = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp');
  12. $this->removeNbspExceptions = $config->get('AutoFormat.RemoveEmpty.RemoveNbsp.Exceptions');
  13. $this->attrValidator = new HTMLPurifier_AttrValidator();
  14. }
  15. public function handleElement(&$token) {
  16. if (!$token instanceof HTMLPurifier_Token_Start) return;
  17. $next = false;
  18. for ($i = $this->inputIndex + 1, $c = count($this->inputTokens); $i < $c; $i++) {
  19. $next = $this->inputTokens[$i];
  20. if ($next instanceof HTMLPurifier_Token_Text) {
  21. if ($next->is_whitespace) continue;
  22. if ($this->removeNbsp && !isset($this->removeNbspExceptions[$token->name])) {
  23. $plain = str_replace("\xC2\xA0", "", $next->data);
  24. $isWsOrNbsp = $plain === '' || ctype_space($plain);
  25. if ($isWsOrNbsp) continue;
  26. }
  27. }
  28. break;
  29. }
  30. if (!$next || ($next instanceof HTMLPurifier_Token_End && $next->name == $token->name)) {
  31. if (isset($this->_exclude[$token->name])) return;
  32. $this->attrValidator->validateToken($token, $this->config, $this->context);
  33. $token->armor['ValidateAttributes'] = true;
  34. if (isset($token->attr['id']) || isset($token->attr['name'])) return;
  35. $token = $i - $this->inputIndex + 1;
  36. for ($b = $this->inputIndex - 1; $b > 0; $b--) {
  37. $prev = $this->inputTokens[$b];
  38. if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) continue;
  39. break;
  40. }
  41. // This is safe because we removed the token that triggered this.
  42. $this->rewind($b - 1);
  43. return;
  44. }
  45. }
  46. }
  47. // vim: et sw=4 sts=4