Skip to content

Commit

Permalink
Depend on latest Plug and use more efficient query decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 6, 2023
1 parent 1c6b997 commit 3341126
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
25 changes: 12 additions & 13 deletions lib/phoenix_live_view/test/client_proxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
connect_params: %{},
connect_info: %{}

alias Plug.Conn.Query
alias Phoenix.LiveViewTest.{ClientProxy, DOM, Element, View, Upload}

@doc """
Expand Down Expand Up @@ -1057,20 +1058,20 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
tag == "form" ->
defaults =
node
|> DOM.reverse_filter(fn node ->
|> DOM.filter(fn node ->
DOM.tag(node) in ~w(input textarea select) and is_nil(DOM.attribute(node, "disabled"))
end)
|> Enum.reduce(%{}, &form_defaults/2)
|> Enum.reduce(Query.decode_init(), &form_defaults/2)

with {:ok, defaults} <- maybe_submitter(defaults, type, node, element),
{:ok, value} <- fill_in_map(Enum.to_list(element.form_data || %{}), "", node, []) do
{:ok, DOM.deep_merge(defaults, value)}
{:ok, DOM.deep_merge(Query.decode_done(defaults), value)}
else
{:error, _, _} = error -> error
end

type == :change and tag in ~w(input select textarea) ->
{:ok, form_defaults(node, %{})}
{:ok, form_defaults(node, Query.decode_init()) |> Query.decode_done()}

true ->
{:error, :invalid, "phx-#{type} is only allowed in forms, got #{inspect(tag)}"}
Expand Down Expand Up @@ -1103,7 +1104,7 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
"form submitter selected by #{inspect(element.selector)} must have a name"}

submitter?(node) and is_nil(DOM.attribute(node, "disabled")) ->
{:ok, Plug.Conn.Query.decode_pair({name, DOM.attribute(node, "value")}, defaults)}
{:ok, Plug.Conn.Query.decode_each({name, DOM.attribute(node, "value")}, defaults)}

true ->
{:error, :invalid,
Expand Down Expand Up @@ -1178,19 +1179,17 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
)
end

all_selected
|> Enum.reverse()
|> Enum.reduce(acc, fn selected, acc ->
Plug.Conn.Query.decode_pair({name, DOM.attribute(selected, "value")}, acc)
Enum.reduce(all_selected, acc, fn selected, acc ->
Plug.Conn.Query.decode_each({name, DOM.attribute(selected, "value")}, acc)
end)
end

defp form_defaults({"textarea", _, []}, name, acc) do
Plug.Conn.Query.decode_pair({name, ""}, acc)
Plug.Conn.Query.decode_each({name, ""}, acc)
end

defp form_defaults({"textarea", _, [value]}, name, acc) do
Plug.Conn.Query.decode_pair({name, String.replace_prefix(value, "\n", "")}, acc)
Plug.Conn.Query.decode_each({name, String.replace_prefix(value, "\n", "")}, acc)
end

defp form_defaults({"input", _, _} = node, name, acc) do
Expand All @@ -1200,7 +1199,7 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
cond do
type in ["radio", "checkbox"] ->
if DOM.attribute(node, "checked") do
Plug.Conn.Query.decode_pair({name, value}, acc)
Plug.Conn.Query.decode_each({name, value}, acc)
else
acc
end
Expand All @@ -1209,7 +1208,7 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
acc

true ->
Plug.Conn.Query.decode_pair({name, value}, acc)
Plug.Conn.Query.decode_each({name, value}, acc)
end
end

Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ defmodule Phoenix.LiveView.MixProject do
defp deps do
[
{:phoenix, "~> 1.6.15 or ~> 1.7.0"},
{:plug, "~> 1.15"},
{:phoenix_view, "~> 2.0", optional: true},
{:phoenix_template, "~> 1.0"},
{:phoenix_html, "~> 3.3"},
Expand Down
6 changes: 3 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
"makeup_html": {:hex, :makeup_html, "0.1.0", "b0228fda985e311d8f0d25bed58f8280826633a38d7448cabdd723e116165bcf", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "0ca44e7dcb8d933e010740324470dd8ec947243b51304bd34b8165ef3281edc2"},
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"phoenix": {:hex, :phoenix, "1.6.15", "0a1d96bbc10747fd83525370d691953cdb6f3ccbac61aa01b4acb012474b047d", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0 or ~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d70ab9fbf6b394755ea88b644d34d79d8b146e490973151f248cacd122d20672"},
"phoenix_html": {:hex, :phoenix_html, "3.3.0", "bf451c71ebdaac8d2f40d3b703435e819ccfbb9ff243140ca3bd10c155f134cc", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "272c5c1533499f0132309936c619186480bafcc2246588f99a69ce85095556ef"},
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.4.1", "2aff698f5e47369decde4357ba91fc9c37c6487a512b41732818f2204a8ef1d3", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "9bffb834e7ddf08467fe54ae58b5785507aaba6255568ae22b4d46e2bb3615ab"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.1", "ba04e489ef03763bf28a17eb2eaddc2c20c6d217e2150a61e3298b0f4c2012b5", [:mix], [], "hexpm", "81367c6d1eea5878ad726be80808eb5a787a23dee699f96e72b1109c57cdd8d9"},
"phoenix_template": {:hex, :phoenix_template, "1.0.1", "85f79e3ad1b0180abb43f9725973e3b8c2c3354a87245f91431eec60553ed3ef", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "157dc078f6226334c91cb32c1865bf3911686f8bcd6bcff86736f6253e6993ee"},
"phoenix_view": {:hex, :phoenix_view, "2.0.1", "a653e3d9d944aace0a064e4a13ad473ffa68f7bc4ca42dbf83cc1d464f1fb295", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}], "hexpm", "6c358e2cefc5f341c728914b867c556bbfd239fed9e881bac257d70cb2b8a6f6"},
"plug": {:hex, :plug, "1.14.0", "ba4f558468f69cbd9f6b356d25443d0b796fbdc887e03fa89001384a9cac638f", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "bf020432c7d4feb7b3af16a0c2701455cbbbb95e5b6866132cb09eb0c29adc14"},
"plug_crypto": {:hex, :plug_crypto, "1.2.3", "8f77d13aeb32bfd9e654cb68f0af517b371fb34c56c9f2b58fe3df1235c1251a", [:mix], [], "hexpm", "b5672099c6ad5c202c45f5a403f21a3411247f164e4a8fab056e5cd8a290f4a2"},
"plug": {:hex, :plug, "1.15.1", "b7efd81c1a1286f13efb3f769de343236bd8b7d23b4a9f40d3002fc39ad8f74c", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "459497bd94d041d98d948054ec6c0b76feacd28eec38b219ca04c0de13c79d30"},
"plug_crypto": {:hex, :plug_crypto, "1.2.5", "918772575e48e81e455818229bf719d4ab4181fcbf7f85b68a35620f78d89ced", [:mix], [], "hexpm", "26549a1d6345e2172eb1c233866756ae44a9609bd33ee6f99147ab3fd87fd842"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
}

0 comments on commit 3341126

Please sign in to comment.