MultiLength.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. /**
  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. $int = substr($string, 0, $length - 1);
  32. if ($int == '') {
  33. return '*';
  34. }
  35. if (!is_numeric($int)) {
  36. return false;
  37. }
  38. $int = (int)$int;
  39. if ($int < 0) {
  40. return false;
  41. }
  42. if ($int == 0) {
  43. return '0';
  44. }
  45. if ($int == 1) {
  46. return '*';
  47. }
  48. return ((string)$int) . '*';
  49. }
  50. }
  51. // vim: et sw=4 sts=4