4 * Validates Color as defined by CSS.
6 class HTMLPurifier_AttrDef_CSS_Color extends HTMLPurifier_AttrDef
10 * @param string $color
11 * @param HTMLPurifier_Config $config
12 * @param HTMLPurifier_Context $context
15 public function validate($color, $config, $context)
17 static $colors = null;
18 if ($colors === null) {
19 $colors = $config->get('Core.ColorKeywords');
22 $color = trim($color);
27 $lower = strtolower($color);
28 if (isset($colors[$lower])) {
29 return $colors[$lower];
32 if (strpos($color, 'rgb(') !== false) {
33 // rgb literal handling
34 $length = strlen($color);
35 if (strpos($color, ')') !== $length - 1) {
38 $triad = substr($color, 4, $length - 4 - 1);
39 $parts = explode(',', $triad);
40 if (count($parts) !== 3) {
43 $type = false; // to ensure that they're all the same type
45 foreach ($parts as $part) {
50 $length = strlen($part);
51 if ($part[$length - 1] === '%') {
55 } elseif ($type !== 'percentage') {
58 $num = (float)substr($part, 0, $length - 1);
65 $new_parts[] = "$num%";
70 } elseif ($type !== 'integer') {
80 $new_parts[] = (string)$num;
83 $new_triad = implode(',', $new_parts);
84 $color = "rgb($new_triad)";
86 // hexadecimal handling
87 if ($color[0] === '#') {
88 $hex = substr($color, 1);
91 $color = '#' . $color;
93 $length = strlen($hex);
94 if ($length !== 3 && $length !== 6) {
97 if (!ctype_xdigit($hex)) {
105 // vim: et sw=4 sts=4