3 require_once 'HTMLPurifier/AttrDef.php';
6 * Validates an integer.
7 * @note While this class was modeled off the CSS definition, no currently
8 * allowed CSS uses this type. The properties that do are: widows,
9 * orphans, z-index, counter-increment, counter-reset. Some of the
10 * HTML attributes, however, find use for a non-negative version of this.
12 class HTMLPurifier_AttrDef_Integer
extends HTMLPurifier_AttrDef
16 * Bool indicating whether or not negative values are allowed
21 * Bool indicating whether or not zero is allowed
26 * Bool indicating whether or not positive values are allowed
31 * @param $negative Bool indicating whether or not negative values are allowed
32 * @param $zero Bool indicating whether or not zero is allowed
33 * @param $positive Bool indicating whether or not positive values are allowed
35 function HTMLPurifier_AttrDef_Integer(
36 $negative = true, $zero = true, $positive = true
38 $this->negative
= $negative;
40 $this->positive
= $positive;
43 function validate($integer, $config, &$context) {
45 $integer = $this->parseCDATA($integer);
46 if ($integer === '') return false;
48 // we could possibly simply typecast it to integer, but there are
49 // certain fringe cases that must not return an integer.
52 if ( $this->negative
&& $integer[0] === '-' ) {
53 $digits = substr($integer, 1);
54 if ($digits === '0') $integer = '0'; // rm minus sign for zero
55 } elseif( $this->positive
&& $integer[0] === '+' ) {
56 $digits = $integer = substr($integer, 1); // rm unnecessary plus
61 // test if it's numeric
62 if (!ctype_digit($digits)) return false;
64 // perform scope tests
65 if (!$this->zero
&& $integer == 0) return false;
66 if (!$this->positive
&& $integer > 0) return false;
67 if (!$this->negative
&& $integer < 0) return false;