Skip to content

Commit

Permalink
Change params of fun/2 recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
vittoriabitton committed Mar 12, 2024
1 parent 3a0eb98 commit 5109def
Showing 1 changed file with 30 additions and 36 deletions.
66 changes: 30 additions & 36 deletions lib/floki/html_tree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -339,56 +339,50 @@ defmodule Floki.HTMLTree do

container_doc(
open,
html_tree,
{html_tree, html_tree.root_nodes_ids},
close,
opts,
&fun/2,
container_opts
)
end

defp fun({:comment, content}, opts) do
color("<!-- #{content} -->", :comment, opts)
end

defp fun(string, opts) when is_binary(string) do
color(string, :string, opts)
end

defp fun(html_tree, opts) do
defp fun({html_tree, nodes_ids}, opts) do
tag_color = :map
attribute_color = :map

html_tree.root_nodes_ids
nodes_ids
|> Enum.reverse()
|> Enum.map(fn node_id ->
root = Map.get(html_tree.nodes, node_id)

case HTMLTree.to_tuple(html_tree, root) do
{type, attributes, children} ->
built_attributes =
for {name, value} <- attributes do
concat([
color(" #{name}=", attribute_color, opts),
color("\"#{value}\"", :string, opts)
])
end
|> concat()

open =
concat([
color("<#{type}", tag_color, opts),
built_attributes,
color(">", tag_color, opts)
])

close = color("</#{type}>", tag_color, opts)
container_opts = [separator: "", break: :strict]
container_doc(open, children, close, opts, &fun/2, container_opts)

result ->
result
end
built_attributes =
for {name, value} <- root.attributes do
concat([
color(" #{name}=", attribute_color, opts),
color("\"#{value}\"", :string, opts)
])
end
|> concat()

open =
concat([
color("<#{root.type}", tag_color, opts),
built_attributes,
color(">", tag_color, opts)
])

close = color("</#{root.type}>", tag_color, opts)
container_opts = [separator: "", break: :strict]

container_doc(
open,
{html_tree, html_tree.children_nodes_ids},
close,
opts,
&fun/2,
container_opts
)
end)
end
end
Expand Down

0 comments on commit 5109def

Please sign in to comment.