Skip to content

Commit

Permalink
Track braces state in tokenizer, closes #3613
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 2, 2025
1 parent 751fe00 commit 603c8ec
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/phoenix_live_view/html_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ defmodule Phoenix.LiveView.HTMLFormatter do

defp tokenize(source) do
{:ok, eex_nodes} = EEx.tokenize(source)
{tokens, cont} = Enum.reduce(eex_nodes, {[], :text}, &do_tokenize(&1, &2, source))
{tokens, cont} = Enum.reduce(eex_nodes, {[], {:text, :enabled}}, &do_tokenize(&1, &2, source))
Tokenizer.finalize(tokens, "nofile", cont, source)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/phoenix_live_view/tag_engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ defmodule Phoenix.LiveView.TagEngine do
tag_handler = Keyword.fetch!(opts, :tag_handler)

%{
cont: :text,
cont: {:text, :enabled},
tokens: [],
subengine: subengine,
substate: subengine.init(opts),
Expand Down
6 changes: 3 additions & 3 deletions lib/phoenix_live_view/tokenizer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ defmodule Phoenix.LiveView.Tokenizer do
{:close, :tag, "section", %{column: 16, line: 1}},
{:tag, "div", [], %{column: 10, line: 1, closing: :self}},
{:tag, "section", [], %{column: 1, line: 1}}
], :text}
], {:text, :enabled}}
"""
def tokenize(text, meta, tokens, cont, state) do
line = Keyword.get(meta, :line, 1)
column = Keyword.get(meta, :column, 1)

case cont do
:text -> handle_text(text, line, column, [], tokens, state)
{:text, braces} -> handle_text(text, line, column, [], tokens, %{state | braces: braces})
:style -> handle_style(text, line, column, [], tokens, state)
:script -> handle_script(text, line, column, [], tokens, state)
{:comment, _, _} -> handle_comment(text, line, column, [], tokens, state)
Expand Down Expand Up @@ -175,7 +175,7 @@ defmodule Phoenix.LiveView.Tokenizer do
end

defp handle_text(<<>>, line, column, buffer, acc, state) do
ok(text_to_acc(buffer, acc, line, column, state.context), :text)
ok(text_to_acc(buffer, acc, line, column, state.context), {:text, state.braces})
end

## handle_doctype
Expand Down
10 changes: 10 additions & 0 deletions test/phoenix_live_view/html_engine_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ defmodule Phoenix.LiveView.HTMLEngineTest do
""") == "Hello <div>world!</div>"
end

test "handles phx-no-curly-interpolation" do
assert render("""
<div phx-no-curly-interpolation>{open}<%= :eval %>{close}</div>
""") == "<div>{open}eval{close}</div>"

assert render("""
<div phx-no-curly-interpolation>{open}{<%= :eval %>}{close}</div>
""") == "<div>{open}{eval}{close}</div>"
end

test "handles string attributes" do
assert render("""
Hello <div name="my name" phone="111">text</div>
Expand Down
10 changes: 5 additions & 5 deletions test/phoenix_live_view/tokenizer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Phoenix.LiveView.TokenizerTest do
defp tokenizer_state(text), do: Tokenizer.init(0, "nofile", text, Phoenix.LiveView.HTMLEngine)

defp tokenize(text) do
Tokenizer.tokenize(text, [], [], :text, tokenizer_state(text))
Tokenizer.tokenize(text, [], [], {:text, :enabled}, tokenizer_state(text))
|> elem(0)
|> Enum.reverse()
end
Expand Down Expand Up @@ -98,7 +98,7 @@ defmodule Phoenix.LiveView.TokenizerTest do
"""

{first_tokens, cont} =
Tokenizer.tokenize(first_part, [], [], :text, tokenizer_state(first_part))
Tokenizer.tokenize(first_part, [], [], {:text, :enabled}, tokenizer_state(first_part))

second_part = """
</div>
Expand All @@ -109,7 +109,7 @@ defmodule Phoenix.LiveView.TokenizerTest do
</div>
"""

{tokens, :text} =
{tokens, {:text, :enabled}} =
Tokenizer.tokenize(second_part, [], first_tokens, cont, tokenizer_state(second_part))

assert Enum.reverse(tokens) == [
Expand Down Expand Up @@ -140,7 +140,7 @@ defmodule Phoenix.LiveView.TokenizerTest do
"""

{first_tokens, cont} =
Tokenizer.tokenize(first_part, [], [], :text, tokenizer_state(first_part))
Tokenizer.tokenize(first_part, [], [], {:text, :enabled}, tokenizer_state(first_part))

second_part = """
-->
Expand All @@ -158,7 +158,7 @@ defmodule Phoenix.LiveView.TokenizerTest do
</p>
"""

{tokens, :text} =
{tokens, {:text, :enabled}} =
Tokenizer.tokenize(third_part, [], second_tokens, cont, tokenizer_state(third_part))

assert Enum.reverse(tokens) == [
Expand Down

0 comments on commit 603c8ec

Please sign in to comment.