mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-12-22 16:31:53 +00:00
Implement HTML.Nofollow for external links.
Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
This commit is contained in:
parent
d848c99b74
commit
8c80349f9d
1
NEWS
1
NEWS
@ -10,6 +10,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
|
|||||||
==========================
|
==========================
|
||||||
|
|
||||||
4.2.1, unknown release date
|
4.2.1, unknown release date
|
||||||
|
! Added %HTML.Nofollow to add rel="nofollow" to external links.
|
||||||
- Make removal of conditional IE comments ungreedy; thanks Bernd
|
- Make removal of conditional IE comments ungreedy; thanks Bernd
|
||||||
for reporting.
|
for reporting.
|
||||||
|
|
||||||
|
@ -186,15 +186,20 @@
|
|||||||
</directive>
|
</directive>
|
||||||
<directive id="HTML.Proprietary">
|
<directive id="HTML.Proprietary">
|
||||||
<file name="HTMLPurifier/HTMLModuleManager.php">
|
<file name="HTMLPurifier/HTMLModuleManager.php">
|
||||||
<line>221</line>
|
<line>220</line>
|
||||||
</file>
|
</file>
|
||||||
</directive>
|
</directive>
|
||||||
<directive id="HTML.SafeObject">
|
<directive id="HTML.SafeObject">
|
||||||
<file name="HTMLPurifier/HTMLModuleManager.php">
|
<file name="HTMLPurifier/HTMLModuleManager.php">
|
||||||
<line>226</line>
|
<line>223</line>
|
||||||
</file>
|
</file>
|
||||||
</directive>
|
</directive>
|
||||||
<directive id="HTML.SafeEmbed">
|
<directive id="HTML.SafeEmbed">
|
||||||
|
<file name="HTMLPurifier/HTMLModuleManager.php">
|
||||||
|
<line>226</line>
|
||||||
|
</file>
|
||||||
|
</directive>
|
||||||
|
<directive id="HTML.Nofollow">
|
||||||
<file name="HTMLPurifier/HTMLModuleManager.php">
|
<file name="HTMLPurifier/HTMLModuleManager.php">
|
||||||
<line>229</line>
|
<line>229</line>
|
||||||
</file>
|
</file>
|
||||||
|
@ -125,6 +125,7 @@ require 'HTMLPurifier/AttrTransform/Lang.php';
|
|||||||
require 'HTMLPurifier/AttrTransform/Length.php';
|
require 'HTMLPurifier/AttrTransform/Length.php';
|
||||||
require 'HTMLPurifier/AttrTransform/Name.php';
|
require 'HTMLPurifier/AttrTransform/Name.php';
|
||||||
require 'HTMLPurifier/AttrTransform/NameSync.php';
|
require 'HTMLPurifier/AttrTransform/NameSync.php';
|
||||||
|
require 'HTMLPurifier/AttrTransform/Nofollow.php';
|
||||||
require 'HTMLPurifier/AttrTransform/SafeEmbed.php';
|
require 'HTMLPurifier/AttrTransform/SafeEmbed.php';
|
||||||
require 'HTMLPurifier/AttrTransform/SafeObject.php';
|
require 'HTMLPurifier/AttrTransform/SafeObject.php';
|
||||||
require 'HTMLPurifier/AttrTransform/SafeParam.php';
|
require 'HTMLPurifier/AttrTransform/SafeParam.php';
|
||||||
@ -151,6 +152,7 @@ require 'HTMLPurifier/HTMLModule/Image.php';
|
|||||||
require 'HTMLPurifier/HTMLModule/Legacy.php';
|
require 'HTMLPurifier/HTMLModule/Legacy.php';
|
||||||
require 'HTMLPurifier/HTMLModule/List.php';
|
require 'HTMLPurifier/HTMLModule/List.php';
|
||||||
require 'HTMLPurifier/HTMLModule/Name.php';
|
require 'HTMLPurifier/HTMLModule/Name.php';
|
||||||
|
require 'HTMLPurifier/HTMLModule/Nofollow.php';
|
||||||
require 'HTMLPurifier/HTMLModule/NonXMLCommonAttributes.php';
|
require 'HTMLPurifier/HTMLModule/NonXMLCommonAttributes.php';
|
||||||
require 'HTMLPurifier/HTMLModule/Object.php';
|
require 'HTMLPurifier/HTMLModule/Object.php';
|
||||||
require 'HTMLPurifier/HTMLModule/Presentation.php';
|
require 'HTMLPurifier/HTMLModule/Presentation.php';
|
||||||
|
@ -119,6 +119,7 @@ require_once $__dir . '/HTMLPurifier/AttrTransform/Lang.php';
|
|||||||
require_once $__dir . '/HTMLPurifier/AttrTransform/Length.php';
|
require_once $__dir . '/HTMLPurifier/AttrTransform/Length.php';
|
||||||
require_once $__dir . '/HTMLPurifier/AttrTransform/Name.php';
|
require_once $__dir . '/HTMLPurifier/AttrTransform/Name.php';
|
||||||
require_once $__dir . '/HTMLPurifier/AttrTransform/NameSync.php';
|
require_once $__dir . '/HTMLPurifier/AttrTransform/NameSync.php';
|
||||||
|
require_once $__dir . '/HTMLPurifier/AttrTransform/Nofollow.php';
|
||||||
require_once $__dir . '/HTMLPurifier/AttrTransform/SafeEmbed.php';
|
require_once $__dir . '/HTMLPurifier/AttrTransform/SafeEmbed.php';
|
||||||
require_once $__dir . '/HTMLPurifier/AttrTransform/SafeObject.php';
|
require_once $__dir . '/HTMLPurifier/AttrTransform/SafeObject.php';
|
||||||
require_once $__dir . '/HTMLPurifier/AttrTransform/SafeParam.php';
|
require_once $__dir . '/HTMLPurifier/AttrTransform/SafeParam.php';
|
||||||
@ -145,6 +146,7 @@ require_once $__dir . '/HTMLPurifier/HTMLModule/Image.php';
|
|||||||
require_once $__dir . '/HTMLPurifier/HTMLModule/Legacy.php';
|
require_once $__dir . '/HTMLPurifier/HTMLModule/Legacy.php';
|
||||||
require_once $__dir . '/HTMLPurifier/HTMLModule/List.php';
|
require_once $__dir . '/HTMLPurifier/HTMLModule/List.php';
|
||||||
require_once $__dir . '/HTMLPurifier/HTMLModule/Name.php';
|
require_once $__dir . '/HTMLPurifier/HTMLModule/Name.php';
|
||||||
|
require_once $__dir . '/HTMLPurifier/HTMLModule/Nofollow.php';
|
||||||
require_once $__dir . '/HTMLPurifier/HTMLModule/NonXMLCommonAttributes.php';
|
require_once $__dir . '/HTMLPurifier/HTMLModule/NonXMLCommonAttributes.php';
|
||||||
require_once $__dir . '/HTMLPurifier/HTMLModule/Object.php';
|
require_once $__dir . '/HTMLPurifier/HTMLModule/Object.php';
|
||||||
require_once $__dir . '/HTMLPurifier/HTMLModule/Presentation.php';
|
require_once $__dir . '/HTMLPurifier/HTMLModule/Presentation.php';
|
||||||
|
41
library/HTMLPurifier/AttrTransform/Nofollow.php
Normal file
41
library/HTMLPurifier/AttrTransform/Nofollow.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// must be called POST validation
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds rel="nofollow" to all outbound links. This transform is
|
||||||
|
* only attached if Attr.Nofollow is TRUE.
|
||||||
|
*/
|
||||||
|
class HTMLPurifier_AttrTransform_Nofollow extends HTMLPurifier_AttrTransform
|
||||||
|
{
|
||||||
|
private $parser;
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->parser = new HTMLPurifier_URIParser();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function transform($attr, $config, $context) {
|
||||||
|
|
||||||
|
if (!isset($attr['href'])) {
|
||||||
|
return $attr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX Kind of inefficient
|
||||||
|
$url = $this->parser->parse($attr['href']);
|
||||||
|
$scheme = $url->getSchemeObj($config, $context);
|
||||||
|
|
||||||
|
if (!is_null($url->host) && $scheme !== false && $scheme->browsable) {
|
||||||
|
if (isset($attr['rel'])) {
|
||||||
|
$attr['rel'] .= ' nofollow';
|
||||||
|
} else {
|
||||||
|
$attr['rel'] = 'nofollow';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $attr;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// vim: et sw=4 sts=4
|
Binary file not shown.
@ -0,0 +1,7 @@
|
|||||||
|
HTML.Nofollow
|
||||||
|
TYPE: bool
|
||||||
|
VERSION: 4.2.1
|
||||||
|
DEFAULT: FALSE
|
||||||
|
--DESCRIPTION--
|
||||||
|
If enabled, nofollow rel attributes are added to all outgoing links.
|
||||||
|
--# vim: et sw=4 sts=4
|
19
library/HTMLPurifier/HTMLModule/Nofollow.php
Normal file
19
library/HTMLPurifier/HTMLModule/Nofollow.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module adds the nofollow attribute transformation to a tags. It
|
||||||
|
* is enabled by HTML.Nofollow
|
||||||
|
*/
|
||||||
|
class HTMLPurifier_HTMLModule_Nofollow extends HTMLPurifier_HTMLModule
|
||||||
|
{
|
||||||
|
|
||||||
|
public $name = 'Nofollow';
|
||||||
|
|
||||||
|
public function setup($config) {
|
||||||
|
$a = $this->addBlankElement('a');
|
||||||
|
$a->attr_transform_post[] = new HTMLPurifier_AttrTransform_Nofollow();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// vim: et sw=4 sts=4
|
@ -216,19 +216,19 @@ class HTMLPurifier_HTMLModuleManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add proprietary module (this gets special treatment because
|
// custom modules
|
||||||
// it is completely removed from doctypes, etc.)
|
|
||||||
if ($config->get('HTML.Proprietary')) {
|
if ($config->get('HTML.Proprietary')) {
|
||||||
$modules[] = 'Proprietary';
|
$modules[] = 'Proprietary';
|
||||||
}
|
}
|
||||||
|
|
||||||
// add SafeObject/Safeembed modules
|
|
||||||
if ($config->get('HTML.SafeObject')) {
|
if ($config->get('HTML.SafeObject')) {
|
||||||
$modules[] = 'SafeObject';
|
$modules[] = 'SafeObject';
|
||||||
}
|
}
|
||||||
if ($config->get('HTML.SafeEmbed')) {
|
if ($config->get('HTML.SafeEmbed')) {
|
||||||
$modules[] = 'SafeEmbed';
|
$modules[] = 'SafeEmbed';
|
||||||
}
|
}
|
||||||
|
if ($config->get('HTML.Nofollow')) {
|
||||||
|
$modules[] = 'Nofollow';
|
||||||
|
}
|
||||||
|
|
||||||
// merge in custom modules
|
// merge in custom modules
|
||||||
$modules = array_merge($modules, $this->userModules);
|
$modules = array_merge($modules, $this->userModules);
|
||||||
|
20
tests/HTMLPurifier/HTMLModule/NofollowTest.php
Normal file
20
tests/HTMLPurifier/HTMLModule/NofollowTest.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class HTMLPurifier_HTMLModule_NofollowTest extends HTMLPurifier_HTMLModuleHarness
|
||||||
|
{
|
||||||
|
|
||||||
|
function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
$this->config->set('HTML.Nofollow', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testNofollow() {
|
||||||
|
$this->assertResult(
|
||||||
|
'<a href="http://google.com">a</a><a href="/local">b</a><a href="mailto:foo@example.com">c</a>',
|
||||||
|
'<a href="http://google.com" rel="nofollow">a</a><a href="/local">b</a><a href="mailto:foo@example.com">c</a>'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// vim: et sw=4 sts=4
|
Loading…
Reference in New Issue
Block a user