From 3993c798c004af3c84cf729630a0e3deee13c00a Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Wed, 8 Apr 2020 22:54:09 -0400 Subject: [PATCH] Join markup with a new line Text nodes separated by new lines still show whitespace when rendered, so we should maintain user specified new lines. Closes #59 Closes #60 --- lib/temple/utils.ex | 2 +- test/support/component.ex | 2 +- test/support/utils.ex | 22 ++++++++++++++++++++++ test/temple/elements_test.exs | 1 + test/temple/form_test.exs | 1 + test/temple/html_test.exs | 1 + test/temple/link_test.exs | 1 + test/temple/svg_test.exs | 2 +- test/temple_test.exs | 31 ++++++++++++++++--------------- 9 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 test/support/utils.ex diff --git a/lib/temple/utils.ex b/lib/temple/utils.ex index 4e4f631..93f0ce1 100644 --- a/lib/temple/utils.ex +++ b/lib/temple/utils.ex @@ -62,7 +62,7 @@ defmodule Temple.Utils do end def join_and_escape(markup) do - markup |> Enum.reverse() |> Enum.join("") |> Phoenix.HTML.raw() + markup |> Enum.reverse() |> Enum.join("\n") |> Phoenix.HTML.raw() end def start_buffer(initial_buffer), do: Agent.start(fn -> initial_buffer end) diff --git a/test/support/component.ex b/test/support/component.ex index a33887a..fab56f7 100644 --- a/test/support/component.ex +++ b/test/support/component.ex @@ -1,4 +1,4 @@ -defmodule Component do +defmodule Temple.Support.Component do import Temple defcomponent :flex do diff --git a/test/support/utils.ex b/test/support/utils.ex new file mode 100644 index 0000000..8d7dee7 --- /dev/null +++ b/test/support/utils.ex @@ -0,0 +1,22 @@ +defmodule Temple.Support.Utils do + defmacro __using__(_) do + quote do + import Kernel, except: [==: 2, =~: 2] + import unquote(__MODULE__) + end + end + + def a == b when is_binary(a) and is_binary(b) do + Kernel.==( + String.replace(a, ~r/\n/, ""), + String.replace(b, ~r/\n/, "") + ) + end + + def a =~ b when is_binary(a) and is_binary(b) do + Kernel.=~( + String.replace(a, ~r/\n/, ""), + String.replace(b, ~r/\n/, "") + ) + end +end diff --git a/test/temple/elements_test.exs b/test/temple/elements_test.exs index c3b35b8..0e4d948 100644 --- a/test/temple/elements_test.exs +++ b/test/temple/elements_test.exs @@ -3,6 +3,7 @@ defmodule Temple.ElementsTest do import Temple.Elements, only: [defelement: 2] import Temple, only: [temple: 1, text: 1] import Temple.Html, only: [option: 2] + use Temple.Support.Utils defelement(:my_select, :nonvoid) defelement(:my_input, :void) diff --git a/test/temple/form_test.exs b/test/temple/form_test.exs index 9d6d9fe..53f62c8 100644 --- a/test/temple/form_test.exs +++ b/test/temple/form_test.exs @@ -1,6 +1,7 @@ defmodule Temple.FormTest do use ExUnit.Case, async: true use Temple + use Temple.Support.Utils describe "form_for" do test "returns a form tag" do diff --git a/test/temple/html_test.exs b/test/temple/html_test.exs index f203d56..4bd8380 100644 --- a/test/temple/html_test.exs +++ b/test/temple/html_test.exs @@ -1,6 +1,7 @@ defmodule Temple.HtmlTest do use ExUnit.Case, async: true use Temple + use Temple.Support.Utils test "renders a html with a block" do {:safe, result} = diff --git a/test/temple/link_test.exs b/test/temple/link_test.exs index f70f868..4222db1 100644 --- a/test/temple/link_test.exs +++ b/test/temple/link_test.exs @@ -1,6 +1,7 @@ defmodule Temple.LinkTest do use ExUnit.Case, async: true use Temple + use Temple.Support.Utils describe "phx_link" do test "emits a link" do diff --git a/test/temple/svg_test.exs b/test/temple/svg_test.exs index dff143f..e2e1668 100644 --- a/test/temple/svg_test.exs +++ b/test/temple/svg_test.exs @@ -1,8 +1,8 @@ defmodule Temple.SvgTest do use ExUnit.Case, async: true import Temple - import Temple.Svg import Temple.Utils, only: [to_valid_tag: 1] + use Temple.Support.Utils for tag <- Temple.Svg.elements() -- [:text_] do test "renders a #{tag}" do diff --git a/test/temple_test.exs b/test/temple_test.exs index 699dfe4..251340e 100644 --- a/test/temple_test.exs +++ b/test/temple_test.exs @@ -1,10 +1,11 @@ defmodule TempleTest do use ExUnit.Case, async: true use Temple + use Temple.Support.Utils describe "custom component" do test "defcomponent works when requiring the module" do - require Component, as: C + require Temple.Support.Component, as: C {:safe, result} = temple do @@ -23,7 +24,7 @@ defmodule TempleTest do end test "defines a basic component" do - import Component + import Temple.Support.Component {:safe, result} = temple do @@ -34,7 +35,7 @@ defmodule TempleTest do end test "defines a component that takes 1 child" do - import Component + import Temple.Support.Component {:safe, result} = temple do @@ -48,7 +49,7 @@ defmodule TempleTest do end test "defines a component that takes multiple children" do - import Component + import Temple.Support.Component {:safe, result} = temple do @@ -63,7 +64,7 @@ defmodule TempleTest do end test "can access a prop" do - import Component + import Temple.Support.Component {:safe, result} = temple do @@ -77,7 +78,7 @@ defmodule TempleTest do end test "can access assigns list" do - import Component + import Temple.Support.Component assigns = [foo: "bar", hello: "world"] @@ -90,7 +91,7 @@ defmodule TempleTest do end test "can access assigns map" do - import Component + import Temple.Support.Component assigns = %{foo: "bar", hello: "world"} @@ -103,7 +104,7 @@ defmodule TempleTest do end test "can have arbitrary code inside the definition" do - import Component + import Temple.Support.Component {:safe, result} = temple do @@ -114,7 +115,7 @@ defmodule TempleTest do end test "can use conditionals to render different markup" do - import Component + import Temple.Support.Component {:safe, result} = temple do @@ -126,7 +127,7 @@ defmodule TempleTest do end test "can pass arbitrary data as assigns" do - import Component + import Temple.Support.Component {:safe, result} = temple do @@ -140,7 +141,7 @@ defmodule TempleTest do end test "can pass a variable as a prop" do - import Component + import Temple.Support.Component bob = "hi" @@ -153,7 +154,7 @@ defmodule TempleTest do end test "can pass a variable as a prop to a component with a block" do - import Component + import Temple.Support.Component bob = "hi" @@ -168,7 +169,7 @@ defmodule TempleTest do end test "can pass all of the assigns as a variable" do - import Component + import Temple.Support.Component assigns = [bob: "hi"] @@ -181,7 +182,7 @@ defmodule TempleTest do end test "can pass all of the assigns as a variable with a block" do - import Component + import Temple.Support.Component assigns = [bob: "hi"] @@ -196,7 +197,7 @@ defmodule TempleTest do end test "can pass a map as assigns with a block" do - import Component + import Temple.Support.Component assigns = %{bob: "hi"}