Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash at (floki 0.37.0) lib/floki/finder.ex:56: Floki.Finder.find/2 #604

Open
mellelieuwes opened this issue Dec 10, 2024 · 0 comments
Open
Labels

Comments

@mellelieuwes
Copy link

mellelieuwes commented Dec 10, 2024

Description

Error in Phoenix Live View test code. The error:

16:43:41.594 [error] GenServer #PID<0.1169.0> terminating
** (MatchError) no match of right hand side value: []
(floki 0.37.0) lib/floki/finder.ex:56: Floki.Finder.find/2
(phoenix_live_view 1.0.0) lib/phoenix_live_view/test/dom.ex:70: Phoenix.LiveViewTest.DOM.targets_from_selector/2
(phoenix_live_view 1.0.0) lib/phoenix_live_view/test/client_proxy.ex:1063: anonymous fn/6 in Phoenix.LiveViewTest.ClientProxy.maybe_js_commands/6
(elixir 1.17.0) lib/enum.ex:4353: Enum.flat_map_list/2
(phoenix_live_view 1.0.0) lib/phoenix_live_view/test/client_proxy.ex:321: Phoenix.LiveViewTest.ClientProxy.handle_info/2
(stdlib 6.1.2) gen_server.erl:2345: :gen_server.try_handle_info/3
(stdlib 6.1.2) gen_server.erl:2433: :gen_server.handle_msg/6
(stdlib 6.1.2) proc_lib.erl:329: :proc_lib.init_p_do_apply/3
Last message: {:EXIT, #PID<0.1124.0>, {{:badmatch, []}, [{Floki.Finder, :find, 2, [file: ~c"lib/floki/finder.ex", line: 56]}, {Phoenix.LiveViewTest.DOM, :targets_from_selector, 2, [file: ~c"lib/phoenix_live_view/test/dom.ex", line: 70]}, {Phoenix.LiveViewTest.ClientProxy, :"-maybe_js_commands/6-fun-1-", 6, [file: ~c"lib/phoenix_live_view/test/client_proxy.ex", line: 1063]}, {Enum, :flat_map_list, 2, [file: ~c"lib/enum.ex", line: 4353]}, {Phoenix.LiveViewTest.ClientProxy, :handle_info, 2, [file: ~c"lib/phoenix_live_view/test/client_proxy.ex", line: 321]}, {:gen_server, :try_handle_info, 3, [file: ~c"gen_server.erl", line: 2345]}, {:gen_server, :handle_msg, 6, [file: ~c"gen_server.erl", line: 2433]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 329]}]}}

Cause

 def find(html_tree_as_tuple, selectors)
      when (is_list(html_tree_as_tuple) or is_html_node(html_tree_as_tuple)) and
             is_list(selectors) do
    if traverse_html_tuples?(selectors) do
      [selector] = selectors
      html_tree_as_tuple = List.wrap(html_tree_as_tuple)
      results = traverse_html_tuples(html_tree_as_tuple, selector, [])
      Enum.reverse(results)
    else
      tree = HTMLTree.build(html_tree_as_tuple)
      results = find(tree, selectors)
      Enum.map(results, fn html_node -> HTMLTree.to_tuple(tree, html_node) end)
    end
  end

On line 55 traverse_html_tuples?(selectors) can result in true while selectors list is empty due to this line:

defp traverse_html_tuples?([]), do: true

This results in a no match of right hand side value: [] error on line 56:

[selector] = selectors

Possible fix

Return false when selector list is empty:
defp traverse_html_tuples?([]), do: false

Phoenix Live View test code

    test "a valid token activates the account", %{conn: conn} do
      user = Factories.insert!(:member, %{confirmed_at: nil})

      token =
        extract_user_token(fn url ->
          Account.Public.deliver_user_confirmation_instructions(user, url)
        end)

      {:ok, view, _html} = live(conn, ~p"/user/confirm/#{token}")

      {:error, {:redirect, %{to: to}}} =
        view
        |> element("[phx-click=\"confirm\"]")
        |> render_click()

      assert to == "/user/signin?#{URI.encode_query(%{email: user.email})}"
      assert Account.Public.get_user!(user.id).confirmed_at
    end 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant