国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.2KB

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