| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Generic pre-transform that converts an attribute with a fixed number of |
|---|
| 5 | * values (enumerated) to CSS. |
|---|
| 6 | */ |
|---|
| 7 | class HTMLPurifier_AttrTransform_EnumToCSS extends HTMLPurifier_AttrTransform { |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * Name of attribute to transform from |
|---|
| 11 | */ |
|---|
| 12 | protected $attr; |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * Lookup array of attribute values to CSS |
|---|
| 16 | */ |
|---|
| 17 | protected $enumToCSS = array(); |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * Case sensitivity of the matching |
|---|
| 21 | * @warning Currently can only be guaranteed to work with ASCII |
|---|
| 22 | * values. |
|---|
| 23 | */ |
|---|
| 24 | protected $caseSensitive = false; |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * @param $attr String attribute name to transform from |
|---|
| 28 | * @param $enumToCSS Lookup array of attribute values to CSS |
|---|
| 29 | * @param $case_sensitive Boolean case sensitivity indicator, default false |
|---|
| 30 | */ |
|---|
| 31 | public function __construct($attr, $enum_to_css, $case_sensitive = false) { |
|---|
| 32 | $this->attr = $attr; |
|---|
| 33 | $this->enumToCSS = $enum_to_css; |
|---|
| 34 | $this->caseSensitive = (bool) $case_sensitive; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | public function transform($attr, $config, $context) { |
|---|
| 38 | |
|---|
| 39 | if (!isset($attr[$this->attr])) return $attr; |
|---|
| 40 | |
|---|
| 41 | $value = trim($attr[$this->attr]); |
|---|
| 42 | unset($attr[$this->attr]); |
|---|
| 43 | |
|---|
| 44 | if (!$this->caseSensitive) $value = strtolower($value); |
|---|
| 45 | |
|---|
| 46 | if (!isset($this->enumToCSS[$value])) { |
|---|
| 47 | return $attr; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | $this->prependCSS($attr, $this->enumToCSS[$value]); |
|---|
| 51 | |
|---|
| 52 | return $attr; |
|---|
| 53 | |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | } |
|---|
| 57 | |
|---|