Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / AttrDef / CSS / Number.php
blob48f1335ac8932fe32771dc079e233171c8b6bded
1 <?php
3 /**
4 * Validates a number as defined by the CSS spec.
5 */
6 class HTMLPurifier_AttrDef_CSS_Number extends HTMLPurifier_AttrDef
9 /**
10 * Bool indicating whether or not only positive values allowed.
12 var $non_negative = false;
14 /**
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;
27 $sign = '';
28 switch ($number[0]) {
29 case '-':
30 if ($this->non_negative) return false;
31 $sign = '-';
32 case '+':
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');
49 if ($right === '') {
50 return $left ? $sign . $left : '0';
51 } elseif (!ctype_digit($right)) {
52 return false;
55 return $sign . $left . '.' . $right;