From 3cf23c3f891e2e81c977ea4ab83b62bc2a444b70 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 5 Jan 2023 05:25:37 +0900 Subject: [PATCH] Implement SafeBuffer#bytesplice --- .../core_ext/string/output_safety.rb | 4 +++ .../test/core_ext/string_ext_test.rb | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index a51f2f64cbe27..c436821c94a0b 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -987,6 +987,36 @@ def to_s assert_predicate string, :html_safe? end + if "".respond_to?(:bytesplice) + test "Bytesplicing safe into safe yields safe" do + string = "hello".html_safe + string.bytesplice(0, 0, "".html_safe) + + assert_equal "hello", string + assert_predicate string, :html_safe? + + string = "hello".html_safe + string.bytesplice(0..1, "".html_safe) + + assert_equal "llo", string + assert_predicate string, :html_safe? + end + + test "Bytesplicing unsafe into safe yields escaped safe" do + string = "hello".html_safe + string.bytesplice(1, 0, "") + + assert_equal "h<b>ello", string + assert_predicate string, :html_safe? + + string = "hello".html_safe + string.bytesplice(1..2, "") + + assert_equal "h<b>lo", string + assert_predicate string, :html_safe? + end + end + test "emits normal string yaml" do assert_equal "foo".to_yaml, "foo".html_safe.to_yaml(foo: 1) end