Packages
phoenix_live_view
1.2.3
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.2.0-rc.3
1.2.0-rc.2
1.2.0-rc.1
1.2.0-rc.0
1.1.32
1.1.31
1.1.30
1.1.29
1.1.28
1.1.27
1.1.26
1.1.25
1.1.24
1.1.23
1.1.22
1.1.21
1.1.20
1.1.19
1.1.18
1.1.17
1.1.16
1.1.15
1.1.14
1.1.13
1.1.12
1.1.11
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
retired
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.1.0-rc.4
1.1.0-rc.3
1.1.0-rc.2
1.1.0-rc.1
1.1.0-rc.0
1.0.18
1.0.17
1.0.16
1.0.15
1.0.14
1.0.13
1.0.12
1.0.11
1.0.10
1.0.9
1.0.8
retired
1.0.7
1.0.6
retired
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.9
1.0.0-rc.8
1.0.0-rc.7
1.0.0-rc.6
1.0.0-rc.5
1.0.0-rc.4
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.20.17
0.20.16
0.20.15
0.20.14
0.20.13
0.20.12
0.20.11
0.20.10
0.20.9
0.20.8
0.20.7
0.20.6
0.20.5
0.20.4
0.20.3
0.20.2
0.20.1
0.20.0
0.19.5
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.18
0.18.17
0.18.16
0.18.15
0.18.14
0.18.13
0.18.12
0.18.11
0.18.10
0.18.9
0.18.8
0.18.7
0.18.6
0.18.5
0.18.4
0.18.3
0.18.2
0.18.1
0.18.0
0.17.14
0.17.13
0.17.12
0.17.11
0.17.10
0.17.9
0.17.8
0.17.7
0.17.6
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.7
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.8
0.14.7
0.14.6
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.1
0.11.0
0.10.0
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.6.0-dev
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.1
0.1.0
Rich, real-time user experiences with server-rendered HTML
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/phoenix_live_view/route.ex
defmodule Phoenix.LiveView.Route do
@moduledoc false
alias Phoenix.LiveView.{Route, Socket}
defstruct path: nil,
view: nil,
action: nil,
opts: [],
live_session: %{},
params: %{},
uri: nil
@doc """
Computes the container from the route options and falls back to view options.
"""
def container(%Route{} = route) do
route.opts[:container] || route.view.__live__()[:container]
end
@doc """
Returns the internal or external matched LiveView route info for the given socket
and uri, raises if none is available.
"""
def live_link_info!(%Socket{router: nil}, view, _uri) do
raise ArgumentError,
"cannot invoke handle_params/3 on #{inspect(view)} " <>
"because it is not mounted nor accessed through the router live/3 macro"
end
def live_link_info!(%Socket{} = socket, view, uri) do
%{private: %{live_session_name: session_name}} = socket
case live_link_info_without_checks(socket.endpoint, socket.router, uri) do
{:internal, %Route{view: ^view, live_session: %{name: ^session_name}} = route} ->
{:internal, route}
{:internal, %Route{} = route} ->
{:external, route.uri}
{:external, _parsed_uri} = external ->
external
:error ->
raise ArgumentError,
"cannot invoke handle_params nor navigate/patch to #{inspect(uri)} " <>
"because it isn't defined in #{inspect(socket.router)}"
end
end
@doc """
Returns the internal or external matched LiveView route info for the given uri.
"""
def live_link_info_without_checks(endpoint, router, uri) when is_binary(uri) do
live_link_info_without_checks(endpoint, router, URI.parse(uri))
end
def live_link_info_without_checks(endpoint, router, %URI{} = parsed_uri)
when is_atom(endpoint) and is_atom(router) do
%URI{host: host, path: path, query: query} = parsed_uri
query_params = if query, do: Plug.Conn.Query.decode(query), else: %{}
split_path =
for segment <- String.split(path || "", "/"), segment != "", do: URI.decode(segment)
route_path = strip_segments(endpoint.script_name(), split_path) || split_path
case Phoenix.Router.route_info(router, "GET", route_path, host) do
%{plug: Phoenix.LiveView.Plug, phoenix_live_view: lv, path_params: path_params} ->
{view, action, opts, live_session} = lv
route = %Route{
view: view,
path: route_path,
action: action,
uri: parsed_uri,
opts: opts,
live_session: live_session,
params: Map.merge(query_params, path_params)
}
{:internal, route}
%{} ->
{:external, parsed_uri}
:error ->
:error
end
end
defp strip_segments([head | tail1], [head | tail2]), do: strip_segments(tail1, tail2)
defp strip_segments([], tail2), do: tail2
defp strip_segments(_, _), do: nil
end