File indexing completed on 2024-05-12 06:02:04

0001 <?php
0002 
0003 /**
0004  * Pre-transform that changes deprecated hspace and vspace attributes to CSS
0005  */
0006 class HTMLPurifier_AttrTransform_ImgSpace extends HTMLPurifier_AttrTransform
0007 {
0008     /**
0009      * @type string
0010      */
0011     protected $attr;
0012 
0013     /**
0014      * @type array
0015      */
0016     protected $css = array(
0017         'hspace' => array('left', 'right'),
0018         'vspace' => array('top', 'bottom')
0019     );
0020 
0021     /**
0022      * @param string $attr
0023      */
0024     public function __construct($attr)
0025     {
0026         $this->attr = $attr;
0027         if (!isset($this->css[$attr])) {
0028             trigger_error(htmlspecialchars($attr) . ' is not valid space attribute');
0029         }
0030     }
0031 
0032     /**
0033      * @param array $attr
0034      * @param HTMLPurifier_Config $config
0035      * @param HTMLPurifier_Context $context
0036      * @return array
0037      */
0038     public function transform($attr, $config, $context)
0039     {
0040         if (!isset($attr[$this->attr])) {
0041             return $attr;
0042         }
0043 
0044         $width = $this->confiscateAttr($attr, $this->attr);
0045         // some validation could happen here
0046 
0047         if (!isset($this->css[$this->attr])) {
0048             return $attr;
0049         }
0050 
0051         $style = '';
0052         foreach ($this->css[$this->attr] as $suffix) {
0053             $property = "margin-$suffix";
0054             $style .= "$property:{$width}px;";
0055         }
0056         $this->prependCSS($attr, $style);
0057         return $attr;
0058     }
0059 }
0060 
0061 // vim: et sw=4 sts=4