ImgSpace.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Pre-transform that changes deprecated hspace and vspace attributes to CSS
  4. */
  5. class HTMLPurifier_AttrTransform_ImgSpace extends HTMLPurifier_AttrTransform
  6. {
  7. /**
  8. * @type string
  9. */
  10. protected $attr;
  11. /**
  12. * @type array
  13. */
  14. protected $css = array(
  15. 'hspace' => array('left', 'right'),
  16. 'vspace' => array('top', 'bottom')
  17. );
  18. /**
  19. * @param string $attr
  20. */
  21. public function __construct($attr)
  22. {
  23. $this->attr = $attr;
  24. if (!isset($this->css[$attr])) {
  25. trigger_error(htmlspecialchars($attr) . ' is not valid space attribute');
  26. }
  27. }
  28. /**
  29. * @param array $attr
  30. * @param HTMLPurifier_Config $config
  31. * @param HTMLPurifier_Context $context
  32. * @return array
  33. */
  34. public function transform($attr, $config, $context)
  35. {
  36. if (!isset($attr[$this->attr])) {
  37. return $attr;
  38. }
  39. $width = $this->confiscateAttr($attr, $this->attr);
  40. // some validation could happen here
  41. if (!isset($this->css[$this->attr])) {
  42. return $attr;
  43. }
  44. $style = '';
  45. foreach ($this->css[$this->attr] as $suffix) {
  46. $property = "margin-$suffix";
  47. $style .= "$property:{$width}px;";
  48. }
  49. $this->prependCSS($attr, $style);
  50. return $attr;
  51. }
  52. }
  53. // vim: et sw=4 sts=4