0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-23 13:51:54 +00:00

Add AutoFormat.LinkifyEmail Option.

If enabled, automatically provide links to e-mail addresses.
For example,
  Send an e-mail to user@example.com.
is transformed to
  Send an e-mail to <a href="mailto:user@example.com">user@example.com</a>.

Signed-off-by: Bradley M. Froehle <brad.froehle@gmail.com>
This commit is contained in:
Bradley M. Froehle 2011-02-13 20:34:32 -08:00
parent e76f4b45d0
commit 6385f42ebc
8 changed files with 112 additions and 0 deletions

2
NEWS
View File

@ -22,6 +22,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
directory/file permissions directory/file permissions
! Fix longstanding bug in Flash support for non-IE browsers, and ! Fix longstanding bug in Flash support for non-IE browsers, and
allow more wmode attributes. allow more wmode attributes.
! Add %AutoFormat.LinkifyEmail option for automatically linking
to e-mail addresses.
- Switch to an iterative traversal of the DOM, which prevents us - Switch to an iterative traversal of the DOM, which prevents us
from running out of stack space for deeply nested documents. from running out of stack space for deeply nested documents.
Thanks Maxim Krizhanovsky for contributing a patch. Thanks Maxim Krizhanovsky for contributing a patch.

View File

@ -254,6 +254,9 @@
<file name="HTMLPurifier/URIDefinition.php"> <file name="HTMLPurifier/URIDefinition.php">
<line>64</line> <line>64</line>
</file> </file>
<file name="HTMLPurifier/URIScheme.php">
<line>75</line>
</file>
</directive> </directive>
<directive id="URI.Base"> <directive id="URI.Base">
<file name="HTMLPurifier/URIDefinition.php"> <file name="HTMLPurifier/URIDefinition.php">

View File

@ -176,6 +176,7 @@ require 'HTMLPurifier/HTMLModule/Tidy/XHTML.php';
require 'HTMLPurifier/Injector/AutoParagraph.php'; require 'HTMLPurifier/Injector/AutoParagraph.php';
require 'HTMLPurifier/Injector/DisplayLinkURI.php'; require 'HTMLPurifier/Injector/DisplayLinkURI.php';
require 'HTMLPurifier/Injector/Linkify.php'; require 'HTMLPurifier/Injector/Linkify.php';
require 'HTMLPurifier/Injector/LinkifyEmail.php';
require 'HTMLPurifier/Injector/PurifierLinkify.php'; require 'HTMLPurifier/Injector/PurifierLinkify.php';
require 'HTMLPurifier/Injector/RemoveEmpty.php'; require 'HTMLPurifier/Injector/RemoveEmpty.php';
require 'HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php'; require 'HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php';

View File

@ -170,6 +170,7 @@ require_once $__dir . '/HTMLPurifier/HTMLModule/Tidy/XHTML.php';
require_once $__dir . '/HTMLPurifier/Injector/AutoParagraph.php'; require_once $__dir . '/HTMLPurifier/Injector/AutoParagraph.php';
require_once $__dir . '/HTMLPurifier/Injector/DisplayLinkURI.php'; require_once $__dir . '/HTMLPurifier/Injector/DisplayLinkURI.php';
require_once $__dir . '/HTMLPurifier/Injector/Linkify.php'; require_once $__dir . '/HTMLPurifier/Injector/Linkify.php';
require_once $__dir . '/HTMLPurifier/Injector/LinkifyEmail.php';
require_once $__dir . '/HTMLPurifier/Injector/PurifierLinkify.php'; require_once $__dir . '/HTMLPurifier/Injector/PurifierLinkify.php';
require_once $__dir . '/HTMLPurifier/Injector/RemoveEmpty.php'; require_once $__dir . '/HTMLPurifier/Injector/RemoveEmpty.php';
require_once $__dir . '/HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php'; require_once $__dir . '/HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php';

View File

@ -0,0 +1,12 @@
AutoFormat.LinkifyEmail
TYPE: bool
VERSION: 4.3.0
DEFAULT: false
--DESCRIPTION--
<p>
This directive turns on e-mail linkification, auto-linking e-mail addresses.
<code>a</code> tags with the <code>href</code> attribute
must be allowed.
</p>
--# vim: et sw=4 sts=4

View File

@ -0,0 +1,43 @@
<?php
/**
* Injector that converts e-mails to actual links.
*/
class HTMLPurifier_Injector_LinkifyEmail extends HTMLPurifier_Injector
{
public $name = 'LinkifyEmail';
public $needed = array('a' => array('href'));
public function handleText(&$token) {
if (!$this->allowsElement('a')) return;
if (strpos($token->data, '@') === false) {
// our really quick heuristic failed, abort
return;
}
// e-mail regex comes from Drupal 7, see http://api.drupal.org/_filter_url
$bits = preg_split('#([A-Za-z0-9._-]+@(?:(?:[A-Za-z0-9._+-]+\.)?[A-Za-z]{2,64}\b))#S', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);
$token = array();
// $i = index
// $c = count
// $l = is link
for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {
if (!$l) {
if ($bits[$i] === '') continue;
$token[] = new HTMLPurifier_Token_Text($bits[$i]);
} else {
$token[] = new HTMLPurifier_Token_Start('a', array('href' => 'mailto:' . $bits[$i]));
$token[] = new HTMLPurifier_Token_Text($bits[$i]);
$token[] = new HTMLPurifier_Token_End('a');
}
}
}
}
// vim: et sw=4 sts=4

View File

@ -0,0 +1,50 @@
<?php
class HTMLPurifier_Injector_LinkifyEmailTest extends HTMLPurifier_InjectorHarness
{
function setup() {
parent::setup();
$this->config->set('AutoFormat.LinkifyEmail', true);
}
function testLinkifyEmailInRootNode() {
$this->assertResult(
'user@example.com',
'<a href="mailto:user@example.com">user@example.com</a>'
);
}
function testLinkifyEmailLInInlineNode() {
$this->assertResult(
'<b>user@example.com</b>',
'<b><a href="mailto:user@example.com">user@example.com</a></b>'
);
}
function testBasicUsageCase() {
$this->assertResult(
'This e-mail user@example.com is what you need',
'This e-mail <a href="mailto:user@example.com">user@example.com</a> is what you need'
);
}
function testIgnoreEmailInATag() {
$this->assertResult(
'<a>user@example.com</a>'
);
}
function testNeeded() {
$this->config->set('HTML.Allowed', 'b');
$this->expectError('Cannot enable LinkifyEmail injector because a is not allowed');
$this->assertResult('user@example.com');
}
function testExcludes() {
$this->assertResult('<a><span>user@example.com</span></a>');
}
}
// vim: et sw=4 sts=4