Length.php 950 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * Validates the HTML type length (not to be confused with CSS's length).
  4. *
  5. * This accepts integer pixels or percentages as lengths for certain
  6. * HTML attributes.
  7. */
  8. class HTMLPurifier_AttrDef_HTML_Length extends HTMLPurifier_AttrDef_HTML_Pixels
  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. $points = substr($string, 0, $length - 1);
  19. if (!is_numeric($points)) return false;
  20. $points = (int) $points;
  21. if ($points < 0) return '0%';
  22. if ($points > 100) return '100%';
  23. return ((string) $points) . '%';
  24. }
  25. }
  26. // vim: et sw=4 sts=4