1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

auto_link: support arbitrary URI schemes like "ftp:" and "file:"

recognizes all URI scheme allowed characters, such as colon and period.

[#3494 state:resolved]
This commit is contained in:
Mislav Marohnić 2010-04-17 05:13:40 +02:00
parent 69a9669d9d
commit 133ada6ab0
2 changed files with 13 additions and 14 deletions

View file

@ -537,7 +537,7 @@ module ActionView
end end
AUTO_LINK_RE = %r{ AUTO_LINK_RE = %r{
( https?:// | www\. ) (?: ([\w+.:-]+:)// | www\. )
[^\s<]+ [^\s<]+
}x unless const_defined?(:AUTO_LINK_RE) }x unless const_defined?(:AUTO_LINK_RE)
@ -548,7 +548,7 @@ module ActionView
def auto_link_urls(text, html_options = {}) def auto_link_urls(text, html_options = {})
link_attributes = html_options.stringify_keys link_attributes = html_options.stringify_keys
text.gsub(AUTO_LINK_RE) do text.gsub(AUTO_LINK_RE) do
href = $& scheme, href = $1, $&
punctuation = [] punctuation = []
left, right = $`, $' left, right = $`, $'
# detect already linked URLs and URLs in the middle of a tag # detect already linked URLs and URLs in the middle of a tag
@ -566,7 +566,7 @@ module ActionView
end end
link_text = block_given?? yield(href) : href 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('') content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation.reverse.join('')
end end

View file

@ -376,17 +376,16 @@ class TextHelperTest < ActionView::TestCase
end end
def test_auto_link_other_protocols def test_auto_link_other_protocols
silence_warnings do ftp_raw = 'ftp://example.com/file.txt'
begin assert_equal %(Download #{generate_result(ftp_raw)}), auto_link("Download #{ftp_raw}")
old_re_value = ActionView::Helpers::TextHelper::AUTO_LINK_RE
ActionView::Helpers::TextHelper.const_set :AUTO_LINK_RE, %r{(ftp://)[^\s<]+} file_scheme = 'file:///home/username/RomeoAndJuliet.pdf'
link_raw = 'ftp://example.com/file.txt' z39_scheme = 'z39.50r://host:696/db'
link_result = generate_result(link_raw) chrome_scheme = 'chrome://package/section/path'
assert_equal %(Download #{link_result}), auto_link("Download #{link_raw}") view_source = 'view-source:http://en.wikipedia.org/wiki/URI_scheme'
ensure assert_equal generate_result(z39_scheme), auto_link(z39_scheme)
ActionView::Helpers::TextHelper.const_set :AUTO_LINK_RE, old_re_value assert_equal generate_result(chrome_scheme), auto_link(chrome_scheme)
end assert_equal generate_result(view_source), auto_link(view_source)
end
end end
def test_auto_link_already_linked def test_auto_link_already_linked