You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 line
1.9KB

  1. <?php
  2. /**
  3. * Represents a Length as defined by CSS.
  4. */
  5. class HTMLPurifier_AttrDef_CSS_Length extends HTMLPurifier_AttrDef
  6. {
  7. /**
  8. * @type HTMLPurifier_Length|string
  9. */
  10. protected $min;
  11. /**
  12. * @type HTMLPurifier_Length|string
  13. */
  14. protected $max;
  15. /**
  16. * @param HTMLPurifier_Length|string $min Minimum length, or null for no bound. String is also acceptable.
  17. * @param HTMLPurifier_Length|string $max Maximum length, or null for no bound. String is also acceptable.
  18. */
  19. public function __construct($min = null, $max = null)
  20. {
  21. $this->min = $min !== null ? HTMLPurifier_Length::make($min) : null;
  22. $this->max = $max !== null ? HTMLPurifier_Length::make($max) : null;
  23. }
  24. /**
  25. * @param string $string
  26. * @param HTMLPurifier_Config $config
  27. * @param HTMLPurifier_Context $context
  28. * @return bool|string
  29. */
  30. public function validate($string, $config, $context)
  31. {
  32. $string = $this->parseCDATA($string);
  33. // Optimizations
  34. if ($string === '') {
  35. return false;
  36. }
  37. if ($string === '0') {
  38. return '0';
  39. }
  40. if (strlen($string) === 1) {
  41. return false;
  42. }
  43. $length = HTMLPurifier_Length::make($string);
  44. if (!$length->isValid()) {
  45. return false;
  46. }
  47. if ($this->min) {
  48. $c = $length->compareTo($this->min);
  49. if ($c === false) {
  50. return false;
  51. }
  52. if ($c < 0) {
  53. return false;
  54. }
  55. }
  56. if ($this->max) {
  57. $c = $length->compareTo($this->max);
  58. if ($c === false) {
  59. return false;
  60. }
  61. if ($c > 0) {
  62. return false;
  63. }
  64. }
  65. return $length->toString();
  66. }
  67. }
  68. // vim: et sw=4 sts=4