From baf69031dd9ef306d03e8a801da69fe7e3b9f962 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 13 Feb 2012 17:54:58 +0900 Subject: [PATCH] add AS::SafeBuffer#clone_empty --- .../lib/active_support/core_ext/string/output_safety.rb | 6 ++++++ activesupport/test/safe_buffer_test.rb | 9 +++++++++ 2 files changed, 15 insertions(+) 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 8876f1bbc2afa..445d11cf171dd 100644 --- a/activesupport/lib/active_support/core_ext/string/output_safety.rb +++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb @@ -101,6 +101,12 @@ def initialize_copy(other) @dirty = other.dirty? end + def clone_empty + new_safe_buffer = self[0, 0] + new_safe_buffer.instance_variable_set(:@dirty, @dirty) + new_safe_buffer + end + def concat(value) if dirty? || value.html_safe? super(value) diff --git a/activesupport/test/safe_buffer_test.rb b/activesupport/test/safe_buffer_test.rb index 2e2373a1b34f3..74015759ab9ed 100644 --- a/activesupport/test/safe_buffer_test.rb +++ b/activesupport/test/safe_buffer_test.rb @@ -102,4 +102,13 @@ def setup @buffer.safe_concat "BUSTED" end end + + test "clone_empty returns an empty buffer" do + assert_equal '', ActiveSupport::SafeBuffer.new('foo').clone_empty + end + + test "clone_empty keeps the original dirtyness" do + assert @buffer.clone_empty.html_safe? + assert !@buffer.gsub!('', '').clone_empty.html_safe? + end end