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