Skip to content

Commit

Permalink
Ensure matching escapes for nil and empty strings
Browse files Browse the repository at this point in the history
Closes #444.
  • Loading branch information
josevalim committed Jun 24, 2024
1 parent 81782d9 commit 3f23221
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/phoenix_html/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Phoenix.HTML.Engine do
"""
def encode_to_iodata!({:safe, body}), do: body
def encode_to_iodata!(nil), do: ""
def encode_to_iodata!(""), do: ""
def encode_to_iodata!(bin) when is_binary(bin), do: html_escape(bin)
def encode_to_iodata!(list) when is_list(list), do: Phoenix.HTML.Safe.List.to_iodata(list)
def encode_to_iodata!(other), do: Phoenix.HTML.Safe.to_iodata(other)
Expand Down
6 changes: 6 additions & 0 deletions test/phoenix_html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ defmodule Phoenix.HTMLTest do
html_escape(~c"foo🐥")
end
end

test "equivalences" do
# Since some HTML code may compare html_escape("") with html_escape(nil),
# we make sure they have equivalent representations.
assert html_escape("") == html_escape(nil)
end
end

describe "attributes_escape" do
Expand Down

0 comments on commit 3f23221

Please sign in to comment.