]>
Commit | Line | Data |
---|---|---|
d4949327 NL |
1 | <?php\r |
2 | \r | |
3 | /**\r | |
4 | * Validates a color according to the HTML spec.\r | |
5 | */\r | |
6 | class HTMLPurifier_AttrDef_HTML_Color extends HTMLPurifier_AttrDef\r | |
7 | {\r | |
8 | \r | |
9 | /**\r | |
10 | * @param string $string\r | |
11 | * @param HTMLPurifier_Config $config\r | |
12 | * @param HTMLPurifier_Context $context\r | |
13 | * @return bool|string\r | |
14 | */\r | |
15 | public function validate($string, $config, $context)\r | |
16 | {\r | |
17 | static $colors = null;\r | |
18 | if ($colors === null) {\r | |
19 | $colors = $config->get('Core.ColorKeywords');\r | |
20 | }\r | |
21 | \r | |
22 | $string = trim($string);\r | |
23 | \r | |
24 | if (empty($string)) {\r | |
25 | return false;\r | |
26 | }\r | |
27 | $lower = strtolower($string);\r | |
28 | if (isset($colors[$lower])) {\r | |
29 | return $colors[$lower];\r | |
30 | }\r | |
31 | if ($string[0] === '#') {\r | |
32 | $hex = substr($string, 1);\r | |
33 | } else {\r | |
34 | $hex = $string;\r | |
35 | }\r | |
36 | \r | |
37 | $length = strlen($hex);\r | |
38 | if ($length !== 3 && $length !== 6) {\r | |
39 | return false;\r | |
40 | }\r | |
41 | if (!ctype_xdigit($hex)) {\r | |
42 | return false;\r | |
43 | }\r | |
44 | if ($length === 3) {\r | |
45 | $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];\r | |
46 | }\r | |
47 | return "#$hex";\r | |
48 | }\r | |
49 | }\r | |
50 | \r | |
51 | // vim: et sw=4 sts=4\r |