Length.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * Represents a Length as defined by CSS.
  4. */
  5. class HTMLPurifier_AttrDef_CSS_Length extends HTMLPurifier_AttrDef
  6. {
  7. protected $min, $max;
  8. /**
  9. * @param HTMLPurifier_Length $max Minimum length, or null for no bound. String is also acceptable.
  10. * @param HTMLPurifier_Length $max Maximum length, or null for no bound. String is also acceptable.
  11. */
  12. public function __construct($min = null, $max = null) {
  13. $this->min = $min !== null ? HTMLPurifier_Length::make($min) : null;
  14. $this->max = $max !== null ? HTMLPurifier_Length::make($max) : null;
  15. }
  16. public function validate($string, $config, $context) {
  17. $string = $this->parseCDATA($string);
  18. // Optimizations
  19. if ($string === '') return false;
  20. if ($string === '0') return '0';
  21. if (strlen($string) === 1) return false;
  22. $length = HTMLPurifier_Length::make($string);
  23. if (!$length->isValid()) return false;
  24. if ($this->min) {
  25. $c = $length->compareTo($this->min);
  26. if ($c === false) return false;
  27. if ($c < 0) return false;
  28. }
  29. if ($this->max) {
  30. $c = $length->compareTo($this->max);
  31. if ($c === false) return false;
  32. if ($c > 0) return false;
  33. }
  34. return $length->toString();
  35. }
  36. }
  37. // vim: et sw=4 sts=4