From f76947906cafa1c6627d357f303fab0abf17356f Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Fri, 22 Sep 2023 10:33:15 -0400 Subject: [PATCH] Drop HTML module --- lib/phoenix_live_view/test/dom.ex | 33 ++++++++++++ test/phoenix_component/components_test.exs | 2 +- .../declarative_assigns_test.exs | 2 +- test/support/html.ex | 51 ------------------- 4 files changed, 35 insertions(+), 53 deletions(-) delete mode 100644 test/support/html.ex diff --git a/lib/phoenix_live_view/test/dom.ex b/lib/phoenix_live_view/test/dom.ex index 3b20f0112b..5fcb2e934c 100644 --- a/lib/phoenix_live_view/test/dom.ex +++ b/lib/phoenix_live_view/test/dom.ex @@ -569,4 +569,37 @@ defmodule Phoenix.LiveViewTest.DOM do {tag, new_attrs, children} end + + defmacro sigil_X({:<<>>, _, [binary]}, []) when is_binary(binary) do + Macro.escape(parse_sorted!(binary)) + end + + defmacro sigil_x(term, []) do + quote do + unquote(__MODULE__).parse_sorted!(unquote(term)) + end + end + + def t2h(template) do + template + |> Phoenix.LiveViewTest.rendered_to_string() + |> parse_sorted!() + end + + @doc""" + Parses HTML into Floki format with sorted attributes. + """ + def parse_sorted!(value) do + value + |> Floki.parse_fragment!() + |> Enum.map(&normalize_attribute_order/1) + end + + defp normalize_attribute_order({node_type, attributes, content}), + do: {node_type, Enum.sort(attributes), Enum.map(content, &normalize_attribute_order/1)} + + defp normalize_attribute_order(values) when is_list(values), + do: Enum.map(values, &normalize_attribute_order/1) + + defp normalize_attribute_order(value), do: value end diff --git a/test/phoenix_component/components_test.exs b/test/phoenix_component/components_test.exs index 92b55596aa..6168e3839d 100644 --- a/test/phoenix_component/components_test.exs +++ b/test/phoenix_component/components_test.exs @@ -3,7 +3,7 @@ defmodule Phoenix.LiveView.ComponentsTest do import Phoenix.HTML.Form import Phoenix.Component - import Phoenix.LiveViewTest.HTML + import Phoenix.LiveViewTest.DOM, only: [t2h: 1, sigil_X: 2, sigil_x: 2] describe "link patch" do test "basic usage" do diff --git a/test/phoenix_component/declarative_assigns_test.exs b/test/phoenix_component/declarative_assigns_test.exs index 853cdb2047..dc93c86c40 100644 --- a/test/phoenix_component/declarative_assigns_test.exs +++ b/test/phoenix_component/declarative_assigns_test.exs @@ -2,7 +2,7 @@ defmodule Phoenix.ComponentDeclarativeAssignsTest do use ExUnit.Case, async: true import Phoenix.LiveViewTest - import Phoenix.LiveViewTest.HTML + import Phoenix.LiveViewTest.DOM, only: [t2h: 1, sigil_X: 2] use Phoenix.Component defp render_template(mod, func, assigns) do diff --git a/test/support/html.ex b/test/support/html.ex deleted file mode 100644 index 732d52f10d..0000000000 --- a/test/support/html.ex +++ /dev/null @@ -1,51 +0,0 @@ -defmodule Phoenix.LiveViewTest.HTML do - defmacro sigil_X({:<<>>, _, [binary]}, []) when is_binary(binary) do - Macro.escape(parse_sorted!(binary)) - end - - defmacro sigil_x(term, []) do - quote bind_quoted: [term: term] do - parse_sorted!(term) - end - end - - def t2h(template) do - template - |> Phoenix.LiveViewTest.rendered_to_string() - |> parse_sorted!() - end - - @doc """ - This function will parse a binary into a list of in the format - of floki, however the attributes of any node are in sorted - order. - - ``` - {"node_name", [{"attribute_name", "attribute_value"}], [content]} - ``` - - or - - ``` - "string contents with no html/xml nodes" - ``` - - While soting the html attributes does mean we can't detect - differences in behavior, it also keeps the order of map - key/value from failing tests. - - """ - def parse_sorted!(value) do - value - |> Floki.parse_fragment!() - |> Enum.map(&normalize_attribute_order/1) - end - - defp normalize_attribute_order({node_type, attributes, content}), - do: {node_type, Enum.sort(attributes), Enum.map(content, &normalize_attribute_order/1)} - - defp normalize_attribute_order(values) when is_list(values), - do: Enum.map(values, &normalize_attribute_order/1) - - defp normalize_attribute_order(value), do: value -end