aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Monette <richard.monette@shopify.com>2019-03-13 18:28:43 -0400
committerRichard Monette <richard.monette@shopify.com>2019-03-13 19:22:04 -0400
commitaf2052295488fcd3dda88ef11ebd211cf777305f (patch)
treee423d0b8ab38de9a9c0103fb75ca60fe2adaa0d6
parenta783e4171241480324be575259de32a49d59405d (diff)
downloadrails-af2052295488fcd3dda88ef11ebd211cf777305f.tar.gz
rails-af2052295488fcd3dda88ef11ebd211cf777305f.tar.bz2
rails-af2052295488fcd3dda88ef11ebd211cf777305f.zip
support slice assignment on SafeBuffer
-rw-r--r--activesupport/lib/active_support/core_ext/string/output_safety.rb8
-rw-r--r--activesupport/test/safe_buffer_test.rb32
2 files changed, 38 insertions, 2 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 3a80de4617..0b40c3d799 100644
--- a/activesupport/lib/active_support/core_ext/string/output_safety.rb
+++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -199,8 +199,12 @@ module ActiveSupport #:nodoc:
super(html_escape_interpolated_argument(value))
end
- def []=(index, value)
- super(index, html_escape_interpolated_argument(value))
+ def []=(*args)
+ if args.count == 3
+ super(args[0], args[1], html_escape_interpolated_argument(args[2]))
+ else
+ super(args[0], html_escape_interpolated_argument(args[1]))
+ end
end
def +(other)
diff --git a/activesupport/test/safe_buffer_test.rb b/activesupport/test/safe_buffer_test.rb
index 08d04e3223..32e1d2d5bf 100644
--- a/activesupport/test/safe_buffer_test.rb
+++ b/activesupport/test/safe_buffer_test.rb
@@ -112,6 +112,38 @@ class SafeBufferTest < ActiveSupport::TestCase
end
end
+ test "can assign value into zero-index" do
+ buffer = ActiveSupport::SafeBuffer.new("012345")
+
+ buffer[0] = "<"
+
+ assert_equal "&lt;12345", buffer
+ end
+
+ test "can assign value into non zero-index" do
+ buffer = ActiveSupport::SafeBuffer.new("012345")
+
+ buffer[2] = "<"
+
+ assert_equal "01&lt;345", buffer
+ end
+
+ test "can assign value into slice" do
+ buffer = ActiveSupport::SafeBuffer.new("012345")
+
+ buffer[0, 3] = "<"
+
+ assert_equal "&lt;345", buffer
+ end
+
+ test "can assign value into offset slice" do
+ buffer = ActiveSupport::SafeBuffer.new("012345")
+
+ buffer[1, 3] = "<"
+
+ assert_equal "0&lt;45", buffer
+ end
+
test "Should escape dirty buffers on add" do
clean = "hello".html_safe
@buffer.gsub!("", "<>")