Permalink
Browse files

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
  • Loading branch information...
jamis committed Sep 13, 2005
1 parent 0f8a3a3 commit b97f4e459707694bd98babb7a8890f08016d5ebf
Showing with 26 additions and 1 deletion.
  1. +19 −1 actionpack/lib/action_view/helpers/text_helper.rb
  2. +7 −0 actionpack/test/template/text_helper_test.rb
@@ -279,9 +279,27 @@ def escape_regexp(text)
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 =~ /<a\s/i # don't replace URL's that are already linked
all
@@ -119,6 +119,13 @@ def test_auto_linking
assert_equal %(<p>Go to #{link3_result}. seriously, #{link3_result}? i think I'll say hello to #{email_result}. instead.</p>), auto_link(%(<p>Go to #{link3_raw}. seriously, #{link3_raw}? i think I'll say hello to #{email_raw}. instead.</p>))
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 %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
+ end
+
def test_sanitize_form
raw = "<form action=\"/foo/bar\" method=\"post\"><input></form>"
result = sanitize(raw)

0 comments on commit b97f4e4

Please sign in to comment.