0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-19 10:45:18 +00:00

[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
This commit is contained in:
Edward Z. Yang 2006-11-08 03:10:43 +00:00
parent e1b29d7c25
commit d2fd193bc4
6 changed files with 91 additions and 4 deletions

View File

@ -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.

View File

@ -0,0 +1,17 @@
<?php
require_once 'HTMLPurifier/AttrDef.php';
class HTMLPurifier_AttrDef_Email extends HTMLPurifier_AttrDef
{
/**
* Unpacks a mailbox into its display-name and address
*/
function unpack($string) {
// needs to be implemented
}
}
?>

View File

@ -0,0 +1,23 @@
<?php
require_once 'HTMLPurifier/AttrDef/Email.php';
/**
* Primitive email validation class based on the regexp found at
* http://www.regular-expressions.info/email.html
*/
class HTMLPurifier_AttrDef_Email_SimpleCheck extends HTMLPurifier_AttrDef_Email
{
function validate($string, $config, &$context) {
// no support for named mailboxes i.e. "Bob <bob@example.com>"
// 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;
}
}
?>

View File

@ -0,0 +1,16 @@
<?php
require_once 'HTMLPurifier/AttrDef/Email/SimpleCheck.php';
require_once 'HTMLPurifier/AttrDef/EmailHarness.php';
class HTMLPurifier_AttrDef_Email_SimpleCheckTest
extends HTMLPurifier_AttrDef_EmailHarness
{
function setUp() {
$this->def = new HTMLPurifier_AttrDef_Email_SimpleCheck();
}
}
?>

View File

@ -0,0 +1,33 @@
<?php
require_once 'HTMLPurifier/AttrDef/Email.php';
class HTMLPurifier_AttrDef_EmailHarness extends HTMLPurifier_AttrDefHarness
{
/**
* Tests common email strings that are obviously pass/fail
*/
function testCore() {
$this->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 <bobby.bob.bob@it.is.example.com>');
// 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);
}
}
?>

View File

@ -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';