Skip to content
This repository
Browse code

Removed Array#safe_join in AS core_ext and moved it to a view helper …

…with the same same.
  • Loading branch information...
commit 98c0c5db50a7679b3d58769ac22cb0a27a62c930 1 parent 1a73407
Josh Kalderimis authored February 10, 2011 josevalim committed February 10, 2011
4  actionpack/lib/action_view/helpers.rb
@@ -18,7 +18,7 @@ module Helpers #:nodoc:
18 18
     autoload :JavaScriptHelper, "action_view/helpers/javascript_helper"
19 19
     autoload :NumberHelper
20 20
     autoload :PrototypeHelper
21  
-    autoload :RawOutputHelper
  21
+    autoload :OutputSafetyHelper
22 22
     autoload :RecordTagHelper
23 23
     autoload :SanitizeHelper
24 24
     autoload :ScriptaculousHelper
@@ -48,7 +48,7 @@ module Helpers #:nodoc:
48 48
     include JavaScriptHelper
49 49
     include NumberHelper
50 50
     include PrototypeHelper
51  
-    include RawOutputHelper
  51
+    include OutputSafetyHelper
52 52
     include RecordTagHelper
53 53
     include SanitizeHelper
54 54
     include ScriptaculousHelper
40  actionpack/lib/action_view/helpers/output_safety_helper.rb
... ...
@@ -0,0 +1,40 @@
  1
+require 'active_support/core_ext/string/output_safety'
  2
+
  3
+module ActionView #:nodoc:
  4
+  # = Action View Raw Output Helper
  5
+  module Helpers #:nodoc:
  6
+    module OutputSafetyHelper
  7
+      # This method outputs without escaping a string. Since escaping tags is
  8
+      # now default, this can be used when you don't want Rails to automatically
  9
+      # escape tags. This is not recommended if the data is coming from the user's
  10
+      # input.
  11
+      #
  12
+      # For example:
  13
+      #
  14
+      # <%=raw @user.name %>
  15
+      def raw(stringish)
  16
+        stringish.to_s.html_safe
  17
+      end
  18
+
  19
+      # This method returns a html safe string using <tt>Array#join</tt> if all
  20
+      # the items in the array, including the supplied separator, are html safe.
  21
+      # Otherwise the result of <tt>Array#join</tt> is returned without marking
  22
+      # it as html safe.
  23
+      #
  24
+      #   safe_join(["Mr", "Bojangles"]).html_safe?
  25
+      #   # => false
  26
+      #
  27
+      #   safe_join(["Mr".html_safe, "Bojangles".html_safe]).html_safe?
  28
+      #   # => true
  29
+      #
  30
+      def safe_join(array, sep=$,)
  31
+        sep ||= "".html_safe
  32
+        str = array.join(sep)
  33
+
  34
+        is_html_safe = array.all? { |item| item.html_safe? }
  35
+
  36
+        (sep.html_safe? && is_html_safe) ? str.html_safe : str
  37
+      end
  38
+    end
  39
+  end
  40
+end
18  actionpack/lib/action_view/helpers/raw_output_helper.rb
... ...
@@ -1,18 +0,0 @@
1  
-module ActionView #:nodoc:
2  
-  # = Action View Raw Output Helper
3  
-  module Helpers #:nodoc:
4  
-    module RawOutputHelper
5  
-      # This method outputs without escaping a string. Since escaping tags is
6  
-      # now default, this can be used when you don't want Rails to automatically
7  
-      # escape tags. This is not recommended if the data is coming from the user's
8  
-      # input.
9  
-      #
10  
-      # For example:
11  
-      #
12  
-      # <%=raw @user.name %>
13  
-      def raw(stringish)
14  
-        stringish.to_s.html_safe
15  
-      end
16  
-    end
17  
-  end
18  
-end
55  actionpack/test/template/output_safety_helper_test.rb
... ...
@@ -0,0 +1,55 @@
  1
+require 'abstract_unit'
  2
+require 'testing_sandbox'
  3
+
  4
+class OutputSafetyHelperTest < ActionView::TestCase
  5
+  tests ActionView::Helpers::OutputSafetyHelper
  6
+  include TestingSandbox
  7
+
  8
+  def setup
  9
+    @string = "hello"
  10
+  end
  11
+
  12
+  test "raw returns the safe string" do
  13
+    result = raw(@string)
  14
+    assert_equal @string, result
  15
+    assert result.html_safe?
  16
+  end
  17
+
  18
+  test "raw handles nil values correctly" do
  19
+    assert_equal "", raw(nil)
  20
+  end
  21
+
  22
+  test "joining safe elements without a separator is safe" do
  23
+    array = 5.times.collect { "some string".html_safe }
  24
+    assert safe_join(array).html_safe?
  25
+  end
  26
+
  27
+  test "Joining safe elements with a safe separator is safe" do
  28
+    array = 5.times.collect { "some string".html_safe }
  29
+    assert safe_join(array, "-".html_safe).html_safe?
  30
+  end
  31
+
  32
+  test "Joining safe elements with an unsafe separator is unsafe" do
  33
+    array = 5.times.collect { "some string".html_safe }
  34
+    assert !safe_join(array, "-").html_safe?
  35
+  end
  36
+
  37
+  test "Joining is unsafe if any element is unsafe even with a safe separator" do
  38
+    array = 5.times.collect { "some string".html_safe }
  39
+    array << "some string"
  40
+    assert !safe_join(array, "-".html_safe).html_safe?
  41
+  end
  42
+
  43
+  test "Joining is unsafe if any element is unsafe and no separator is given" do
  44
+    array = 5.times.collect { "some string".html_safe }
  45
+    array << "some string"
  46
+    assert !safe_join(array).html_safe?
  47
+  end
  48
+
  49
+  test "Joining is unsafe if any element is unsafe and the separator is unsafe" do
  50
+    array = 5.times.collect { "some string".html_safe }
  51
+    array << "some string"
  52
+    assert !safe_join(array, "-").html_safe?
  53
+  end
  54
+
  55
+end
21  actionpack/test/template/raw_output_helper_test.rb
... ...
@@ -1,21 +0,0 @@
1  
-require 'abstract_unit'
2  
-require 'testing_sandbox'
3  
-
4  
-class RawOutputHelperTest < ActionView::TestCase
5  
-  tests ActionView::Helpers::RawOutputHelper
6  
-  include TestingSandbox
7  
-
8  
-  def setup
9  
-    @string = "hello"
10  
-  end
11  
-
12  
-  test "raw returns the safe string" do
13  
-    result = raw(@string)
14  
-    assert_equal @string, result
15  
-    assert result.html_safe?
16  
-  end
17  
-
18  
-  test "raw handles nil values correctly" do
19  
-    assert_equal "", raw(nil)
20  
-  end
21  
-end
31  activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -122,34 +122,3 @@ def html_safe
122 122
     ActiveSupport::SafeBuffer.new(self)
123 123
   end
124 124
 end
125  
-
126  
-class Array
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=$,)
139  
-    sep ||= "".html_safe
140  
-    str = join(sep)
141  
-    (sep.html_safe? && html_safe?) ? str.html_safe : str
142  
-  end
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  
-  #
152  
-  def html_safe?
153  
-    detect { |e| !e.html_safe? }.nil?
154  
-  end
155  
-end
44  activesupport/test/core_ext/string_ext_test.rb
@@ -434,50 +434,6 @@ def to_s
434 434
     assert string.html_safe?
435 435
   end
436 436
 
437  
-  test "Joining safe elements without a separator is safe" do
438  
-    array = 5.times.collect { "some string".html_safe }
439  
-    assert array.safe_join.html_safe?
440  
-  end
441  
-
442  
-  test "Joining safe elements with a safe separator is safe" do
443  
-    array = 5.times.collect { "some string".html_safe }
444  
-    assert array.safe_join("-".html_safe).html_safe?
445  
-  end
446  
-
447  
-  test "Joining safe elements with an unsafe separator is unsafe" do
448  
-    array = 5.times.collect { "some string".html_safe }
449  
-    assert !array.safe_join("-").html_safe?
450  
-  end
451  
-
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 }
454  
-    array << "some string"
455  
-    assert !array.safe_join("-".html_safe).html_safe?
456  
-  end
457  
-
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 }
460  
-    array << "some string"
461  
-    assert !array.safe_join.html_safe?
462  
-  end
463  
-
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 }
466  
-    array << "some string"
467  
-    assert !array.safe_join("-").html_safe?
468  
-  end
469  
-
470  
-  test "Array is safe if all elements are safe" do
471  
-    array = 5.times.collect { "some string".html_safe }
472  
-    assert array.html_safe?
473  
-  end
474  
-
475  
-  test "Array is unsafe if any element is unsafe" do
476  
-    array = 5.times.collect { "some string".html_safe }
477  
-    array << "some string"
478  
-    assert !array.html_safe?
479  
-  end
480  
-
481 437
   test 'emits normal string yaml' do
482 438
     assert_equal 'foo'.to_yaml, 'foo'.html_safe.to_yaml(:foo => 1)
483 439
   end

0 notes on commit 98c0c5d

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