提交 f7221f5c 编写于 作者: P Paul Hieromnimon 提交者: José Valim

Initial html_safe implemention for Array

上级 c560c8b2
......@@ -122,3 +122,19 @@ def html_safe
ActiveSupport::SafeBuffer.new(self)
end
end
class Array
alias_method :original_join, :join
def join(sep=$,)
sep ||= "".html_safe
str = original_join(sep)
(sep.html_safe? && html_safe?) ? str.html_safe : str
end
def html_safe?
self.detect {|e| !e.html_safe?}.nil?
end
end
......@@ -434,6 +434,50 @@ def to_s
assert string.html_safe?
end
test "Joining safe elements without a separator is safe" do
array = 5.times.collect {"some string".html_safe}
assert array.join.html_safe?
end
test "Joining safe elements with a safe separator is safe" do
array = 5.times.collect {"some string".html_safe}
assert array.join("-".html_safe).html_safe?
end
test "Joining safe elements with an unsafe separator is unsafe" do
array = 5.times.collect {"some string".html_safe}
assert_false array.join("-").html_safe?
end
test "Joining is unsafe if any element is unsafe even with a safe separator" do
array = 5.times.collect {"some string".html_safe}
array << "some string"
assert_false array.join("-".html_safe).html_safe?
end
test "Joining is unsafe if any element is unsafe and no separator is given" do
array = 5.times.collect {"some string".html_safe}
array << "some string"
assert_false array.join.html_safe?
end
test "Joining is unsafe if any element is unsafe and the separator is unsafe" do
array = 5.times.collect {"some string".html_safe}
array << "some string"
assert_false array.join("-").html_safe?
end
test "Array is safe if all elements are safe" do
array = 5.times.collect { "some string".html_safe }
assert array.html_safe?
end
test "Array is unsafe if any element is unsafe" do
array = 5.times.collect { "some string".html_safe }
array << "some string"
assert_false array.html_safe?
end
test 'emits normal string yaml' do
assert_equal 'foo'.to_yaml, 'foo'.html_safe.to_yaml(:foo => 1)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册