From 0022b9fcf0cd875b3bd82f38a015a17dc8d7c9bc Mon Sep 17 00:00:00 2001 From: Zach Brock Date: Wed, 18 Nov 2009 22:44:08 -0800 Subject: [PATCH] fixing autolinking other protocols --- actionpack/lib/action_view/helpers/text_helper.rb | 4 ++-- actionpack/test/template/text_helper_test.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index de6a2dd..c65690a 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -548,7 +548,7 @@ module ActionView left, right = $`, $' # detect already linked URLs and URLs in the middle of a tag if left =~ /<[^>]+$/ && right =~ /^[^>]*>/ - # do not change string; URL is alreay linked + # do not change string; URL is already linked href else # don't include trailing punctuation character as part of the URL @@ -560,7 +560,7 @@ module ActionView end link_text = block_given?? yield(href) : href - href = 'http://' + href unless href.index('http') == 0 + href = 'http://' + href unless href =~ %r{^[a-z]+://}i content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 715c390..a6cc4b9 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -355,6 +355,20 @@ class TextHelperTest < ActionView::TestCase link10_result = generate_result(link10_raw) assert_equal %(

#{link10_result} Link

), auto_link("

#{link10_raw} Link

") end + + def test_auto_link_other_protocols + silence_warnings do + begin + old_re_value = ActionView::Helpers::TextHelper::AUTO_LINK_RE + ActionView::Helpers::TextHelper.const_set :AUTO_LINK_RE, %r{(ftp://)[^\s<]+} + link_raw = 'ftp://example.com/file.txt' + link_result = generate_result(link_raw) + assert_equal %(Download #{link_result}), auto_link("Download #{link_raw}") + ensure + ActionView::Helpers::TextHelper.const_set :AUTO_LINK_RE, old_re_value + end + end + end def test_auto_link_already_linked linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com') -- 1.6.0.2