aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view
diff options
context:
space:
mode:
authorMislav Marohnić <mislav.marohnic@gmail.com>2008-11-12 13:15:57 +0100
committerMichael Koziarski <michael@koziarski.com>2008-11-15 18:30:01 +0100
commitc6c5cd554110f6e62290de3e3008076b2f69e7cb (patch)
tree8acdf33374ce90a659cd20b75825fcde9e99a4fc /actionpack/lib/action_view
parent789a3f5b035fd293a9e235672a97b683a56ba0c3 (diff)
downloadrails-c6c5cd554110f6e62290de3e3008076b2f69e7cb.tar.gz
rails-c6c5cd554110f6e62290de3e3008076b2f69e7cb.tar.bz2
rails-c6c5cd554110f6e62290de3e3008076b2f69e7cb.zip
refactor autolink helper. change tests to expect HTML-escaped URLs
Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb46
1 files changed, 20 insertions, 26 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 36f7575652..07f98158f7 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -545,38 +545,32 @@ module ActionView
end
AUTO_LINK_RE = %r{
- ( # leading text
- <\w+.*?>| # leading HTML tag, or
- [^=!:'"/]| # leading punctuation, or
- ^ # beginning of line
- )
- (
- (?:https?://)| # protocol spec, or
- (?:www\.) # www.*
- )
- (
- [-\w]+ # subdomain or domain
- (?:\.[-\w]+)* # remaining subdomains or domain
- (?::\d+)? # port
- (?:/(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$]))*)* # path
- (?:\?[\w\+@%&=.;:-]+)? # query string
- (?:\#[\w\-]*)? # trailing anchor
- )
- ([[:punct:]]|<|$|) # trailing text
- }x unless const_defined?(:AUTO_LINK_RE)
+ ( https?:// | www\. )
+ [^\s<]+
+ }x unless const_defined?(:AUTO_LINK_RE)
# Turns all urls into clickable links. If a block is given, each url
# is yielded and the result is used as the link text.
def auto_link_urls(text, html_options = {})
- extra_options = tag_options(html_options.stringify_keys) || ""
+ link_attributes = html_options.stringify_keys
text.gsub(AUTO_LINK_RE) do
- all, a, b, c, d = $&, $1, $2, $3, $4
- if a =~ /<a\s/i # don't replace URL's that are already linked
- all
+ href = $&
+ # detect already linked URLs
+ unless $` =~ /<a\s[^>]*href="$/
+ if href =~ /[^\w\/-]$/
+ punctuation = href[-1, 1]
+ href = href[0, href.length - 1]
+ else
+ punctuation = ''
+ end
+
+ link_text = block_given?? yield(href) : href
+ href = 'http://' + href unless href.index('http') == 0
+
+ content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation
else
- text = b + c
- text = yield(text) if block_given?
- %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}"#{extra_options}>#{text}</a>#{d})
+ # do not change string; URL is alreay linked
+ href
end
end
end