Skip to content

Commit

Permalink
Implement SafeBuffer#bytesplice
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda authored and jhawthorn committed Mar 13, 2023
1 parent f0c5e60 commit 3cf23c3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ def concat(value)
end
alias << concat

def bytesplice(*args, value)
super(*args, implicit_html_escape_interpolated_argument(value))
end

def insert(index, value)
super(index, html_escape_interpolated_argument(value))
end
Expand Down
30 changes: 30 additions & 0 deletions activesupport/test/core_ext/string_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<b>".html_safe)

assert_equal "<b>hello", string
assert_predicate string, :html_safe?

string = "hello".html_safe
string.bytesplice(0..1, "<b>".html_safe)

assert_equal "<b>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, "<b>")

assert_equal "h&lt;b&gt;ello", string
assert_predicate string, :html_safe?

string = "hello".html_safe
string.bytesplice(1..2, "<b>")

assert_equal "h&lt;b&gt;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
Expand Down

0 comments on commit 3cf23c3

Please sign in to comment.