Skip to content

Commit

Permalink
Element text selector: Normalize whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ftes committed Jan 9, 2025
1 parent 38be0a1 commit 7d295f9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/phoenix_live_view/test/dom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ defmodule Phoenix.LiveViewTest.DOM do

def to_html(html_tree), do: Floki.raw_html(html_tree)

def to_text(html_tree), do: Floki.text(html_tree)
def to_text(html_tree) do
html_tree
|> Floki.text()
|> String.replace(~r/[\s]+/, " ")
|> String.trim()
end

# TODO: rewrite to use Floki.get_by_id/2
# currently it does not raise when multiple elements are found
Expand Down
1 change: 1 addition & 0 deletions test/phoenix_live_view/integrations/elements_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Phoenix.LiveView.ElementsTest do
assert view |> element("#scoped-render") |> has_element?()
assert view |> element("div", "This is a div") |> has_element?()
assert view |> element("#scoped-render", ~r/^This is a div$/) |> has_element?()
assert view |> element("span", "Normalize whitespace") |> has_element?()

refute view |> element("#unknown") |> has_element?()
refute view |> element("div", "no matching text") |> has_element?()
Expand Down
4 changes: 4 additions & 0 deletions test/support/live_views/elements.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ defmodule Phoenix.LiveViewTest.Support.ElementsLive do
<div id="child-component">
<.live_component module={Phoenix.LiveViewTest.Support.ElementsComponent} id={1} />
</div>
<span phx-no-format>
Normalize
<span> whitespace</span>
</span>
{# basic render_*}
<span id="span-no-attr">This is a span</span>
Expand Down

0 comments on commit 7d295f9

Please sign in to comment.