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
This commit is contained in:
Mitchell Hanberg 2020-04-08 22:54:09 -04:00
parent 1093a4d602
commit 3993c798c0
9 changed files with 45 additions and 18 deletions

View file

@ -62,7 +62,7 @@ defmodule Temple.Utils do
end end
def join_and_escape(markup) do def join_and_escape(markup) do
markup |> Enum.reverse() |> Enum.join("") |> Phoenix.HTML.raw() markup |> Enum.reverse() |> Enum.join("\n") |> Phoenix.HTML.raw()
end end
def start_buffer(initial_buffer), do: Agent.start(fn -> initial_buffer end) def start_buffer(initial_buffer), do: Agent.start(fn -> initial_buffer end)

View file

@ -1,4 +1,4 @@
defmodule Component do defmodule Temple.Support.Component do
import Temple import Temple
defcomponent :flex do defcomponent :flex do

22
test/support/utils.ex Normal file
View file

@ -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

View file

@ -3,6 +3,7 @@ defmodule Temple.ElementsTest do
import Temple.Elements, only: [defelement: 2] import Temple.Elements, only: [defelement: 2]
import Temple, only: [temple: 1, text: 1] import Temple, only: [temple: 1, text: 1]
import Temple.Html, only: [option: 2] import Temple.Html, only: [option: 2]
use Temple.Support.Utils
defelement(:my_select, :nonvoid) defelement(:my_select, :nonvoid)
defelement(:my_input, :void) defelement(:my_input, :void)

View file

@ -1,6 +1,7 @@
defmodule Temple.FormTest do defmodule Temple.FormTest do
use ExUnit.Case, async: true use ExUnit.Case, async: true
use Temple use Temple
use Temple.Support.Utils
describe "form_for" do describe "form_for" do
test "returns a form tag" do test "returns a form tag" do

View file

@ -1,6 +1,7 @@
defmodule Temple.HtmlTest do defmodule Temple.HtmlTest do
use ExUnit.Case, async: true use ExUnit.Case, async: true
use Temple use Temple
use Temple.Support.Utils
test "renders a html with a block" do test "renders a html with a block" do
{:safe, result} = {:safe, result} =

View file

@ -1,6 +1,7 @@
defmodule Temple.LinkTest do defmodule Temple.LinkTest do
use ExUnit.Case, async: true use ExUnit.Case, async: true
use Temple use Temple
use Temple.Support.Utils
describe "phx_link" do describe "phx_link" do
test "emits a link" do test "emits a link" do

View file

@ -1,8 +1,8 @@
defmodule Temple.SvgTest do defmodule Temple.SvgTest do
use ExUnit.Case, async: true use ExUnit.Case, async: true
import Temple import Temple
import Temple.Svg
import Temple.Utils, only: [to_valid_tag: 1] import Temple.Utils, only: [to_valid_tag: 1]
use Temple.Support.Utils
for tag <- Temple.Svg.elements() -- [:text_] do for tag <- Temple.Svg.elements() -- [:text_] do
test "renders a #{tag}" do test "renders a #{tag}" do

View file

@ -1,10 +1,11 @@
defmodule TempleTest do defmodule TempleTest do
use ExUnit.Case, async: true use ExUnit.Case, async: true
use Temple use Temple
use Temple.Support.Utils
describe "custom component" do describe "custom component" do
test "defcomponent works when requiring the module" do test "defcomponent works when requiring the module" do
require Component, as: C require Temple.Support.Component, as: C
{:safe, result} = {:safe, result} =
temple do temple do
@ -23,7 +24,7 @@ defmodule TempleTest do
end end
test "defines a basic component" do test "defines a basic component" do
import Component import Temple.Support.Component
{:safe, result} = {:safe, result} =
temple do temple do
@ -34,7 +35,7 @@ defmodule TempleTest do
end end
test "defines a component that takes 1 child" do test "defines a component that takes 1 child" do
import Component import Temple.Support.Component
{:safe, result} = {:safe, result} =
temple do temple do
@ -48,7 +49,7 @@ defmodule TempleTest do
end end
test "defines a component that takes multiple children" do test "defines a component that takes multiple children" do
import Component import Temple.Support.Component
{:safe, result} = {:safe, result} =
temple do temple do
@ -63,7 +64,7 @@ defmodule TempleTest do
end end
test "can access a prop" do test "can access a prop" do
import Component import Temple.Support.Component
{:safe, result} = {:safe, result} =
temple do temple do
@ -77,7 +78,7 @@ defmodule TempleTest do
end end
test "can access assigns list" do test "can access assigns list" do
import Component import Temple.Support.Component
assigns = [foo: "bar", hello: "world"] assigns = [foo: "bar", hello: "world"]
@ -90,7 +91,7 @@ defmodule TempleTest do
end end
test "can access assigns map" do test "can access assigns map" do
import Component import Temple.Support.Component
assigns = %{foo: "bar", hello: "world"} assigns = %{foo: "bar", hello: "world"}
@ -103,7 +104,7 @@ defmodule TempleTest do
end end
test "can have arbitrary code inside the definition" do test "can have arbitrary code inside the definition" do
import Component import Temple.Support.Component
{:safe, result} = {:safe, result} =
temple do temple do
@ -114,7 +115,7 @@ defmodule TempleTest do
end end
test "can use conditionals to render different markup" do test "can use conditionals to render different markup" do
import Component import Temple.Support.Component
{:safe, result} = {:safe, result} =
temple do temple do
@ -126,7 +127,7 @@ defmodule TempleTest do
end end
test "can pass arbitrary data as assigns" do test "can pass arbitrary data as assigns" do
import Component import Temple.Support.Component
{:safe, result} = {:safe, result} =
temple do temple do
@ -140,7 +141,7 @@ defmodule TempleTest do
end end
test "can pass a variable as a prop" do test "can pass a variable as a prop" do
import Component import Temple.Support.Component
bob = "hi" bob = "hi"
@ -153,7 +154,7 @@ defmodule TempleTest do
end end
test "can pass a variable as a prop to a component with a block" do test "can pass a variable as a prop to a component with a block" do
import Component import Temple.Support.Component
bob = "hi" bob = "hi"
@ -168,7 +169,7 @@ defmodule TempleTest do
end end
test "can pass all of the assigns as a variable" do test "can pass all of the assigns as a variable" do
import Component import Temple.Support.Component
assigns = [bob: "hi"] assigns = [bob: "hi"]
@ -181,7 +182,7 @@ defmodule TempleTest do
end end
test "can pass all of the assigns as a variable with a block" do test "can pass all of the assigns as a variable with a block" do
import Component import Temple.Support.Component
assigns = [bob: "hi"] assigns = [bob: "hi"]
@ -196,7 +197,7 @@ defmodule TempleTest do
end end
test "can pass a map as assigns with a block" do test "can pass a map as assigns with a block" do
import Component import Temple.Support.Component
assigns = %{bob: "hi"} assigns = %{bob: "hi"}