Multiple.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Framework class for strings that involve multiple values.
  4. *
  5. * Certain CSS properties such as border-width and margin allow multiple
  6. * lengths to be specified. This class can take a vanilla border-width
  7. * definition and multiply it, usually into a max of four.
  8. *
  9. * @note Even though the CSS specification isn't clear about it, inherit
  10. * can only be used alone: it will never manifest as part of a multi
  11. * shorthand declaration. Thus, this class does not allow inherit.
  12. */
  13. class HTMLPurifier_AttrDef_CSS_Multiple extends HTMLPurifier_AttrDef
  14. {
  15. /**
  16. * Instance of component definition to defer validation to.
  17. * @todo Make protected
  18. */
  19. public $single;
  20. /**
  21. * Max number of values allowed.
  22. * @todo Make protected
  23. */
  24. public $max;
  25. /**
  26. * @param $single HTMLPurifier_AttrDef to multiply
  27. * @param $max Max number of values allowed (usually four)
  28. */
  29. public function __construct($single, $max = 4) {
  30. $this->single = $single;
  31. $this->max = $max;
  32. }
  33. public function validate($string, $config, $context) {
  34. $string = $this->parseCDATA($string);
  35. if ($string === '') return false;
  36. $parts = explode(' ', $string); // parseCDATA replaced \r, \t and \n
  37. $length = count($parts);
  38. $final = '';
  39. for ($i = 0, $num = 0; $i < $length && $num < $this->max; $i++) {
  40. if (ctype_space($parts[$i])) continue;
  41. $result = $this->single->validate($parts[$i], $config, $context);
  42. if ($result !== false) {
  43. $final .= $result . ' ';
  44. $num++;
  45. }
  46. }
  47. if ($final === '') return false;
  48. return rtrim($final);
  49. }
  50. }
  51. // vim: et sw=4 sts=4