RemoveSpansWithoutAttributes.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Injector that removes spans with no attributes
  4. */
  5. class HTMLPurifier_Injector_RemoveSpansWithoutAttributes extends HTMLPurifier_Injector
  6. {
  7. public $name = 'RemoveSpansWithoutAttributes';
  8. public $needed = array('span');
  9. private $attrValidator;
  10. /**
  11. * Used by AttrValidator
  12. */
  13. private $config;
  14. private $context;
  15. public function prepare($config, $context) {
  16. $this->attrValidator = new HTMLPurifier_AttrValidator();
  17. $this->config = $config;
  18. $this->context = $context;
  19. return parent::prepare($config, $context);
  20. }
  21. public function handleElement(&$token) {
  22. if ($token->name !== 'span' || !$token instanceof HTMLPurifier_Token_Start) {
  23. return;
  24. }
  25. // We need to validate the attributes now since this doesn't normally
  26. // happen until after MakeWellFormed. If all the attributes are removed
  27. // the span needs to be removed too.
  28. $this->attrValidator->validateToken($token, $this->config, $this->context);
  29. $token->armor['ValidateAttributes'] = true;
  30. if (!empty($token->attr)) {
  31. return;
  32. }
  33. $nesting = 0;
  34. $spanContentTokens = array();
  35. while ($this->forwardUntilEndToken($i, $current, $nesting)) {}
  36. if ($current instanceof HTMLPurifier_Token_End && $current->name === 'span') {
  37. // Mark closing span tag for deletion
  38. $current->markForDeletion = true;
  39. // Delete open span tag
  40. $token = false;
  41. }
  42. }
  43. public function handleEnd(&$token) {
  44. if ($token->markForDeletion) {
  45. $token = false;
  46. }
  47. }
  48. }
  49. // vim: et sw=4 sts=4