From 0b6cdf1b1ca3330eee9a7ec0a73c0daf8a62fe68 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 11 Mar 2009 11:27:44 +0100 Subject: [PATCH] avoid auto-linking already linked email addresses [#1862 state:resolved] auto_link('Mail me') should not change the original string. References [#1523] --- actionpack/lib/action_view/helpers/text_helper.rb | 6 ++++-- actionpack/test/template/text_helper_test.rb | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 48bf471..8e12382 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -564,8 +564,10 @@ module ActionView body = text.dup text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do text = $1 - - if body.match(/]*>(.*)(#{Regexp.escape(text)})(.*)<\/a>/) + left, right = $`, $' + in_attribute = left =~ /<[^>]+$/ && right =~ /^[^>]*>/ + + if in_attribute or body.match(/]*>(.*)(#{Regexp.escape(text)})(.*)<\/a>/) text else display_text = (block_given?) ? yield(text) : text diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index a370f14..0072f5d 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -379,6 +379,9 @@ class TextHelperTest < ActionView::TestCase link_raw = 'http://www.rubyonrails.org/images/rails.png' link_result = %Q() assert_equal link_result, auto_link(link_result) + # already linked mailto links should also be intact: + link_result = %Q(Mail me) + assert_equal link_result, auto_link(link_result) end def test_auto_link_at_eol -- 1.6.1