99cbb42962
This basically just adds svg elements as void and nonvoid element aliases and it works, will test on a real proejct before releasing the next release. Also, fixed the weird behaviour problem by defining types for each of the ast nodes and then referencing those types when defining the ast type. Unclear why this works, but I imagine it has to do with the types not being a big part of the compilation process or something. This also uses the typed_struct library to do so. Seems pretty slick and does what it claims it does.
53 lines
1.1 KiB
Elixir
53 lines
1.1 KiB
Elixir
defmodule Temple.Parser.NonvoidElementsAliases do
|
|
@moduledoc false
|
|
@behaviour Temple.Parser
|
|
|
|
use TypedStruct
|
|
|
|
typedstruct do
|
|
field :name, atom()
|
|
field :attrs, list()
|
|
field :children, list()
|
|
field :meta, map()
|
|
end
|
|
|
|
alias Temple.Parser
|
|
|
|
@impl true
|
|
def applicable?({name, _, _}) do
|
|
name in Parser.nonvoid_elements_aliases()
|
|
end
|
|
|
|
def applicable?(_), do: false
|
|
|
|
@impl true
|
|
def run({name, meta, args}) do
|
|
name = Parser.nonvoid_elements_lookup()[name]
|
|
|
|
{do_and_else, args} = Temple.Parser.Utils.split_args(args)
|
|
|
|
{do_and_else, args} = Temple.Parser.Utils.consolidate_blocks(do_and_else, args)
|
|
|
|
children = Temple.Parser.parse(do_and_else[:do])
|
|
|
|
Temple.Ast.new(__MODULE__,
|
|
name: to_string(name) |> String.replace_suffix("!", ""),
|
|
attrs: args,
|
|
meta: %{whitespace: whitespace(meta)},
|
|
children:
|
|
Temple.Ast.new(Temple.Parser.ElementList,
|
|
children: children,
|
|
whitespace: whitespace(meta)
|
|
)
|
|
)
|
|
end
|
|
|
|
defp whitespace(meta) do
|
|
if Keyword.has_key?(meta, :end) do
|
|
:loose
|
|
else
|
|
:tight
|
|
end
|
|
end
|
|
end
|