Length.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. /**
  11. * @param string $string
  12. * @param HTMLPurifier_Config $config
  13. * @param HTMLPurifier_Context $context
  14. * @return bool|string
  15. */
  16. public function validate($string, $config, $context)
  17. {
  18. $string = trim($string);
  19. if ($string === '') {
  20. return false;
  21. }
  22. $parent_result = parent::validate($string, $config, $context);
  23. if ($parent_result !== false) {
  24. return $parent_result;
  25. }
  26. $length = strlen($string);
  27. $last_char = $string[$length - 1];
  28. if ($last_char !== '%') {
  29. return false;
  30. }
  31. $points = substr($string, 0, $length - 1);
  32. if (!is_numeric($points)) {
  33. return false;
  34. }
  35. $points = (int)$points;
  36. if ($points < 0) {
  37. return '0%';
  38. }
  39. if ($points > 100) {
  40. return '100%';
  41. }
  42. return ((string)$points) . '%';
  43. }
  44. }
  45. // vim: et sw=4 sts=4