Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3468503

Browse files
amatsudajhawthorn
authored andcommitted
Implement SafeBuffer#bytesplice
1 parent 7c70791 commit 3468503

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

activesupport/lib/active_support/core_ext/string/output_safety.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ def concat(value)
219219
end
220220
alias << concat
221221

222+
def bytesplice(*args, value)
223+
super(*args, implicit_html_escape_interpolated_argument(value))
224+
end
225+
222226
def insert(index, value)
223227
super(index, implicit_html_escape_interpolated_argument(value))
224228
end

activesupport/test/core_ext/string_ext_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,36 @@ def to_s
10091009
assert_predicate string, :html_safe?
10101010
end
10111011

1012+
if "".respond_to?(:bytesplice)
1013+
test "Bytesplicing safe into safe yields safe" do
1014+
string = "hello".html_safe
1015+
string.bytesplice(0, 0, "<b>".html_safe)
1016+
1017+
assert_equal "<b>hello", string
1018+
assert_predicate string, :html_safe?
1019+
1020+
string = "hello".html_safe
1021+
string.bytesplice(0..1, "<b>".html_safe)
1022+
1023+
assert_equal "<b>llo", string
1024+
assert_predicate string, :html_safe?
1025+
end
1026+
1027+
test "Bytesplicing unsafe into safe yields escaped safe" do
1028+
string = "hello".html_safe
1029+
string.bytesplice(1, 0, "<b>")
1030+
1031+
assert_equal "h&lt;b&gt;ello", string
1032+
assert_predicate string, :html_safe?
1033+
1034+
string = "hello".html_safe
1035+
string.bytesplice(1..2, "<b>")
1036+
1037+
assert_equal "h&lt;b&gt;lo", string
1038+
assert_predicate string, :html_safe?
1039+
end
1040+
end
1041+
10121042
test "emits normal string yaml" do
10131043
assert_equal "foo".to_yaml, "foo".html_safe.to_yaml(foo: 1)
10141044
end

0 commit comments

Comments
 (0)