ImgSpace.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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