diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-09-20 20:40:22 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-09-20 20:40:22 +0000 |
commit | 09e76e6acacb5f90dfc67d50c44b8257d36c4e95 (patch) | |
tree | 414b11fc37f29b32a1c2644a5d2b838012c23083 | |
parent | 835cb43745e79521514738044b3cf0232957de0b (diff) | |
download | rails-09e76e6acacb5f90dfc67d50c44b8257d36c4e95.tar.gz rails-09e76e6acacb5f90dfc67d50c44b8257d36c4e95.tar.bz2 rails-09e76e6acacb5f90dfc67d50c44b8257d36c4e95.zip |
Autolink behaves well with emails embedded in URLs. Closes #7313.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7516 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 16 | ||||
-rw-r--r-- | actionpack/test/template/text_helper_test.rb | 7 |
3 files changed, 20 insertions, 5 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index cdd82db90a..0d71df79f9 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Autolink behaves well with emails embedded in URLs. #7313 [Jeremy McAnally, tarmo] + * Fixed that default layouts did not take the format into account #9564 [lifofifo] * Fixed optimized route segment escaping. #9562 [wildchild, Jeremy Kemper] diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 0c1d92cdc6..e07c8c5ef3 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -305,7 +305,7 @@ module ActionView def auto_link(text, link = :all, href_options = {}, &block) return '' if text.blank? case link - when :all then auto_link_urls(auto_link_email_addresses(text, &block), href_options, &block) + when :all then auto_link_email_addresses(auto_link_urls(text, href_options, &block), &block) when :email_addresses then auto_link_email_addresses(text, &block) when :urls then auto_link_urls(text, href_options, &block) end @@ -534,8 +534,8 @@ module ActionView [-\w]+ # subdomain or domain (?:\.[-\w]+)* # remaining subdomains or domain (?::\d+)? # port - (?:/(?:(?:[~\w\+%-]|(?:[,.;:][^\s$]))+)?)* # path - (?:\?[\w\+%&=.;-]+)? # query string + (?:/(?:(?:[~\w\+@%-]|(?:[,.;:][^\s$]))+)?)* # path + (?:\?[\w\+@%&=.;-]+)? # query string (?:\#[\w\-]*)? # trailing anchor ) ([[:punct:]]|\s|<|$) # trailing text @@ -560,10 +560,16 @@ module ActionView # Turns all email addresses into clickable links. If a block is given, # each email is yielded and the result is used as the link text. def auto_link_email_addresses(text) + body = text.dup text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do text = $1 - text = yield(text) if block_given? - %{<a href="mailto:#{$1}">#{text}</a>} + + if body.match(/<a\b[^>]*>(.*)(#{Regexp.escape(text)})(.*)<\/a>/) + text + else + display_text = (block_given?) ? yield(text) : text + %{<a href="mailto:#{text}">#{display_text}</a>} + end end end end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 1f5b90e811..1e80d030ec 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -157,6 +157,7 @@ class TextHelperTest < Test::Unit::TestCase http://www.rubyonrails.com/contact;new?with=query&string=params http://www.rubyonrails.com/~minam/contact;new?with=query&string=params http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007 + http://www.mail-archive.com/rails@lists.rubyonrails.org/ ) urls.each do |url| @@ -167,6 +168,8 @@ class TextHelperTest < Test::Unit::TestCase def test_auto_linking email_raw = 'david@loudthinking.com' email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>} + email2_raw = '+david@loudthinking.com' + email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>} link_raw = 'http://www.rubyonrails.com' link_result = %{<a href="#{link_raw}">#{link_raw}</a>} link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>} @@ -186,6 +189,8 @@ class TextHelperTest < Test::Unit::TestCase link8_result = %{<a href="#{link8_raw}">#{link8_raw}</a>} link9_raw = 'http://business.timesonline.co.uk/article/0,,9065-2473189,00.html' link9_result = %{<a href="#{link9_raw}">#{link9_raw}</a>} + link10_raw = 'http://www.mail-archive.com/ruby-talk@ruby-lang.org/' + link10_result = %{<a href="#{link10_raw}">#{link10_raw}</a>} assert_equal %(hello #{email_result}), auto_link("hello #{email_raw}", :email_addresses) assert_equal %(Go to #{link_result}), auto_link("Go to #{link_raw}", :urls) @@ -225,6 +230,8 @@ class TextHelperTest < Test::Unit::TestCase assert_equal %(<p>#{link9_result} Link</p>), auto_link("<p>#{link9_raw} Link</p>") assert_equal %(Go to #{link9_result}.), auto_link(%(Go to #{link9_raw}.)) assert_equal %(<p>Go to #{link9_result}. seriously, #{link9_result}? i think I'll say hello to #{email_result}. instead.</p>), auto_link(%(<p>Go to #{link9_raw}. seriously, #{link9_raw}? i think I'll say hello to #{email_raw}. instead.</p>)) + assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>") + assert_equal email2_result, auto_link(email2_raw) assert_equal '', auto_link(nil) assert_equal '', auto_link('') end |