3 require_once 'HTMLPurifier/AttrDef.php';
4 require_once 'HTMLPurifier/AttrDef/CSS/Color.php'; // for %Core.ColorKeywords
7 * Validates a color according to the HTML spec.
9 class HTMLPurifier_AttrDef_HTML_Color
extends HTMLPurifier_AttrDef
12 function validate($string, $config, &$context) {
14 static $colors = null;
15 if ($colors === null) $colors = $config->get('Core', 'ColorKeywords');
17 $string = trim($string);
19 if (empty($string)) return false;
20 if (isset($colors[$string])) return $colors[$string];
21 if ($string[0] === '#') $hex = substr($string, 1);
24 $length = strlen($hex);
25 if ($length !== 3 && $length !== 6) return false;
26 if (!ctype_xdigit($hex)) return false;
27 if ($length === 3) $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];