Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 0cba68d9ebd4c12a3e5555332a3516d56519464a Mon Sep 17 00:00:00 2001
- From: David Morton <mortonda@dgrmm.net>
- Date: Thu, 23 Oct 2008 02:09:48 -0500
- Subject: [PATCH] Custom Injector to display URL address along with link text.
- When viewing potentially hostile html, it may be helpful to see what
- a given link was pointing to. This new injector takes the href
- attribute and adds the text after the link, and deletes the href
- attribute.
- Other forms of display could easily be contrived, but this seems to be
- a good basic way to present the information.
- Signed-off-by: David Morton <mortonda@dgrmm.net>
- ---
- library/HTMLPurifier/Injector/DisplayLinkUrls.php | 24 +++++++++++++++
- .../HTMLPurifier/Injector/DisplayLinkUrlsTest.php | 32 ++++++++++++++++++++
- 2 files changed, 56 insertions(+), 0 deletions(-)
- create mode 100644 library/HTMLPurifier/Injector/DisplayLinkUrls.php
- create mode 100644 tests/HTMLPurifier/Injector/DisplayLinkUrlsTest.php
- diff --git a/library/HTMLPurifier/Injector/DisplayLinkUrls.php b/library/HTMLPurifier/Injector/DisplayLinkUrls.php
- new file mode 100644
- index 0000000..c314213
- --- /dev/null
- +++ b/library/HTMLPurifier/Injector/DisplayLinkUrls.php
- @@ -0,0 +1,24 @@
- +<?php
- +
- +/**
- + * Injector that displays the URL of an anchor instead of linking to it, in addition to showing the text of the link.
- + */
- +class HTMLPurifier_Injector_DisplayLinkUrls extends HTMLPurifier_Injector
- +{
- +
- + public $name = 'DisplayLinkUrls';
- + public $needed = array('a');
- +
- + public function handleElement(&$token) {
- + }
- +
- + public function handleEnd(&$token) {
- + if (isset($token->start->attr['href'])){
- + $url = $token->start->attr['href'];
- + unset($token->start->attr['href']);
- + $token = array($token, new HTMLPurifier_Token_Text(" ($url)"));
- + } else {
- + // nothing to display
- + }
- + }
- +}
- \ No newline at end of file
- diff --git a/tests/HTMLPurifier/Injector/DisplayLinkUrlsTest.php b/tests/HTMLPurifier/Injector/DisplayLinkUrlsTest.php
- new file mode 100644
- index 0000000..af27715
- --- /dev/null
- +++ b/tests/HTMLPurifier/Injector/DisplayLinkUrlsTest.php
- @@ -0,0 +1,32 @@
- +<?php
- +
- +class HTMLPurifier_Injector_DisplayLinkUrlsTest extends HTMLPurifier_InjectorHarness
- +{
- +
- + function setup() {
- + parent::setup();
- + $this->config->set('AutoFormat', 'Custom', array(new HTMLPurifier_Injector_DisplayLinkUrls()));
- + }
- +
- + function testBasicLink() {
- + $this->assertResult(
- + '<a href="http://malware.example.com">Don\'t go here!</a>',
- + '<a>Don\'t go here!</a> (http://malware.example.com)'
- + );
- + }
- +
- + function testEmptyLink() {
- + $this->assertResult(
- + '<a>Don\'t go here!</a>',
- + '<a>Don\'t go here!</a>'
- + );
- + }
- + function testEmptyText() {
- + $this->assertResult(
- + '<a href="http://malware.example.com"></a>',
- + '<a></a> (http://malware.example.com)'
- + );
- + }
- +
- +}
- +?>
- \ No newline at end of file
- --
- 1.5.6.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement