Nmtokens.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Validates contents based on NMTOKENS attribute type.
  4. */
  5. class HTMLPurifier_AttrDef_HTML_Nmtokens extends HTMLPurifier_AttrDef
  6. {
  7. public function validate($string, $config, $context) {
  8. $string = trim($string);
  9. // early abort: '' and '0' (strings that convert to false) are invalid
  10. if (!$string) return false;
  11. $tokens = $this->split($string, $config, $context);
  12. $tokens = $this->filter($tokens, $config, $context);
  13. if (empty($tokens)) return false;
  14. return implode(' ', $tokens);
  15. }
  16. /**
  17. * Splits a space separated list of tokens into its constituent parts.
  18. */
  19. protected function split($string, $config, $context) {
  20. // OPTIMIZABLE!
  21. // do the preg_match, capture all subpatterns for reformulation
  22. // we don't support U+00A1 and up codepoints or
  23. // escaping because I don't know how to do that with regexps
  24. // and plus it would complicate optimization efforts (you never
  25. // see that anyway).
  26. $pattern = '/(?:(?<=\s)|\A)'. // look behind for space or string start
  27. '((?:--|-?[A-Za-z_])[A-Za-z_\-0-9]*)'.
  28. '(?:(?=\s)|\z)/'; // look ahead for space or string end
  29. preg_match_all($pattern, $string, $matches);
  30. return $matches[1];
  31. }
  32. /**
  33. * Template method for removing certain tokens based on arbitrary criteria.
  34. * @note If we wanted to be really functional, we'd do an array_filter
  35. * with a callback. But... we're not.
  36. */
  37. protected function filter($tokens, $config, $context) {
  38. return $tokens;
  39. }
  40. }
  41. // vim: et sw=4 sts=4