Skip to content
This repository
Browse code

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]
  • Loading branch information...
commit 1a73407b8506b548a5343c66c16897140203472e 1 parent f7221f5
Josh Kalderimis authored josevalim committed
29  activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -124,17 +124,32 @@ def html_safe
124 124
 end
125 125
 
126 126
 class Array
127  
-
128  
-  alias_method :original_join, :join
129  
-
130  
-  def join(sep=$,)
  127
+  # If the separator and all the items in the array are html safe
  128
+  # then an html safe string is returned using <tt>Array#join</tt>,
  129
+  # otherwise the result of <tt>Array#join</tt> is returned without
  130
+  # marking it as html safe.
  131
+  #
  132
+  #   ["Mr", "Bojangles"].join.html_safe?
  133
+  #   # => false
  134
+  #
  135
+  #   ["Mr".html_safe, "Bojangles".html_safe].join.html_safe?
  136
+  #   # => true
  137
+  #
  138
+  def safe_join(sep=$,)
131 139
     sep ||= "".html_safe
132  
-    str = original_join(sep)
  140
+    str = join(sep)
133 141
     (sep.html_safe? && html_safe?) ? str.html_safe : str
134 142
   end
135 143
 
  144
+  # Returns +true+ if all items in the array are html safe.
  145
+  #
  146
+  #   [""].html_safe?
  147
+  #   # => false
  148
+  #
  149
+  #   ["".html_safe].html_safe?
  150
+  #   # => true
  151
+  #
136 152
   def html_safe?
137  
-    self.detect {|e| !e.html_safe?}.nil?
  153
+    detect { |e| !e.html_safe? }.nil?
138 154
   end
139  
-
140 155
 end
26  activesupport/test/core_ext/string_ext_test.rb
@@ -435,36 +435,36 @@ def to_s
435 435
   end
436 436
 
437 437
   test "Joining safe elements without a separator is safe" do
438  
-    array = 5.times.collect {"some string".html_safe}
439  
-    assert array.join.html_safe?
  438
+    array = 5.times.collect { "some string".html_safe }
  439
+    assert array.safe_join.html_safe?
440 440
   end
441 441
 
442 442
   test "Joining safe elements with a safe separator is safe" do
443  
-    array = 5.times.collect {"some string".html_safe}
444  
-    assert array.join("-".html_safe).html_safe?
  443
+    array = 5.times.collect { "some string".html_safe }
  444
+    assert array.safe_join("-".html_safe).html_safe?
445 445
   end
446 446
 
447 447
   test "Joining safe elements with an unsafe separator is unsafe" do
448  
-    array = 5.times.collect {"some string".html_safe}
449  
-    assert_false array.join("-").html_safe?
  448
+    array = 5.times.collect { "some string".html_safe }
  449
+    assert !array.safe_join("-").html_safe?
450 450
   end
451 451
 
452 452
   test "Joining is unsafe if any element is unsafe even with a safe separator" do
453  
-    array = 5.times.collect {"some string".html_safe}
  453
+    array = 5.times.collect { "some string".html_safe }
454 454
     array << "some string"
455  
-    assert_false array.join("-".html_safe).html_safe?
  455
+    assert !array.safe_join("-".html_safe).html_safe?
456 456
   end
457 457
 
458 458
   test "Joining is unsafe if any element is unsafe and no separator is given" do
459  
-    array = 5.times.collect {"some string".html_safe}
  459
+    array = 5.times.collect { "some string".html_safe }
460 460
     array << "some string"
461  
-    assert_false array.join.html_safe?
  461
+    assert !array.safe_join.html_safe?
462 462
   end
463 463
 
464 464
   test "Joining is unsafe if any element is unsafe and the separator is unsafe" do
465  
-    array = 5.times.collect {"some string".html_safe}
  465
+    array = 5.times.collect { "some string".html_safe }
466 466
     array << "some string"
467  
-    assert_false array.join("-").html_safe?
  467
+    assert !array.safe_join("-").html_safe?
468 468
   end
469 469
 
470 470
   test "Array is safe if all elements are safe" do
@@ -475,7 +475,7 @@ def to_s
475 475
   test "Array is unsafe if any element is unsafe" do
476 476
     array = 5.times.collect { "some string".html_safe }
477 477
     array << "some string"
478  
-    assert_false array.html_safe?
  478
+    assert !array.html_safe?
479 479
   end
480 480
 
481 481
   test 'emits normal string yaml' do

0 notes on commit 1a73407

Please sign in to comment.
Something went wrong with that request. Please try again.