Skip to content

Commit

Permalink
Remove whitespace on node without attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ypconstante committed May 22, 2024
1 parent b04be56 commit 1f6e118
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
9 changes: 4 additions & 5 deletions lib/floki/raw_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ defmodule Floki.RawHTML do
defp build_raw_html([{:pi, tag, attrs} | tail], html, encoder, padding, self_closing_tags) do
build_raw_html(
tail,
[html, leftpad(padding), "<?", tag, " ", tag_attrs(attrs, encoder), "?>"],
[html, leftpad(padding), "<?", tag, tag_attrs(attrs, encoder), "?>"],
encoder,
padding,
self_closing_tags
Expand Down Expand Up @@ -129,7 +129,7 @@ defmodule Floki.RawHTML do
end

defp tag_attrs(attr_list, encoder) do
Enum.map_intersperse(attr_list, ?\s, &build_attrs(&1, encoder))
Enum.map(attr_list, &build_attrs(&1, encoder))
end

defp tag_with_attrs(type, [], children, padding, _encoder, self_closing_tags),
Expand All @@ -140,7 +140,6 @@ defmodule Floki.RawHTML do
leftpad(padding),
"<",
type,
?\s,
tag_attrs(attrs, encoder) | close_open_tag(type, children, self_closing_tags)
]

Expand All @@ -167,10 +166,10 @@ defmodule Floki.RawHTML do
end

defp build_attrs({attr, value}, encoder) do
[attr, "=\"", encoder.(value) | "\""]
[?\s, attr, "=\"", encoder.(value) | "\""]
end

defp build_attrs(attr, _encoder), do: attr
defp build_attrs(attr, _encoder), do: [?\s, attr]

defp tag_for(type, attrs, children, encoder, padding, self_closing_tags) do
encoder =
Expand Down
4 changes: 2 additions & 2 deletions test/floki_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,10 @@ defmodule FlokiTest do
# no guarantee of attribute order from a map
recombined =
case Floki.raw_html(parsed) do
"<div id=\"content\"><p ><a class=\"bar\" href=\"site\"><span >lol</span><img src=\"foo.png\"/></a></p><br /></div>" ->
"<div id=\"content\"><p><a class=\"bar\" href=\"site\"><span>lol</span><img src=\"foo.png\"/></a></p><br/></div>" ->
true

"<div id=\"content\"><p ><a href=\"site\" class=\"bar\"><span >lol</span><img src=\"foo.png\"/></a></p><br /></div>" ->
"<div id=\"content\"><p><a href=\"site\" class=\"bar\"><span>lol</span><img src=\"foo.png\"/></a></p><br/></div>" ->
true

_other ->
Expand Down

0 comments on commit 1f6e118

Please sign in to comment.