File indexing completed on 2024-12-22 05:36:19

0001 <?php
0002 
0003 /**
0004  * Generic pre-transform that converts an attribute with a fixed number of
0005  * values (enumerated) to CSS.
0006  */
0007 class HTMLPurifier_AttrTransform_EnumToCSS extends HTMLPurifier_AttrTransform
0008 {
0009     /**
0010      * Name of attribute to transform from.
0011      * @type string
0012      */
0013     protected $attr;
0014 
0015     /**
0016      * Lookup array of attribute values to CSS.
0017      * @type array
0018      */
0019     protected $enumToCSS = array();
0020 
0021     /**
0022      * Case sensitivity of the matching.
0023      * @type bool
0024      * @warning Currently can only be guaranteed to work with ASCII
0025      *          values.
0026      */
0027     protected $caseSensitive = false;
0028 
0029     /**
0030      * @param string $attr Attribute name to transform from
0031      * @param array $enum_to_css Lookup array of attribute values to CSS
0032      * @param bool $case_sensitive Case sensitivity indicator, default false
0033      */
0034     public function __construct($attr, $enum_to_css, $case_sensitive = false)
0035     {
0036         $this->attr = $attr;
0037         $this->enumToCSS = $enum_to_css;
0038         $this->caseSensitive = (bool)$case_sensitive;
0039     }
0040 
0041     /**
0042      * @param array $attr
0043      * @param HTMLPurifier_Config $config
0044      * @param HTMLPurifier_Context $context
0045      * @return array
0046      */
0047     public function transform($attr, $config, $context)
0048     {
0049         if (!isset($attr[$this->attr])) {
0050             return $attr;
0051         }
0052 
0053         $value = trim($attr[$this->attr]);
0054         unset($attr[$this->attr]);
0055 
0056         if (!$this->caseSensitive) {
0057             $value = strtolower($value);
0058         }
0059 
0060         if (!isset($this->enumToCSS[$value])) {
0061             return $attr;
0062         }
0063         $this->prependCSS($attr, $this->enumToCSS[$value]);
0064         return $attr;
0065     }
0066 }
0067 
0068 // vim: et sw=4 sts=4