From d2fd193bc402dfd40826ad3b9c2d2c518d55309f Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Wed, 8 Nov 2006 03:10:43 +0000 Subject: [PATCH] [1.2.0] Implement primitive email regexp to be used for mailto. There are many spotty implementation issues, so this code is not actually called anywhere else currently. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@517 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier/AttrDef.php | 5 +-- library/HTMLPurifier/AttrDef/Email.php | 17 ++++++++++ .../AttrDef/Email/SimpleCheck.php | 23 +++++++++++++ .../AttrDef/Email/SimpleCheckTest.php | 16 +++++++++ tests/HTMLPurifier/AttrDef/EmailHarness.php | 33 +++++++++++++++++++ tests/index.php | 1 + 6 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 library/HTMLPurifier/AttrDef/Email.php create mode 100644 library/HTMLPurifier/AttrDef/Email/SimpleCheck.php create mode 100644 tests/HTMLPurifier/AttrDef/Email/SimpleCheckTest.php create mode 100644 tests/HTMLPurifier/AttrDef/EmailHarness.php diff --git a/library/HTMLPurifier/AttrDef.php b/library/HTMLPurifier/AttrDef.php index 4abe3acc..334a7ace 100644 --- a/library/HTMLPurifier/AttrDef.php +++ b/library/HTMLPurifier/AttrDef.php @@ -20,10 +20,7 @@ class HTMLPurifier_AttrDef var $minimized = false; /** - * Abstract function defined for functions that validate and clean strings. - * - * This function forms the basis for all the subclasses: they must - * define this method. + * Validates and cleans passed string according to a definition. * * @public * @param $string String to be validated and cleaned. diff --git a/library/HTMLPurifier/AttrDef/Email.php b/library/HTMLPurifier/AttrDef/Email.php new file mode 100644 index 00000000..7a7ad6ab --- /dev/null +++ b/library/HTMLPurifier/AttrDef/Email.php @@ -0,0 +1,17 @@ + \ No newline at end of file diff --git a/library/HTMLPurifier/AttrDef/Email/SimpleCheck.php b/library/HTMLPurifier/AttrDef/Email/SimpleCheck.php new file mode 100644 index 00000000..4b9fdf1a --- /dev/null +++ b/library/HTMLPurifier/AttrDef/Email/SimpleCheck.php @@ -0,0 +1,23 @@ +" + // that needs more percent encoding to be done + if ($string == '') return false; + $string = trim($string); + $result = preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $string); + return $result ? $string : false; + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/AttrDef/Email/SimpleCheckTest.php b/tests/HTMLPurifier/AttrDef/Email/SimpleCheckTest.php new file mode 100644 index 00000000..70a77f72 --- /dev/null +++ b/tests/HTMLPurifier/AttrDef/Email/SimpleCheckTest.php @@ -0,0 +1,16 @@ +def = new HTMLPurifier_AttrDef_Email_SimpleCheck(); + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/AttrDef/EmailHarness.php b/tests/HTMLPurifier/AttrDef/EmailHarness.php new file mode 100644 index 00000000..715a5293 --- /dev/null +++ b/tests/HTMLPurifier/AttrDef/EmailHarness.php @@ -0,0 +1,33 @@ +assertDef('bob@example.com'); + $this->assertDef(' bob@example.com ', 'bob@example.com'); + $this->assertDef('bob.thebuilder@example.net'); + $this->assertDef('Bob_the_Builder-the-2nd@example.org'); + $this->assertDef('Bob%20the%20Builder@white-space.test'); + + // extended format, with real name + //$this->assertDef('Bob%20Builder%20%3Cbobby.bob.bob@it.is.example.com%3E'); + //$this->assertDef('Bob Builder '); + + // time to fail + $this->assertDef('bob', false); + $this->assertDef('bob@home@work', false); + $this->assertDef('@example.com', false); + $this->assertDef('bob@', false); + $this->assertDef('', false); + + } + +} + +?> \ No newline at end of file diff --git a/tests/index.php b/tests/index.php index 4ceb8d96..33ad68e6 100644 --- a/tests/index.php +++ b/tests/index.php @@ -78,6 +78,7 @@ $test_files[] = 'AttrDef/IPv6Test.php'; $test_files[] = 'AttrDef/FontTest.php'; $test_files[] = 'AttrDef/BorderTest.php'; $test_files[] = 'AttrDef/ListStyleTest.php'; +$test_files[] = 'AttrDef/Email/SimpleCheckTest.php'; $test_files[] = 'IDAccumulatorTest.php'; $test_files[] = 'TagTransformTest.php'; $test_files[] = 'AttrTransform/LangTest.php';