3 require_once 'HTMLPurifier/AttrDef.php';
5 HTMLPurifier_ConfigSchema
::define(
6 'Core', 'ColorKeywords', array(
10 'yellow' => '#FFFF00',
12 'purple' => '#800080',
13 'fuchsia' => '#FF00FF',
22 'silver' => '#C0C0C0',
25 Lookup array of color names to six digit hexadecimal number corresponding
26 to color, with preceding hash mark. Used when parsing colors.
27 This directive has been available since 2.0.0.
31 * Validates Color as defined by CSS.
33 class HTMLPurifier_AttrDef_CSS_Color
extends HTMLPurifier_AttrDef
36 function validate($color, $config, &$context) {
38 static $colors = null;
39 if ($colors === null) $colors = $config->get('Core', 'ColorKeywords');
41 $color = trim($color);
42 if ($color === '') return false;
44 $lower = strtolower($color);
45 if (isset($colors[$lower])) return $colors[$lower];
47 if (strpos($color, 'rgb(') !== false) {
48 // rgb literal handling
49 $length = strlen($color);
50 if (strpos($color, ')') !== $length - 1) return false;
51 $triad = substr($color, 4, $length - 4 - 1);
52 $parts = explode(',', $triad);
53 if (count($parts) !== 3) return false;
54 $type = false; // to ensure that they're all the same type
56 foreach ($parts as $part) {
58 if ($part === '') return false;
59 $length = strlen($part);
60 if ($part[$length - 1] === '%') {
64 } elseif ($type !== 'percentage') {
67 $num = (float) substr($part, 0, $length - 1);
68 if ($num < 0) $num = 0;
69 if ($num > 100) $num = 100;
70 $new_parts[] = "$num%";
75 } elseif ($type !== 'integer') {
79 if ($num < 0) $num = 0;
80 if ($num > 255) $num = 255;
81 $new_parts[] = (string) $num;
84 $new_triad = implode(',', $new_parts);
85 $color = "rgb($new_triad)";
87 // hexadecimal handling
88 if ($color[0] === '#') {
89 $hex = substr($color, 1);
92 $color = '#' . $color;
94 $length = strlen($hex);
95 if ($length !== 3 && $length !== 6) return false;
96 if (!ctype_xdigit($hex)) return false;