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 7c70791 commit 3468503
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Expand Up @@ -219,6 +219,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, implicit_html_escape_interpolated_argument(value))
end
Expand Down
30 changes: 30 additions & 0 deletions activesupport/test/core_ext/string_ext_test.rb
Expand Up @@ -1009,6 +1009,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 3468503

Please sign in to comment.