Skip to content
This repository
Browse code

add AS::SafeBuffer#clone_empty

  • Loading branch information...
commit e50ee96a0b37e7c5adfc555edd402ad04cc159f1 1 parent 5b4082f
Akira Matsuda authored February 13, 2012 tenderlove committed February 20, 2012
6  activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -100,6 +100,12 @@ def initialize_copy(other)
100 100
       @dirty = other.dirty?
101 101
     end
102 102
 
  103
+    def clone_empty
  104
+      new_safe_buffer = self[0, 0]
  105
+      new_safe_buffer.instance_variable_set(:@dirty, @dirty)
  106
+      new_safe_buffer
  107
+    end
  108
+
103 109
     def concat(value)
104 110
       if dirty? || value.html_safe?
105 111
         super(value)
9  activesupport/test/safe_buffer_test.rb
@@ -83,4 +83,13 @@ def setup
83 83
       @buffer.safe_concat "BUSTED"
84 84
     end
85 85
   end
  86
+
  87
+  test "clone_empty returns an empty buffer" do
  88
+    assert_equal '', ActiveSupport::SafeBuffer.new('foo').clone_empty
  89
+  end
  90
+
  91
+  test "clone_empty keeps the original dirtyness" do
  92
+    assert @buffer.clone_empty.html_safe?
  93
+    assert !@buffer.gsub!('', '').clone_empty.html_safe?
  94
+  end
86 95
 end

0 notes on commit e50ee96

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