From f2f34a87f84b420cc5a5df16820e46b693cf911a Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Sat, 15 Aug 2009 06:45:31 +0000
Subject: [PATCH] #362972 by tic2000 and stewsnooze: Have nofollow filter
 remove 'rel' attributes instead of just adding rel='nofollow'.

---
 modules/filter/filter.module | 7 ++++++-
 modules/filter/filter.test   | 4 ++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/modules/filter/filter.module b/modules/filter/filter.module
index 945a90518d2d..58a3bf4ab9b5 100644
--- a/modules/filter/filter.module
+++ b/modules/filter/filter.module
@@ -728,7 +728,12 @@ function _filter_html($text, $format) {
   $text = filter_xss($text, $allowed_tags);
 
   if (variable_get("filter_html_nofollow_$format", FALSE)) {
-    $text = preg_replace('/<a([^>]+)>/i', '<a\\1 rel="nofollow">', $text);
+    $html_dom = filter_dom_load($text);
+    $links = $html_dom->getElementsByTagName('a');
+    foreach($links as $link) {
+      $link->setAttribute('rel', 'nofollow');
+    }
+    $text = filter_dom_serialize($html_dom);
   }
 
   return trim($text);
diff --git a/modules/filter/filter.test b/modules/filter/filter.test
index 4f74a1ae3d09..f73be063f78c 100644
--- a/modules/filter/filter.test
+++ b/modules/filter/filter.test
@@ -475,6 +475,10 @@ class FilterUnitTest extends DrupalWebTestCase {
 
     $f = _filter_html('<!--[if true]><a href="http://www.example.com/">text</a><![endif]-->', 'f');
     $this->assertNormalized($f, 'rel="nofollow"', t('Spam deterrent evasion -- link within a comment.'));
+
+    $f = _filter_html('<a href="http://www.example.com/" rel="follow">text</a>', 'f');
+    $this->assertNoNormalized($f, 'rel="follow"', t('Spam deterrent evasion -- with rel set - rel="follow" removed.'));
+    $this->assertNormalized($f, 'rel="nofollow"', t('Spam deterrent evasion -- with rel set - rel="nofollow" added.'));
   }
 
   /**
-- 
GitLab