RemoveSpansWithoutAttributes.php 2.0 KB

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