aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
authorJosh Kalderimis <josh.kalderimis@gmail.com>2011-02-10 22:03:53 +0800
committerJosé Valim <jose.valim@gmail.com>2011-02-10 23:47:54 +0800
commit1a73407b8506b548a5343c66c16897140203472e (patch)
tree2a8ed779ae295c83dcb886839bf47be0bcdbbee0 /activesupport/lib/active_support/core_ext
parentf7221f5c7571da50d4d0199c0a7502cc7cd82b6e (diff)
downloadrails-1a73407b8506b548a5343c66c16897140203472e.tar.gz
rails-1a73407b8506b548a5343c66c16897140203472e.tar.bz2
rails-1a73407b8506b548a5343c66c16897140203472e.zip
Corrected the html_safe implementation for Array. Moved the html safe version of join to its own method (safe_join) as not to degrade the performance of join for unrelated html_safe use. [#6298 state:resolved]
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/string/output_safety.rb29
1 files changed, 22 insertions, 7 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 520aa4e67d..0c8fc20ea5 100644
--- a/activesupport/lib/active_support/core_ext/string/output_safety.rb
+++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -124,17 +124,32 @@ class String
end
class Array
-
- alias_method :original_join, :join
-
- def join(sep=$,)
+ # 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 = original_join(sep)
+ 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?
- self.detect {|e| !e.html_safe?}.nil?
+ detect { |e| !e.html_safe? }.nil?
end
-
end