ImgSpace.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. protected $attr;
  7. protected $css = array(
  8. 'hspace' => array('left', 'right'),
  9. 'vspace' => array('top', 'bottom')
  10. );
  11. public function __construct($attr) {
  12. $this->attr = $attr;
  13. if (!isset($this->css[$attr])) {
  14. trigger_error(htmlspecialchars($attr) . ' is not valid space attribute');
  15. }
  16. }
  17. public function transform($attr, $config, $context) {
  18. if (!isset($attr[$this->attr])) return $attr;
  19. $width = $this->confiscateAttr($attr, $this->attr);
  20. // some validation could happen here
  21. if (!isset($this->css[$this->attr])) return $attr;
  22. $style = '';
  23. foreach ($this->css[$this->attr] as $suffix) {
  24. $property = "margin-$suffix";
  25. $style .= "$property:{$width}px;";
  26. }
  27. $this->prependCSS($attr, $style);
  28. return $attr;
  29. }
  30. }
  31. // vim: et sw=4 sts=4