Multiple.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. * @type HTMLPurifier_AttrDef
  18. * @todo Make protected
  19. */
  20. public $single;
  21. /**
  22. * Max number of values allowed.
  23. * @todo Make protected
  24. */
  25. public $max;
  26. /**
  27. * @param HTMLPurifier_AttrDef $single HTMLPurifier_AttrDef to multiply
  28. * @param int $max Max number of values allowed (usually four)
  29. */
  30. public function __construct($single, $max = 4)
  31. {
  32. $this->single = $single;
  33. $this->max = $max;
  34. }
  35. /**
  36. * @param string $string
  37. * @param HTMLPurifier_Config $config
  38. * @param HTMLPurifier_Context $context
  39. * @return bool|string
  40. */
  41. public function validate($string, $config, $context)
  42. {
  43. $string = $this->mungeRgb($this->parseCDATA($string));
  44. if ($string === '') {
  45. return false;
  46. }
  47. $parts = explode(' ', $string); // parseCDATA replaced \r, \t and \n
  48. $length = count($parts);
  49. $final = '';
  50. for ($i = 0, $num = 0; $i < $length && $num < $this->max; $i++) {
  51. if (ctype_space($parts[$i])) {
  52. continue;
  53. }
  54. $result = $this->single->validate($parts[$i], $config, $context);
  55. if ($result !== false) {
  56. $final .= $result . ' ';
  57. $num++;
  58. }
  59. }
  60. if ($final === '') {
  61. return false;
  62. }
  63. return rtrim($final);
  64. }
  65. }
  66. // vim: et sw=4 sts=4