aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-02-10 16:50:35 +0100
committerJosé Valim <jose.valim@gmail.com>2011-02-10 16:50:35 +0100
commit89a5f1463d7e9546ed7a0cf482afea99ba2040e7 (patch)
tree2a8ed779ae295c83dcb886839bf47be0bcdbbee0 /activesupport
parent98c0c5db50a7679b3d58769ac22cb0a27a62c930 (diff)
downloadrails-89a5f1463d7e9546ed7a0cf482afea99ba2040e7.tar.gz
rails-89a5f1463d7e9546ed7a0cf482afea99ba2040e7.tar.bz2
rails-89a5f1463d7e9546ed7a0cf482afea99ba2040e7.zip
Revert "Removed Array#safe_join in AS core_ext and moved it to a view helper with the same same."
Applied the wrong version. This reverts commit 98c0c5db50a7679b3d58769ac22cb0a27a62c930.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/string/output_safety.rb31
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb44
2 files changed, 75 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/output_safety.rb b/activesupport/lib/active_support/core_ext/string/output_safety.rb
index c930abc003..0c8fc20ea5 100644
--- a/activesupport/lib/active_support/core_ext/string/output_safety.rb
+++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -122,3 +122,34 @@ class String
ActiveSupport::SafeBuffer.new(self)
end
end
+
+class Array
+ # If the separator and all the items in the array are html safe
+ # then an html safe string is returned using <tt>Array#join</tt>,
+ # otherwise the result of <tt>Array#join</tt> is returned without
+ # marking it as html safe.
+ #
+ # ["Mr", "Bojangles"].join.html_safe?
+ # # => false
+ #
+ # ["Mr".html_safe, "Bojangles".html_safe].join.html_safe?
+ # # => true
+ #
+ def safe_join(sep=$,)
+ sep ||= "".html_safe
+ str = join(sep)
+ (sep.html_safe? && html_safe?) ? str.html_safe : str
+ end
+
+ # Returns +true+ if all items in the array are html safe.
+ #
+ # [""].html_safe?
+ # # => false
+ #
+ # ["".html_safe].html_safe?
+ # # => true
+ #
+ def html_safe?
+ detect { |e| !e.html_safe? }.nil?
+ end
+end
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index bb865cae91..15e39a06c3 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -434,6 +434,50 @@ class OutputSafetyTest < ActiveSupport::TestCase
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.safe_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.safe_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 !array.safe_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 !array.safe_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 !array.safe_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 !array.safe_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 !array.html_safe?
+ end
+
test 'emits normal string yaml' do
assert_equal 'foo'.to_yaml, 'foo'.html_safe.to_yaml(:foo => 1)
end