Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / HTML / Color.php
blobd6fa6749fb0d645e4b0a5d7d73d669a2c09c40fd
1 <?php
3 require_once 'HTMLPurifier/AttrDef.php';
4 require_once 'HTMLPurifier/AttrDef/CSS/Color.php'; // for %Core.ColorKeywords
6 /**
7 * Validates a color according to the HTML spec.
8 */
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);
22 else $hex = $string;
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];
29 return "#$hex";