Fixing PREG_BACKTRACK_LIMIT_ERROR in HTMLPurifier_Filter_ExtractStyleBlocks
[htmlpurifier.git] / library / HTMLPurifier / AttrTransform / EnumToCSS.php
blob7ccd0e3fb7cf5b99458371df76ac78d0d77ef4b0
1 <?php
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
9 /**
10 * Name of attribute to transform from.
11 * @type string
13 protected $attr;
15 /**
16 * Lookup array of attribute values to CSS.
17 * @type array
19 protected $enumToCSS = array();
21 /**
22 * Case sensitivity of the matching.
23 * @type bool
24 * @warning Currently can only be guaranteed to work with ASCII
25 * values.
27 protected $caseSensitive = false;
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
34 public function __construct($attr, $enum_to_css, $case_sensitive = false)
36 $this->attr = $attr;
37 $this->enumToCSS = $enum_to_css;
38 $this->caseSensitive = (bool)$case_sensitive;
41 /**
42 * @param array $attr
43 * @param HTMLPurifier_Config $config
44 * @param HTMLPurifier_Context $context
45 * @return array
47 public function transform($attr, $config, $context)
49 if (!isset($attr[$this->attr])) {
50 return $attr;
53 $value = trim($attr[$this->attr]);
54 unset($attr[$this->attr]);
56 if (!$this->caseSensitive) {
57 $value = strtolower($value);
60 if (!isset($this->enumToCSS[$value])) {
61 return $attr;
63 $this->prependCSS($attr, $this->enumToCSS[$value]);
64 return $attr;
68 // vim: et sw=4 sts=4