Browse files

Added a third parameter to TextHelper#auto_link called href_options f…

…or specifying additional tag options on the links generated #1401 [tyler.kovacs@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1432 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent a3659d5 commit 5b3bc3139a1b15054f26092eb95a5fbc8fdc07aa @dhh dhh committed Jun 16, 2005
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added a third parameter to TextHelper#auto_link called href_options for specifying additional tag options on the links generated #1401 [tyler.kovacs@gmail.com]. Example: auto_link(text, :all, { :target => "_blank" }) to have all the generated links open in a new window.
+
* Fixed TextHelper#highlight to return the text, not nil, if the phrase is blank #1409 [patrick@lenz.sh]
* Fixed TagHelper such that :name and 'name' keys in the options doesn't result in two attributes #1455 [take_tk]
View
13 actionpack/lib/action_view/helpers/text_helper.rb
@@ -1,3 +1,5 @@
+require File.dirname(__FILE__) + '/tag_helper'
+
module ActionView
module Helpers #:nodoc:
# Provides a set of methods for working with text strings that can help unburden the level of inline Ruby code in the
@@ -116,11 +118,11 @@ def simple_format(text)
# auto_link("Go to http://www.rubyonrails.com and say hello to david@loudthinking.com") =>
# Go to <a href="http://www.rubyonrails.com">http://www.rubyonrails.com</a> and
# say hello to <a href="mailto:david@loudthinking.com">david@loudthinking.com</a>
- def auto_link(text, link = :all)
+ def auto_link(text, link = :all, href_options = {})
case link
- when :all then auto_link_urls(auto_link_email_addresses(text))
+ when :all then auto_link_urls(auto_link_email_addresses(text), href_options)
when :email_addresses then auto_link_email_addresses(text)
- when :urls then auto_link_urls(text)
+ when :urls then auto_link_urls(text, href_options)
end
end
@@ -191,13 +193,14 @@ def escape_regexp(text)
end
# Turns all urls into clickable links.
- def auto_link_urls(text)
+ def auto_link_urls(text, href_options = {})
text.gsub(/(<\w+.*?>|[^=!:'"\/]|^)((?:http[s]?:\/\/)|(?:www\.))([^\s<]+\/?)([[:punct:]]|\s|<|$)/) 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
else
- %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}">#{b}#{c}</a>#{d})
+ extra_options = tag_options(href_options.stringify_keys) || ""
+ %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}"#{extra_options}>#{b}#{c}</a>#{d})
end
end
end
View
3 actionpack/test/template/text_helper_test.rb
@@ -4,6 +4,7 @@
class TextHelperTest < Test::Unit::TestCase
include ActionView::Helpers::TextHelper
+ include ActionView::Helpers::TagHelper
def test_simple_format
assert_equal "<p>crazy\n<br /> cross\n<br /> platform linebreaks</p>", simple_format("crazy\r\n cross\r platform linebreaks")
@@ -77,6 +78,7 @@ def test_auto_linking
email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
link_raw = 'http://www.rubyonrails.com'
link_result = %{<a href="#{link_raw}">#{link_raw}</a>}
+ link_result_with_options = %{<a href="#{link_raw}" target="_blank">#{link_raw}</a>}
link2_raw = 'www.rubyonrails.com'
link2_result = %{<a href="http://#{link2_raw}">#{link2_raw}</a>}
@@ -90,6 +92,7 @@ def test_auto_linking
assert_equal %(Go to #{link2_raw}), auto_link("Go to #{link2_raw}", :email_addresses)
assert_equal %(<p>Link #{link2_result}</p>), auto_link("<p>Link #{link2_raw}</p>")
assert_equal %(<p>#{link2_result} Link</p>), auto_link("<p>#{link2_raw} Link</p>")
+ assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"})
end
def test_sanitize_form

0 comments on commit 5b3bc31

Please sign in to comment.