提交 bcab3f20 编写于 作者: P Paul Grayson

In tag helper, honor html_safe on array parameters; also make safe_join more...

In tag helper, honor html_safe on array parameters; also make safe_join more similar to Array.join by first calling flatten.
上级 80b4fe2c
......@@ -18,9 +18,9 @@ def raw(stringish)
end
# This method returns a html safe string similar to what <tt>Array#join</tt>
# would return. All items in the array, including the supplied separator, are
# html escaped unless they are html safe, and the returned string is marked
# as html safe.
# would return. The array is flattened, and all items, including
# the supplied separator, are html escaped unless they are html
# safe, and the returned string is marked as html safe.
#
# safe_join(["<p>foo</p>".html_safe, "<p>bar</p>"], "<br />")
# # => "<p>foo</p>&lt;br /&gt;&lt;p&gt;bar&lt;/p&gt;"
......@@ -31,7 +31,7 @@ def raw(stringish)
def safe_join(array, sep=$,)
sep = ERB::Util.unwrapped_html_escape(sep)
array.map { |i| ERB::Util.unwrapped_html_escape(i) }.join(sep).html_safe
array.flatten.map! { |i| ERB::Util.unwrapped_html_escape(i) }.join(sep).html_safe
end
end
end
......
......@@ -173,9 +173,21 @@ def boolean_tag_option(key)
end
def tag_option(key, value, escape)
value = value.join(" ") if value.is_a?(Array)
value = ERB::Util.unwrapped_html_escape(value) if escape
%(#{key}="#{value}")
escaped_value = case value
when Array
if escape
safe_join(value, " ")
else
value.join(" ")
end
else
if escape
ERB::Util.unwrapped_html_escape(value)
else
value
end
end
%(#{key}="#{escaped_value}")
end
end
end
......
......@@ -25,4 +25,11 @@ def setup
assert_equal "<p>foo</p><br /><p>bar</p>", joined
end
end
\ No newline at end of file
test "safe_join should work recursively similarly to Array.join" do
joined = safe_join(['a',['b','c']], ':')
assert_equal 'a:b:c', joined
joined = safe_join(['"a"',['<b>','<c>']], ' <br/> ')
assert_equal '&quot;a&quot; &lt;br/&gt; &lt;b&gt; &lt;br/&gt; &lt;c&gt;', joined
end
end
......@@ -80,11 +80,27 @@ def test_content_tag_with_escaped_array_class
str = content_tag('p', "limelight", :class => ["song", "play"])
assert_equal "<p class=\"song play\">limelight</p>", str
str = content_tag('p', "limelight", :class => ["song", ["play"]])
assert_equal "<p class=\"song play\">limelight</p>", str
end
def test_content_tag_with_unescaped_array_class
str = content_tag('p', "limelight", {:class => ["song", "play>"]}, false)
assert_equal "<p class=\"song play>\">limelight</p>", str
str = content_tag('p', "limelight", {:class => ["song", ["play>"]]}, false)
assert_equal "<p class=\"song play>\">limelight</p>", str
end
def test_content_tag_with_empty_array_class
str = content_tag('p', 'limelight', {:class => []})
assert_equal '<p class="">limelight</p>', str
end
def test_content_tag_with_unescaped_empty_array_class
str = content_tag('p', 'limelight', {:class => []}, false)
assert_equal '<p class="">limelight</p>', str
end
def test_content_tag_with_data_attributes
......@@ -115,6 +131,14 @@ def test_tag_honors_html_safe_for_param_values
end
end
def test_tag_honors_html_safe_with_escaped_array_class
str = tag('p', :class => ['song>', 'play>'.html_safe])
assert_equal '<p class="song&gt; play>" />', str
str = tag('p', :class => ['song>'.html_safe, 'play>'])
assert_equal '<p class="song> play&gt;" />', str
end
def test_skip_invalid_escaped_attributes
['&1;', '&#1dfa3;', '& #123;'].each do |escaped|
assert_equal %(<a href="#{escaped.gsub(/&/, '&amp;')}" />), tag('a', :href => escaped)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册