From 3f23221aa7410da703d7164eff421edd63e944f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 24 Jun 2024 22:43:02 +0200 Subject: [PATCH] Ensure matching escapes for nil and empty strings Closes #444. --- lib/phoenix_html/engine.ex | 1 + test/phoenix_html_test.exs | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/lib/phoenix_html/engine.ex b/lib/phoenix_html/engine.ex index 9da33f2c..7f801f19 100644 --- a/lib/phoenix_html/engine.ex +++ b/lib/phoenix_html/engine.ex @@ -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) diff --git a/test/phoenix_html_test.exs b/test/phoenix_html_test.exs index 913e5e9a..32f31537 100644 --- a/test/phoenix_html_test.exs +++ b/test/phoenix_html_test.exs @@ -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