| 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: | * @type string |
| 12: | */ |
| 13: | protected $attr; |
| 14: | |
| 15: | /** |
| 16: | * Lookup array of attribute values to CSS. |
| 17: | * @type array |
| 18: | */ |
| 19: | protected $enumToCSS = array(); |
| 20: | |
| 21: | /** |
| 22: | * Case sensitivity of the matching. |
| 23: | * @type bool |
| 24: | * @warning Currently can only be guaranteed to work with ASCII |
| 25: | * values. |
| 26: | */ |
| 27: | protected $caseSensitive = false; |
| 28: | |
| 29: | /** |
| 30: | * @param string $attr Attribute name to transform from |
| 31: | * @param array $enum_to_css Lookup array of attribute values to CSS |
| 32: | * @param bool $case_sensitive Case sensitivity indicator, default false |
| 33: | */ |
| 34: | public function __construct($attr, $enum_to_css, $case_sensitive = false) |
| 35: | { |
| 36: | $this->attr = $attr; |
| 37: | $this->enumToCSS = $enum_to_css; |
| 38: | $this->caseSensitive = (bool)$case_sensitive; |
| 39: | } |
| 40: | |
| 41: | /** |
| 42: | * @param array $attr |
| 43: | * @param HTMLPurifier_Config $config |
| 44: | * @param HTMLPurifier_Context $context |
| 45: | * @return array |
| 46: | */ |
| 47: | public function transform($attr, $config, $context) |
| 48: | { |
| 49: | if (!isset($attr[$this->attr])) { |
| 50: | return $attr; |
| 51: | } |
| 52: | |
| 53: | $value = trim($attr[$this->attr]); |
| 54: | unset($attr[$this->attr]); |
| 55: | |
| 56: | if (!$this->caseSensitive) { |
| 57: | $value = strtolower($value); |
| 58: | } |
| 59: | |
| 60: | if (!isset($this->enumToCSS[$value])) { |
| 61: | return $attr; |
| 62: | } |
| 63: | $this->prependCSS($attr, $this->enumToCSS[$value]); |
| 64: | return $attr; |
| 65: | } |
| 66: | } |
| 67: | |
| 68: | // vim: et sw=4 sts=4 |
| 69: |