MultiLength.php 984 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * Validates a MultiLength as defined by the HTML spec.
  4. *
  5. * A multilength is either a integer (pixel count), a percentage, or
  6. * a relative number.
  7. */
  8. class HTMLPurifier_AttrDef_HTML_MultiLength extends HTMLPurifier_AttrDef_HTML_Length
  9. {
  10. public function validate($string, $config, $context) {
  11. $string = trim($string);
  12. if ($string === '') return false;
  13. $parent_result = parent::validate($string, $config, $context);
  14. if ($parent_result !== false) return $parent_result;
  15. $length = strlen($string);
  16. $last_char = $string[$length - 1];
  17. if ($last_char !== '*') return false;
  18. $int = substr($string, 0, $length - 1);
  19. if ($int == '') return '*';
  20. if (!is_numeric($int)) return false;
  21. $int = (int) $int;
  22. if ($int < 0) return false;
  23. if ($int == 0) return '0';
  24. if ($int == 1) return '*';
  25. return ((string) $int) . '*';
  26. }
  27. }
  28. // vim: et sw=4 sts=4