diff --git a/library/HTMLPurifier/AttrDef/Integer.php b/library/HTMLPurifier/AttrDef/Integer.php new file mode 100644 index 00000000..3c647173 --- /dev/null +++ b/library/HTMLPurifier/AttrDef/Integer.php @@ -0,0 +1,37 @@ +non_negative = $non_negative; + } + + function validate($integer, $config, &$context) { + + $integer = $this->parseCDATA($integer); + if ($integer === '') return false; + + if ( !$this->non_negative && $integer[0] === '-' ) { + $digits = substr($integer, 1); + } elseif( $integer[0] === '+' ) { + $digits = $integer = substr($integer, 1); + } else { + $digits = $integer; + } + + if (!ctype_digit($digits)) return false; + return $integer; + + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/AttrDef/IntegerTest.php b/tests/HTMLPurifier/AttrDef/IntegerTest.php new file mode 100644 index 00000000..00d5831c --- /dev/null +++ b/tests/HTMLPurifier/AttrDef/IntegerTest.php @@ -0,0 +1,38 @@ +def = new HTMLPurifier_AttrDef_Integer(); + + $this->assertDef('0'); + $this->assertDef('1'); + $this->assertDef('-1'); + $this->assertDef('-10'); + $this->assertDef('14'); + $this->assertDef('+24', '24'); + $this->assertDef(' 14 ', '14'); + + $this->assertDef('-1.4', false); + $this->assertDef('3.4', false); + $this->assertDef('asdf', false); + + } + + function testNonNegative() { + + $this->def = new HTMLPurifier_AttrDef_Integer(true); + + $this->assertDef('0'); + $this->assertDef('1'); + $this->assertDef('-1', false); + + } + +} + +?> \ No newline at end of file diff --git a/tests/index.php b/tests/index.php index 8028446b..c035dd6a 100644 --- a/tests/index.php +++ b/tests/index.php @@ -66,6 +66,7 @@ $test_files[] = 'AttrDef/URITest.php'; $test_files[] = 'AttrDef/CSSTest.php'; $test_files[] = 'AttrDef/CompositeTest.php'; $test_files[] = 'AttrDef/ColorTest.php'; +$test_files[] = 'AttrDef/IntegerTest.php'; $test_files[] = 'IDAccumulatorTest.php'; $test_files[] = 'TagTransformTest.php'; $test_files[] = 'AttrTransform/LangTest.php';