4 * Validates a number as defined by the CSS spec.
6 class HTMLPurifier_AttrDef_CSS_Number
extends HTMLPurifier_AttrDef
10 * Bool indicating whether or not only positive values allowed.
12 var $non_negative = false;
15 * @param $non_negative Bool indicating whether negatives are forbidden
17 function HTMLPurifier_AttrDef_CSS_Number($non_negative = false) {
18 $this->non_negative
= $non_negative;
21 function validate($number, $config, &$context) {
23 $number = $this->parseCDATA($number);
25 if ($number === '') return false;
30 if ($this->non_negative
) return false;
33 $number = substr($number, 1);
36 if (ctype_digit($number)) {
37 $number = ltrim($number, '0');
38 return $number ?
$sign . $number : '0';
40 if (!strpos($number, '.')) return false;
42 list($left, $right) = explode('.', $number, 2);
44 if (!ctype_digit($left)) return false;
45 $left = ltrim($left, '0');
47 $right = rtrim($right, '0');
50 return $left ?
$sign . $left : '0';
51 } elseif (!ctype_digit($right)) {
55 return $sign . $left . '.' . $right;