Skip to content

Commit 8cd0690

Browse files
SteffenDEshunjilin
andauthored
Ensure textarea content is not trimmed in TreeDOM (#3935)
* test: add test for multiline textarea rendering * Ensure textarea content is not trimmed in TreeDOM Closes #3928. --------- Co-authored-by: Shunji Lin <[email protected]>
1 parent 512b382 commit 8cd0690

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

lib/phoenix_live_view/test/client_proxy.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
14311431
end
14321432

14331433
defp form_defaults("textarea", node, name, acc) do
1434-
value = TreeDOM.to_text(node)
1434+
value = TreeDOM.to_text(node, false)
14351435

14361436
if value == "" do
14371437
Plug.Conn.Query.decode_each({name, ""}, acc)

lib/phoenix_live_view/test/tree_dom.ex

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,19 @@ defmodule Phoenix.LiveViewTest.TreeDOM do
6262
@doc """
6363
Returns the text representation of the node, removing extra whitespace.
6464
"""
65-
def to_text(tree) do
66-
tree
67-
|> node_to_text()
68-
|> Enum.join()
69-
|> String.replace(~r/[\s]+/, " ")
70-
|> String.trim()
65+
def to_text(tree, trim \\ true) do
66+
text =
67+
tree
68+
|> node_to_text()
69+
|> Enum.join()
70+
71+
if trim do
72+
text
73+
|> String.replace(~r/[\s]+/, " ")
74+
|> String.trim()
75+
else
76+
text
77+
end
7178
end
7279

7380
defp node_to_text({_tag, _attrs, content}), do: node_to_text(content)

test/phoenix_live_view/integrations/elements_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,17 @@ defmodule Phoenix.LiveView.ElementsTest do
618618
end
619619

620620
describe "submit_form" do
621+
test "submits textarea with newline characters", %{live: view} do
622+
view
623+
|> form("#form")
624+
|> render_submit()
625+
626+
expected_string_in_event =
627+
"textarea_with_newlines\" => \"This is a test.\\nIt has multiple\\nlines of text.\""
628+
629+
assert last_event(view) =~ expected_string_in_event
630+
end
631+
621632
test "raises if element is not a form", %{live: view, conn: conn} do
622633
assert_raise ArgumentError,
623634
~r"given element did not return a form",

test/support/live_views/elements.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ defmodule Phoenix.LiveViewTest.Support.ElementsLive do
203203
</select>
204204
<textarea name="hello[textarea]">Text</textarea>
205205
<textarea name="hello[textarea_empty]"></textarea>
206+
<textarea name="hello[textarea_with_newlines]"><%= @multiline_text %></textarea>
206207
<!-- Mimic textarea from Phoenix.HTML -->
207208
<textarea name="hello[textarea_nl]">
208209
Text</textarea>
@@ -278,6 +279,7 @@ defmodule Phoenix.LiveViewTest.Support.ElementsLive do
278279
socket
279280
|> assign(:event, nil)
280281
|> assign(:trigger_action, false)
282+
|> assign(:multiline_text, "This is a test.\nIt has multiple\nlines of text.")
281283

282284
{:ok, socket}
283285
end

0 commit comments

Comments
 (0)