PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/vjCommentPlugin/lib/tools/htmlpurifier/library/HTMLPurifier/AttrDef/HTML/Nmtokens.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 52 lines | 22 code | 11 blank | 19 comment | 2 complexity | 8face464a844799e3aab2e9f4f918868 MD5 | raw file
Possible License(s): LGPL-2.1
  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