Skip to content

Commit 25eadc5

Browse files
committed
backport #4145
1 parent 49e5f94 commit 25eadc5

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

lib/phoenix_live_view/tag_engine.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,11 @@ defmodule Phoenix.LiveView.TagEngine do
10981098
defp handle_tag_attrs(state, meta, attrs) do
10991099
Enum.reduce(attrs, state, fn
11001100
{:root, {:expr, _, _} = expr, _attr_meta}, state ->
1101-
ast = parse_expr!(expr, state.file)
1101+
ast =
1102+
case parse_expr!(expr, state.file) do
1103+
{:@, _, _} = ast -> ast
1104+
ast -> expand_with_line(ast, meta[:line], state.caller)
1105+
end
11021106

11031107
# If we have a map of literal keys, we unpack it as a list
11041108
# to simplify the downstream check.

test/phoenix_live_view/html_engine_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ defmodule Phoenix.LiveView.HTMLEngineTest do
4444
end
4545
end
4646

47+
defmacro test_attr_macro(a) do
48+
case a do
49+
:base -> quote do: [{"style", "display: flex;"}, {"other", "foo"}, {"another", @bar}]
50+
_ -> quote do: a
51+
end
52+
end
53+
4754
def assigns_component(assigns) do
4855
~H"{inspect(Map.delete(assigns, :__changed__))}"
4956
end
@@ -216,6 +223,15 @@ defmodule Phoenix.LiveView.HTMLEngineTest do
216223

217224
assert %Phoenix.LiveView.Rendered{static: ["<div", " d2=\"2\"", "></div>"]} =
218225
eval(template, assigns)
226+
227+
# macro is expanded
228+
template = ~S|<div {test_attr_macro(:base)} />|
229+
230+
assert %Phoenix.LiveView.Rendered{static: ["<div style=\"", "\" other=\"foo\"", "></div>"]} =
231+
eval(template, %{bar: "baz"})
232+
233+
assert render(template, %{bar: "baz"}) ==
234+
~S|<div style="display: flex;" other="foo" another="baz"></div>|
219235
end
220236

221237
test "optimizes attributes with literal string values" do

0 commit comments

Comments
 (0)