Skip to content

Commit 11df6e3

Browse files
committed
New option: sanitized_options
1 parent 8c9a5e9 commit 11df6e3

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/rails_autolink/helpers.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module TextHelper
1313
# <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
1414
# e-mail address is yielded and the result is used as the link text. By default the
1515
# text given is sanitized, you can override this behaviour setting the
16-
# <tt>:sanitize</tt> option to false.
16+
# <tt>:sanitize</tt> option to false, or you can add options to the sanitization of
17+
# the text using the <tt>:sanitize_options</tt> option hash.
1718
#
1819
# ==== Examples
1920
# auto_link("Go to http://www.rubyonrails.org and say hello to [email protected]")
@@ -55,8 +56,9 @@ def auto_link(text, *args, &block)#link = :all, html = {}, &block)
5556
options[:html] = args[1] || {}
5657
end
5758
options.reverse_merge!(:link => :all, :html => {})
58-
sanitize = (options[:sanitize] != false)
59-
text = conditional_sanitize(text, sanitize).to_str
59+
sanitize = (options[:sanitize] != false)
60+
sanitize_options = options[:sanitize_options] || {}
61+
text = conditional_sanitize(text, sanitize, sanitize_options).to_str
6062
case options[:link].to_sym
6163
when :all then conditional_html_safe(auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block), sanitize)
6264
when :email_addresses then conditional_html_safe(auto_link_email_addresses(text, options[:html], &block), sanitize)
@@ -137,8 +139,8 @@ def auto_linked?(left, right)
137139
(left.rindex(AUTO_LINK_CRE[2]) and $' !~ AUTO_LINK_CRE[3])
138140
end
139141

140-
def conditional_sanitize(target, condition)
141-
condition ? sanitize(target) : target
142+
def conditional_sanitize(target, condition, sanitize_options = {})
143+
condition ? sanitize(target, sanitize_options) : target
142144
end
143145

144146
def conditional_html_safe(target, condition)

test/test_rails_autolink.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ def test_auto_link_should_sanitize_input_when_sanitize_option_is_not_false
8888
assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link("#{link_raw}#{malicious_script}")
8989
assert auto_link("#{link_raw}#{malicious_script}").html_safe?
9090
end
91+
92+
def test_auto_link_should_sanitize_input_with_sanitize_options
93+
link_raw = %{http://www.rubyonrails.com?id=1&num=2}
94+
malicious_script = '<script>alert("malicious!")</script>'
95+
text_with_attributes = %{<a href="http://ruby-lang-org" target="_blank" data-malicious="inject">Ruby</a>}
96+
97+
text_result = %{<a class="big" href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a><a href="http://ruby-lang-org" target="_blank">Ruby</a>}
98+
assert_equal text_result, auto_link("#{link_raw}#{malicious_script}#{text_with_attributes}",
99+
:sanitize_options => {:attributes => ["target", "href"]},
100+
:html => {:class => 'big'})
101+
102+
assert auto_link("#{link_raw}#{malicious_script}#{text_with_attributes}",
103+
:sanitize_options => {:attributes => ["target", "href"]},
104+
:html => {:class => 'big'}).html_safe?
105+
end
91106

92107
def test_auto_link_should_not_sanitize_input_when_sanitize_option_is_false
93108
link_raw = %{http://www.rubyonrails.com?id=1&num=2}
@@ -117,11 +132,13 @@ def test_auto_link_already_linked
117132
linked3 = %('<a href="http://www.example.com" rel="nofollow">www.example.com</a>')
118133
linked4 = %('<a href="http://www.example.com"><b>www.example.com</b></a>')
119134
linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>')
135+
linked6 = %('<a href="#close">close</a> <a href="http://www.example.com" target="_blank" data-ruby="ror"><b>www.example.com</b></a>')
120136
assert_equal linked1, auto_link(linked1)
121137
assert_equal linked2, auto_link(linked2)
122138
assert_equal linked3, auto_link(linked3, :sanitize => false)
123139
assert_equal linked4, auto_link(linked4)
124140
assert_equal linked5, auto_link(linked5)
141+
assert_equal linked6, auto_link(linked6, :sanitize_options => {:attributes => ["href", "target", "data-ruby"]})
125142

126143
linked_email = %Q(<a href="mailto:[email protected]">Mail me</a>)
127144
assert_equal linked_email, auto_link(linked_email)

0 commit comments

Comments
 (0)