Ident.php 724 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. /**
  3. * Validates based on {ident} CSS grammar production
  4. */
  5. class HTMLPurifier_AttrDef_CSS_Ident 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. $string = trim($string);
  16. // early abort: '' and '0' (strings that convert to false) are invalid
  17. if (!$string) {
  18. return false;
  19. }
  20. $pattern = '/^(-?[A-Za-z_][A-Za-z_\-0-9]*)$/';
  21. if (!preg_match($pattern, $string)) {
  22. return false;
  23. }
  24. return $string;
  25. }
  26. }
  27. // vim: et sw=4 sts=4