Skip to content

Commit 3cf23c3

Browse files
amatsudajhawthorn
authored andcommitted
Implement SafeBuffer#bytesplice
1 parent f0c5e60 commit 3cf23c3

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Diff for: activesupport/lib/active_support/core_ext/string/output_safety.rb

+4
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ def concat(value)
216216
end
217217
alias << concat
218218

219+
def bytesplice(*args, value)
220+
super(*args, implicit_html_escape_interpolated_argument(value))
221+
end
222+
219223
def insert(index, value)
220224
super(index, html_escape_interpolated_argument(value))
221225
end

Diff for: activesupport/test/core_ext/string_ext_test.rb

+30
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,36 @@ def to_s
987987
assert_predicate string, :html_safe?
988988
end
989989

990+
if "".respond_to?(:bytesplice)
991+
test "Bytesplicing safe into safe yields safe" do
992+
string = "hello".html_safe
993+
string.bytesplice(0, 0, "<b>".html_safe)
994+
995+
assert_equal "<b>hello", string
996+
assert_predicate string, :html_safe?
997+
998+
string = "hello".html_safe
999+
string.bytesplice(0..1, "<b>".html_safe)
1000+
1001+
assert_equal "<b>llo", string
1002+
assert_predicate string, :html_safe?
1003+
end
1004+
1005+
test "Bytesplicing unsafe into safe yields escaped safe" do
1006+
string = "hello".html_safe
1007+
string.bytesplice(1, 0, "<b>")
1008+
1009+
assert_equal "h&lt;b&gt;ello", string
1010+
assert_predicate string, :html_safe?
1011+
1012+
string = "hello".html_safe
1013+
string.bytesplice(1..2, "<b>")
1014+
1015+
assert_equal "h&lt;b&gt;lo", string
1016+
assert_predicate string, :html_safe?
1017+
end
1018+
end
1019+
9901020
test "emits normal string yaml" do
9911021
assert_equal "foo".to_yaml, "foo".html_safe.to_yaml(foo: 1)
9921022
end

0 commit comments

Comments
 (0)