You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Number.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Validates a number as defined by the CSS spec.
  4. */
  5. class HTMLPurifier_AttrDef_CSS_Number extends HTMLPurifier_AttrDef
  6. {
  7. /**
  8. * Indicates whether or not only positive values are allowed.
  9. * @type bool
  10. */
  11. protected $non_negative = false;
  12. /**
  13. * @param bool $non_negative indicates whether negatives are forbidden
  14. */
  15. public function __construct($non_negative = false)
  16. {
  17. $this->non_negative = $non_negative;
  18. }
  19. /**
  20. * @param string $number
  21. * @param HTMLPurifier_Config $config
  22. * @param HTMLPurifier_Context $context
  23. * @return string|bool
  24. * @warning Some contexts do not pass $config, $context. These
  25. * variables should not be used without checking HTMLPurifier_Length
  26. */
  27. public function validate($number, $config, $context)
  28. {
  29. $number = $this->parseCDATA($number);
  30. if ($number === '') {
  31. return false;
  32. }
  33. if ($number === '0') {
  34. return '0';
  35. }
  36. $sign = '';
  37. switch ($number[0]) {
  38. case '-':
  39. if ($this->non_negative) {
  40. return false;
  41. }
  42. $sign = '-';
  43. case '+':
  44. $number = substr($number, 1);
  45. }
  46. if (ctype_digit($number)) {
  47. $number = ltrim($number, '0');
  48. return $number ? $sign . $number : '0';
  49. }
  50. // Period is the only non-numeric character allowed
  51. if (strpos($number, '.') === false) {
  52. return false;
  53. }
  54. list($left, $right) = explode('.', $number, 2);
  55. if ($left === '' && $right === '') {
  56. return false;
  57. }
  58. if ($left !== '' && !ctype_digit($left)) {
  59. return false;
  60. }
  61. $left = ltrim($left, '0');
  62. $right = rtrim($right, '0');
  63. if ($right === '') {
  64. return $left ? $sign . $left : '0';
  65. } elseif (!ctype_digit($right)) {
  66. return false;
  67. }
  68. return $sign . $left . '.' . $right;
  69. }
  70. }
  71. // vim: et sw=4 sts=4