PSR-2 reformatting PHPDoc corrections
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / HTML / Color.php
blob946ebb7820140f8c01bb28edad9e629d420c5dac
1 <?php
3 /**
4 * Validates a color according to the HTML spec.
5 */
6 class HTMLPurifier_AttrDef_HTML_Color extends HTMLPurifier_AttrDef
9 /**
10 * @param string $string
11 * @param HTMLPurifier_Config $config
12 * @param HTMLPurifier_Context $context
13 * @return bool|string
15 public function validate($string, $config, $context)
17 static $colors = null;
18 if ($colors === null) {
19 $colors = $config->get('Core.ColorKeywords');
22 $string = trim($string);
24 if (empty($string)) {
25 return false;
27 $lower = strtolower($string);
28 if (isset($colors[$lower])) {
29 return $colors[$lower];
31 if ($string[0] === '#') {
32 $hex = substr($string, 1);
33 } else {
34 $hex = $string;
37 $length = strlen($hex);
38 if ($length !== 3 && $length !== 6) {
39 return false;
41 if (!ctype_xdigit($hex)) {
42 return false;
44 if ($length === 3) {
45 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
47 return "#$hex";
51 // vim: et sw=4 sts=4