From 0ede9191e6cf2b10e4d9f825e5bd62aaee553f8c Mon Sep 17 00:00:00 2001 From: Yuri Pereira Constante Date: Thu, 11 Jan 2024 00:22:20 -0300 Subject: [PATCH 1/3] Optimize leftpad on raw_html --- lib/floki/raw_html.ex | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/floki/raw_html.ex b/lib/floki/raw_html.ex index beaba76a..ea449e98 100644 --- a/lib/floki/raw_html.ex +++ b/lib/floki/raw_html.ex @@ -43,7 +43,7 @@ defmodule Floki.RawHTML do padding = case Keyword.fetch(options, :pretty) do - {:ok, true} -> %{pad: " ", line_ending: "\n", depth: 0} + {:ok, true} -> %{pad: "", pad_increase: " ", line_ending: "\n", depth: 0} _ -> :noop end @@ -211,7 +211,7 @@ defmodule Floki.RawHTML do do: map_intersperse(Map.to_list(attrs), separator, mapper) defp leftpad(:noop), do: "" - defp leftpad(%{pad: pad, depth: depth}), do: String.duplicate(pad, depth) + defp leftpad(%{pad: pad}), do: pad defp leftpad_content(:noop, string), do: string @@ -226,7 +226,11 @@ defmodule Floki.RawHTML do end defp pad_increase(:noop), do: :noop - defp pad_increase(padder = %{depth: depth}), do: %{padder | depth: depth + 1} + + defp pad_increase(padder = %{depth: depth, pad_increase: pad_increase}) do + depth = depth + 1 + %{padder | depth: depth, pad: String.duplicate(pad_increase, depth)} + end defp line_ending(:noop), do: "" defp line_ending(%{line_ending: line_ending}), do: line_ending From a9b3de2c88e28794edf905c33716eb1171252221 Mon Sep 17 00:00:00 2001 From: Yuri Pereira Constante Date: Thu, 11 Jan 2024 00:53:38 -0300 Subject: [PATCH 2/3] Avoid pad_increase when children is empty --- lib/floki/raw_html.ex | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/floki/raw_html.ex b/lib/floki/raw_html.ex index ea449e98..01c83e51 100644 --- a/lib/floki/raw_html.ex +++ b/lib/floki/raw_html.ex @@ -178,10 +178,15 @@ defmodule Floki.RawHTML do _ -> encoder end + children_content = case children do + [] -> "" + _ -> build_raw_html(children, "", encoder, pad_increase(padding), self_closing_tags) + end + [ tag_with_attrs(type, attrs, children, padding, encoder, self_closing_tags), line_ending(padding), - build_raw_html(children, "", encoder, pad_increase(padding), self_closing_tags), + children_content, close_end_tag(type, children, padding, self_closing_tags) ] end From 039f88e2a9ced223e8ad4bbff910de6182df03be Mon Sep 17 00:00:00 2001 From: Yuri Pereira Constante Date: Thu, 11 Jan 2024 01:02:12 -0300 Subject: [PATCH 3/3] Format --- lib/floki/raw_html.ex | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/floki/raw_html.ex b/lib/floki/raw_html.ex index 01c83e51..67993013 100644 --- a/lib/floki/raw_html.ex +++ b/lib/floki/raw_html.ex @@ -178,10 +178,11 @@ defmodule Floki.RawHTML do _ -> encoder end - children_content = case children do - [] -> "" - _ -> build_raw_html(children, "", encoder, pad_increase(padding), self_closing_tags) - end + children_content = + case children do + [] -> "" + _ -> build_raw_html(children, "", encoder, pad_increase(padding), self_closing_tags) + end [ tag_with_attrs(type, attrs, children, padding, encoder, self_closing_tags),