Fix collision of Temple.textarea and Phoenix.HTML.Form.textarea

This commit is contained in:
Mitchell Hanberg 2019-07-04 11:21:54 -04:00
parent af2995a659
commit eabe9cdd9b
4 changed files with 111 additions and 5 deletions

View file

@ -34,8 +34,6 @@ defmodule Temple do
defmacro htm([do: block] = _block) do
quote do
import Kernel, except: [div: 2]
import Phoenix.HTML.Link, except: [link: 1, link: 2]
import Phoenix.HTML.Form, only: []
Temple.Utils.lexical_scope(fn ->
{:ok, var!(buff, Temple.Tags)} = Temple.Utils.start_buffer([])

View file

@ -66,7 +66,6 @@ defmodule Temple.Form do
:range_input,
:search_input,
:telephone_input,
:textarea,
:text_input,
:time_input,
:time_select,
@ -89,6 +88,19 @@ defmodule Temple.Form do
end
end
@doc """
Please see `Phoenix.HTML.Form.textarea/3` for details.
Note: Temple defines this function as `text_area` with an underscore, whereas Phoenix.HTML defines it as `textarea` without an underscore.
"""
defmacro text_area(form, field, opts \\ []) do
quote do
{:safe, input} = Phoenix.HTML.Form.textarea(unquote_splicing([form, field, opts]))
Utils.put_buffer(var!(buff, Temple.Tags), input)
end
end
@doc """
Please see `Phoenix.HTML.Form.reset/2` for details.
"""

View file

@ -384,7 +384,7 @@ defmodule Temple.FormTest do
assert result =~ ~s{name="bob"}
end
test "generates a textarea" do
test "generates a text_area/2" do
conn = %Plug.Conn{}
action = "/"
opts = []
@ -392,7 +392,23 @@ defmodule Temple.FormTest do
{:safe, result} =
htm do
form_for conn, action, opts do
textarea(form, :bob, class: "textarea-styles")
text_area(form, :bob)
end
end
assert result =~ ~s{<textarea}
assert result =~ ~s{name="bob"}
end
test "generates a text_area/3" do
conn = %Plug.Conn{}
action = "/"
opts = []
{:safe, result} =
htm do
form_for conn, action, opts do
text_area(form, :bob, class: "textarea-styles")
end
end

View file

@ -2,6 +2,86 @@ defmodule Temple.TagsTest do
use ExUnit.Case, async: true
use Temple
for tag <- Temple.Tags.nonvoid_elements() do
test "renders a #{tag}" do
{:safe, result} =
htm do
unquote(tag)()
end
assert result == ~s{<#{unquote(tag)}></#{unquote(tag)}>}
end
test "renders a #{tag} with attrs" do
{:safe, result} =
htm do
unquote(tag)(class: "hello")
end
assert result == ~s{<#{unquote(tag)} class="hello"></#{unquote(tag)}>}
end
test "renders a #{tag} with content" do
{:safe, result} =
htm do
unquote(tag)("Hi")
end
assert result == "<#{unquote(tag)}>Hi</#{unquote(tag)}>"
end
test "renders a #{tag} with attrs and content" do
{:safe, result} =
htm do
unquote(tag)("Hi", class: "hello")
end
assert result == ~s{<#{unquote(tag)} class="hello">Hi</#{unquote(tag)}>}
end
test "renders a #{tag} with a block" do
{:safe, result} =
htm do
unquote(tag)() do
unquote(tag)()
end
end
assert result == ~s{<#{unquote(tag)}><#{unquote(tag)}></#{unquote(tag)}></#{unquote(tag)}>}
end
test "renders a #{tag} with attrs and a block" do
{:safe, result} =
htm do
unquote(tag)(class: "hello") do
unquote(tag)()
end
end
assert result == ~s{<#{unquote(tag)} class="hello"><#{unquote(tag)}></#{unquote(tag)}></#{unquote(tag)}>}
end
end
for tag <- Temple.Tags.void_elements() do
test "renders a #{tag}" do
{:safe, result} =
htm do
unquote(tag)()
end
assert result == ~s{<#{unquote(tag)}>}
end
test "renders a #{tag} with attrs" do
{:safe, result} =
htm do
unquote(tag)(class: "hello")
end
assert result == ~s{<#{unquote(tag)} class="hello">}
end
end
describe "non-void elements" do
test "renders two divs" do
{:safe, result} =