From 7f193ea0bbe2b2c3f410a97e8df4e40436070468 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Fri, 25 Aug 2023 09:56:19 -0400 Subject: [PATCH] WIP --- lib/phoenix_live_view/engine.ex | 22 +++++++++++++++------- lib/phoenix_live_view/html_engine.ex | 9 +++++---- lib/phoenix_live_view/tag_engine.ex | 14 ++++++++++---- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/phoenix_live_view/engine.ex b/lib/phoenix_live_view/engine.ex index 91f8b7fd5b..611a2541c4 100644 --- a/lib/phoenix_live_view/engine.ex +++ b/lib/phoenix_live_view/engine.ex @@ -391,13 +391,21 @@ defmodule Phoenix.LiveView.Engine do analyze_static_and_dynamic(static, dynamic, vars, assigns, caller) static = - if anno = opts[:root_tag_annotation] do - case static do - [] -> [anno] - [first | rest] -> [anno <> first | rest] - end - else - static + case Keyword.fetch(opts, :root_tag_annotation) do + {:ok, {nil = _before, nil = _aft}} -> + static + + {:ok, {before, aft}} -> + case static do + [] -> + ["#{before}#{aft}"] + + [first | rest] -> + List.update_at([to_string(before) <> first | rest], -1, &(&1 <> to_string(aft))) + end + + :error -> + static end changed = diff --git a/lib/phoenix_live_view/html_engine.ex b/lib/phoenix_live_view/html_engine.ex index a67167eb80..d872777451 100644 --- a/lib/phoenix_live_view/html_engine.ex +++ b/lib/phoenix_live_view/html_engine.ex @@ -59,11 +59,12 @@ defmodule Phoenix.LiveView.HTMLEngine do if Application.get_env(:phoenix, :heex_debug_annotations, false) do %Macro.Env{module: mod, function: {func, _}, file: file, line: line} = caller line = if line == 0, do: 1, else: line - file = Path.relative_to_cwd(file) - begin = "<#{inspect(mod)}.#{func}> #{file}:#{line}" - "" + # file = Path.relative_to_cwd(file) + before = "<#{inspect(mod)}.#{func}> #{file}:#{line}" + aft = "" + {"", ""} else - nil + {nil, nil} end end end diff --git a/lib/phoenix_live_view/tag_engine.ex b/lib/phoenix_live_view/tag_engine.ex index 3ac4603fa1..f473ef17df 100644 --- a/lib/phoenix_live_view/tag_engine.ex +++ b/lib/phoenix_live_view/tag_engine.ex @@ -40,7 +40,8 @@ defmodule Phoenix.LiveView.TagEngine do """ @callback void?(name :: binary()) :: boolean() - @callback annotate_root_tag(Macro.Env.t()) :: binary() | nil + @callback annotate_root_tag(Macro.Env.t()) :: + {anno_before :: binary() | nil, anno_after :: binary() | nil} @doc """ Renders a component defined by the given function. @@ -185,10 +186,15 @@ defmodule Phoenix.LiveView.TagEngine do |> handle_tokens(tokens) |> validate_unclosed_tags!("template") - annotation = - state.has_tags? && state.caller && state.tag_handler.annotate_root_tag(state.caller) + opts = [root: token_state.root || false] + + opts = + if state.has_tags? && state.caller do + Keyword.put(opts, :root_tag_annotation, state.tag_handler.annotate_root_tag(state.caller)) + else + opts + end - opts = [root: token_state.root || false, root_tag_annotation: annotation] ast = invoke_subengine(token_state, :handle_body, [opts]) quote do