Skip to content

Commit

Permalink
Format EEx blocks within script tag, closes #3312
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim authored and SteffenDE committed Jun 21, 2024
1 parent 8f39fd6 commit f6f8f9c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/phoenix_live_view/html_algebra.ex
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
end

# Handle EEX blocks within preserve tags
defp to_algebra({:eex_block, expr, block}, %{mode: :preserve} = context) do
defp to_algebra({:eex_block, expr, block, meta}, %{mode: :preserve} = context) do
doc =
Enum.reduce(block, empty(), fn {block, expr}, doc ->
children = block_to_algebra(block, context)
expr = "<% #{expr} %>"
concat([doc, children, expr])
end)

{:block, group(concat("<%= #{expr} %>", doc))}
{:block, group(concat("<%#{meta.opt} #{expr} %>", doc))}
end

# Handle EEX blocks
Expand Down
20 changes: 20 additions & 0 deletions test/phoenix_live_view/html_formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,26 @@ defmodule Phoenix.LiveView.HTMLFormatterTest do
""")
end

test "formats eex blocks within script tag" do
assert_formatter_doesnt_change("""
<script>
var foo = 1;
<%= if @bar do %>
var bar = 2;
<% end %>
</script>
""")

assert_formatter_doesnt_change("""
<script>
var foo = 1;
<% if @bar do %>
var bar = 2;
<% end %>
</script>
""")
end

test "formats style tag" do
input = """
<div>
Expand Down

0 comments on commit f6f8f9c

Please sign in to comment.