Class.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. /**
  3. * Implements special behavior for class attribute (normally NMTOKENS)
  4. */
  5. class HTMLPurifier_AttrDef_HTML_Class extends HTMLPurifier_AttrDef_HTML_Nmtokens
  6. {
  7. protected function split($string, $config, $context) {
  8. // really, this twiddle should be lazy loaded
  9. $name = $config->getDefinition('HTML')->doctype->name;
  10. if ($name == "XHTML 1.1" || $name == "XHTML 2.0") {
  11. return parent::split($string, $config, $context);
  12. } else {
  13. return preg_split('/\s+/', $string);
  14. }
  15. }
  16. protected function filter($tokens, $config, $context) {
  17. $allowed = $config->get('Attr.AllowedClasses');
  18. $forbidden = $config->get('Attr.ForbiddenClasses');
  19. $ret = array();
  20. foreach ($tokens as $token) {
  21. if (
  22. ($allowed === null || isset($allowed[$token])) &&
  23. !isset($forbidden[$token]) &&
  24. // We need this O(n) check because of PHP's array
  25. // implementation that casts -0 to 0.
  26. !in_array($token, $ret, true)
  27. ) {
  28. $ret[] = $token;
  29. }
  30. }
  31. return $ret;
  32. }
  33. }