From b97f4e459707694bd98babb7a8890f08016d5ebf Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Tue, 13 Sep 2005 18:48:34 +0000 Subject: Fix autolinking to not include trailing tags as part of the URL git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2237 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_view/helpers/text_helper.rb | 20 +++++++++++++++++++- actionpack/test/template/text_helper_test.rb | 7 +++++++ 2 files changed, 26 insertions(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 413f32532d..7b883a8b27 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -279,9 +279,27 @@ module ActionView text.gsub(/([\\|?+*\/\)\(])/) { |m| "\\#{$1}" } end + AUTO_LINK_RE = / + ( # leading text + <\w+.*?>| # leading HTML tag, or + [^=!:'"\/]| # leading punctuation, or + ^ # beginning of line + ) + ( + (?:http[s]?:\/\/)| # protocol spec, or + (?:www\.) # www.* + ) + ( + ([\w]+[\/.-]?)* # url segment + \w+[\/]? # url tail + (?:\#\w*)? # trailing anchor + ) + ([[:punct:]]|\s|<|$) # trailing text + /x + # Turns all urls into clickable links. def auto_link_urls(text, href_options = {}) - text.gsub(/(<\w+.*?>|[^=!:'"\/]|^)((?:http[s]?:\/\/)|(?:www\.))(([\w]+[[:punct:]]?)*\w+[\/]?)([[:punct:]]|\s|<|$)/) do + text.gsub(AUTO_LINK_RE) do all, a, b, c, d = $&, $1, $2, $3, $5 if a =~ /Go to #{link3_result}. seriously, #{link3_result}? i think I'll say hello to #{email_result}. instead.

), auto_link(%(

Go to #{link3_raw}. seriously, #{link3_raw}? i think I'll say hello to #{email_raw}. instead.

)) end + def test_auto_link_at_eol + url1 = "http://api.rubyonrails.com/Foo.html" + url2 = "http://www.ruby-doc.org/core/Bar.html" + + assert_equal %(

#{url1}
#{url2}

), auto_link("

#{url1}
#{url2}

") + end + def test_sanitize_form raw = "
" result = sanitize(raw) -- cgit v1.2.3