File indexing completed on 2024-12-22 05:36:18
0001 <?php 0002 0003 /** 0004 * Validates a color according to the HTML spec. 0005 */ 0006 class HTMLPurifier_AttrDef_HTML_Color extends HTMLPurifier_AttrDef 0007 { 0008 0009 /** 0010 * @param string $string 0011 * @param HTMLPurifier_Config $config 0012 * @param HTMLPurifier_Context $context 0013 * @return bool|string 0014 */ 0015 public function validate($string, $config, $context) 0016 { 0017 static $colors = null; 0018 if ($colors === null) { 0019 $colors = $config->get('Core.ColorKeywords'); 0020 } 0021 0022 $string = trim($string); 0023 0024 if (empty($string)) { 0025 return false; 0026 } 0027 $lower = strtolower($string); 0028 if (isset($colors[$lower])) { 0029 return $colors[$lower]; 0030 } 0031 if ($string[0] === '#') { 0032 $hex = substr($string, 1); 0033 } else { 0034 $hex = $string; 0035 } 0036 0037 $length = strlen($hex); 0038 if ($length !== 3 && $length !== 6) { 0039 return false; 0040 } 0041 if (!ctype_xdigit($hex)) { 0042 return false; 0043 } 0044 if ($length === 3) { 0045 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; 0046 } 0047 return "#$hex"; 0048 } 0049 } 0050 0051 // vim: et sw=4 sts=4