From 71c4a3c50c0c710a74ac449566eac5d6c89d1a0e Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 13 Aug 2006 21:59:52 +0000 Subject: [PATCH] Commit dud AttrDef integer. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@235 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier/AttrDef/Integer.php | 37 +++++++++++++++++++++ tests/HTMLPurifier/AttrDef/IntegerTest.php | 38 ++++++++++++++++++++++ tests/index.php | 1 + 3 files changed, 76 insertions(+) create mode 100644 library/HTMLPurifier/AttrDef/Integer.php create mode 100644 tests/HTMLPurifier/AttrDef/IntegerTest.php 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';