diff options
author | Mislav Marohnić <mislav.marohnic@gmail.com> | 2010-04-17 05:13:40 +0200 |
---|---|---|
committer | Mikel Lindsaar <raasdnil@gmail.com> | 2010-06-03 23:32:09 +1000 |
commit | f00561e0f1e61e31f8957eda3897f15518942ed4 (patch) | |
tree | 60c0a2c2e2a4263251c6f651da79823bcd049779 /actionpack | |
parent | 884716564c3f84f285b58ccc786684782cee8c5a (diff) | |
download | rails-f00561e0f1e61e31f8957eda3897f15518942ed4.tar.gz rails-f00561e0f1e61e31f8957eda3897f15518942ed4.tar.bz2 rails-f00561e0f1e61e31f8957eda3897f15518942ed4.zip |
auto_link: support arbitrary URI schemes like "ftp:" and "file:"
recognizes all URI scheme allowed characters, such as colon and period.
[#3494 state:resolved]
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 6 | ||||
-rw-r--r-- | actionpack/test/template/text_helper_test.rb | 21 |
2 files changed, 13 insertions, 14 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 0e1bc139ff..fa930ac626 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -537,7 +537,7 @@ module ActionView end AUTO_LINK_RE = %r{ - ( https?:// | www\. ) + (?: ([\w+.:-]+:)// | www\. ) [^\s<]+ }x unless const_defined?(:AUTO_LINK_RE) @@ -548,7 +548,7 @@ module ActionView def auto_link_urls(text, html_options = {}) link_attributes = html_options.stringify_keys text.gsub(AUTO_LINK_RE) do - href = $& + scheme, href = $1, $& punctuation = [] left, right = $`, $' # detect already linked URLs and URLs in the middle of a tag @@ -566,7 +566,7 @@ module ActionView end link_text = block_given?? yield(href) : href - href = 'http://' + href unless href =~ %r{^[a-z]+://}i + href = 'http://' + href unless scheme content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation.reverse.join('') end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 8b6c107a21..13f55cacb6 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -376,17 +376,16 @@ class TextHelperTest < ActionView::TestCase 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 + ftp_raw = 'ftp://example.com/file.txt' + assert_equal %(Download #{generate_result(ftp_raw)}), auto_link("Download #{ftp_raw}") + + file_scheme = 'file:///home/username/RomeoAndJuliet.pdf' + z39_scheme = 'z39.50r://host:696/db' + chrome_scheme = 'chrome://package/section/path' + view_source = 'view-source:http://en.wikipedia.org/wiki/URI_scheme' + assert_equal generate_result(z39_scheme), auto_link(z39_scheme) + assert_equal generate_result(chrome_scheme), auto_link(chrome_scheme) + assert_equal generate_result(view_source), auto_link(view_source) end def test_auto_link_already_linked |