Skip to content

Commit 1d473db

Browse files
committed
Add Phoenix.LiveView.TagCompiler namespace
This commit moves the Tokenizer into the Phoenix.LiveView.TagCompiler.Tokenizer module and also adds a Parser that builds a tree similar to what the HTMLFormatter previously did. We're going to use that node tree for compiling templates in a future commit. The HTMLFormatter and HTMLAlgebra were now use this tree format.
1 parent 7b28211 commit 1d473db

12 files changed

Lines changed: 640 additions & 308 deletions

File tree

lib/phoenix_live_view/html_algebra.ex

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
173173
end
174174
end
175175

176-
defp tag_block?({:tag_block, _, _, _, _}), do: true
176+
defp tag_block?({:block, _, _, _, _, _}), do: true
177177
defp tag_block?(_node), do: false
178178

179-
defp tag?({:tag_block, _, _, _, _}), do: true
180-
defp tag?({:tag_self_close, _, _}), do: true
179+
defp tag?({:block, _, _, _, _, _}), do: true
180+
defp tag?({:self_close, _, _, _, _}), do: true
181181
defp tag?(_node), do: false
182182

183183
defp text?({:text, _, _}), do: true
@@ -195,7 +195,7 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
195195

196196
defp text_ends_with_space?(_node), do: false
197197

198-
defp block_preserve?({:tag_block, _, _, _, %{mode: :preserve}}), do: true
198+
defp block_preserve?({:block, _, _, _, _, %{mode: :preserve}}), do: true
199199
defp block_preserve?({:body_expr, _, _}), do: true
200200
defp block_preserve?({:eex, _, _}), do: true
201201
defp block_preserve?(_node), do: false
@@ -208,7 +208,8 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
208208
{:block, group(nest(children, :reset))}
209209
end
210210

211-
defp to_algebra({:tag_block, name, attrs, block, meta}, context) when name in @languages do
211+
defp to_algebra({:block, _type, _name, attrs, block, %{tag_name: name} = meta}, context)
212+
when name in @languages do
212213
children = block_to_algebra(block, %{context | mode: :preserve})
213214

214215
# Convert the whole block to text as there are no
@@ -253,7 +254,10 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
253254
{:block, group}
254255
end
255256

256-
defp to_algebra({:tag_block, name, attrs, block, meta}, %{mode: :preserve} = context) do
257+
defp to_algebra(
258+
{:block, _type, _name, attrs, block, %{tag_name: name} = meta},
259+
%{mode: :preserve} = context
260+
) do
257261
children = block_to_algebra(block, context)
258262

259263
children =
@@ -270,11 +274,11 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
270274
{:inline, tag}
271275
end
272276

273-
defp to_algebra({:tag_block, _name, _attrs, _block, %{mode: :preserve}} = doc, context) do
277+
defp to_algebra({:block, _type, _name, _attrs, _block, %{mode: :preserve}} = doc, context) do
274278
to_algebra(doc, %{context | mode: :preserve})
275279
end
276280

277-
defp to_algebra({:tag_block, name, attrs, block, _meta}, context) do
281+
defp to_algebra({:block, _type, _name, attrs, block, %{tag_name: name}}, context) do
278282
inline? = inline?(name, context)
279283
{block, force_newline?} = trim_block_newlines(block, inline?)
280284
inline? = inline? and not force_newline?
@@ -304,7 +308,7 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
304308
end
305309
end
306310

307-
defp to_algebra({:tag_self_close, name, attrs}, context) do
311+
defp to_algebra({:self_close, _type, _name, attrs, %{tag_name: name}}, context) do
308312
doc =
309313
concat([
310314
"<#{name}",
@@ -318,7 +322,7 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
318322
# Handle EEX blocks within preserve tags
319323
defp to_algebra({:eex_block, expr, block, meta}, %{mode: :preserve} = context) do
320324
doc =
321-
Enum.reduce(block, empty(), fn {block, expr}, doc ->
325+
Enum.reduce(block, empty(), fn {block, expr, _clause_meta}, doc ->
322326
children = block_to_algebra(block, context)
323327
expr = "<% #{expr} %>"
324328
concat([doc, children, expr])
@@ -330,7 +334,7 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
330334
# Handle EEX blocks
331335
defp to_algebra({:eex_block, expr, block, meta}, context) do
332336
{doc, _stab} =
333-
Enum.reduce(block, {empty(), false}, fn {block, expr}, {doc, stab?} ->
337+
Enum.reduce(block, {empty(), false}, fn {block, expr, _clause_meta}, {doc, stab?} ->
334338
{block, _force_newline?} = trim_block_newlines(block, false)
335339
{next_doc, stab?} = eex_block_to_algebra(expr, block, stab?, context)
336340
{concat(doc, force_unfit(next_doc)), stab?}
@@ -529,8 +533,8 @@ defmodule Phoenix.LiveView.HTMLAlgebra do
529533

530534
# Handle EEx clauses
531535
#
532-
# {[], "something ->"}
533-
# {[{:tag_block, "p", [], [text: "do something"]}], "else"}
536+
# {[], "something ->", %{...}}
537+
# {[{:block, :tag, "p", [], [...], %{...}}], "else", %{...}}
534538
defp eex_block_to_algebra(expr, block, stab?, context) when is_list(block) do
535539
indent = if stab?, do: 4, else: 2
536540

0 commit comments

Comments
 (0)