Ratio.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Validates a ratio as defined by the CSS spec.
  4. */
  5. class HTMLPurifier_AttrDef_CSS_Ratio extends HTMLPurifier_AttrDef
  6. {
  7. /**
  8. * @param string $ratio Ratio to validate
  9. * @param HTMLPurifier_Config $config Configuration options
  10. * @param HTMLPurifier_Context $context Context
  11. *
  12. * @return string|boolean
  13. *
  14. * @warning Some contexts do not pass $config, $context. These
  15. * variables should not be used without checking HTMLPurifier_Length
  16. */
  17. public function validate($ratio, $config, $context)
  18. {
  19. $ratio = $this->parseCDATA($ratio);
  20. $parts = explode('/', $ratio, 2);
  21. $length = count($parts);
  22. if ($length < 1 || $length > 2) {
  23. return false;
  24. }
  25. $num = new \HTMLPurifier_AttrDef_CSS_Number();
  26. if ($length === 1) {
  27. return $num->validate($parts[0], $config, $context);
  28. }
  29. $num1 = $num->validate($parts[0], $config, $context);
  30. $num2 = $num->validate($parts[1], $config, $context);
  31. if ($num1 === false || $num2 === false) {
  32. return false;
  33. }
  34. return $num1 . '/' . $num2;
  35. }
  36. }
  37. // vim: et sw=4 sts=4