Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user configurable self closing tags #417

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions lib/floki/raw_html.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Floki.RawHTML do
@moduledoc false

@self_closing_tags [
@default_self_closing_tags [
"area",
"base",
"br",
Expand All @@ -13,13 +13,24 @@ defmodule Floki.RawHTML do
"input",
"keygen",
"link",
"menuitem",
"meta",
"param",
"source",
"track",
"wbr"
]

def default_self_closing_tags(), do: @default_self_closing_tags

def self_closing_tags do
custom_self_closing_tags = Application.get_env(:floki, :self_closing_tags)

if is_list(custom_self_closing_tags),
do: custom_self_closing_tags,
else: @default_self_closing_tags
end

@encoder &HtmlEntities.encode/1

def raw_html(html_tree, options) do
Expand Down Expand Up @@ -99,13 +110,19 @@ defmodule Floki.RawHTML do
defp tag_with_attrs(type, attrs, children, padding),
do: [leftpad(padding), "<", type, ?\s, tag_attrs(attrs) | close_open_tag(type, children)]

defp close_open_tag(type, []) when type in @self_closing_tags, do: "/>"
defp close_open_tag(_type, _), do: ">"

defp close_end_tag(type, [], _padding) when type in @self_closing_tags, do: ""
defp close_open_tag(type, children) do
case {type in self_closing_tags(), children} do
{true, []} -> "/>"
_ -> ">"
end
end

defp close_end_tag(type, _, padding),
do: [leftpad(padding), "</", type, ">", line_ending(padding)]
defp close_end_tag(type, children, padding) do
case {type in self_closing_tags(), children} do
{true, []} -> ""
philss marked this conversation as resolved.
Show resolved Hide resolved
_ -> [leftpad(padding), "</", type, ">", line_ending(padding)]
end
end

defp build_attrs({attr, value}), do: [attr, "=\"", html_escape(value) | "\""]
defp build_attrs(attr), do: attr
Expand Down
59 changes: 59 additions & 0 deletions test/floki_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,65 @@ defmodule FlokiTest do
assert raw_html == "<link>www.example.com</link>"
end

test "raw_html (with custom self closing tag without content and without attributes)" do
original_self_closing_tags = Application.get_env(:floki, :self_closing_tags)
Application.put_env(:floki, :self_closing_tags, ["shy"])

raw_html = Floki.raw_html({"shy", [], []})

assert raw_html == "<shy/>"

Application.put_env(:floki, :self_closing_tags, original_self_closing_tags)
end

test "raw_html (with custom self closing tag without content)" do
original_self_closing_tags = Application.get_env(:floki, :self_closing_tags)
Application.put_env(:floki, :self_closing_tags, ["download"])

raw_html = Floki.raw_html({"download", [{"href", "//www.example.com/file.zip"}], []})

assert raw_html == "<download href=\"//www.example.com/file.zip\"/>"

Application.put_env(:floki, :self_closing_tags, original_self_closing_tags)
end

test "raw_html (with custom self closing tag with content and with attribute)" do
original_self_closing_tags = Application.get_env(:floki, :self_closing_tags)
Application.put_env(:floki, :self_closing_tags, ["download"])

raw_html =
Floki.raw_html(
{"download", [{"href", "//www.example.com/file.zip"}], ["Download file.zip"]}
)

assert raw_html ==
"<download href=\"//www.example.com/file.zip\">Download file.zip</download>"

Application.put_env(:floki, :self_closing_tags, original_self_closing_tags)
end

test "raw_html (with custom self closing tag with content and without attribute)" do
original_self_closing_tags = Application.get_env(:floki, :self_closing_tags)
Application.put_env(:floki, :self_closing_tags, ["strike"])

raw_html = Floki.raw_html({"strike", [], ["stroke text"]})

assert raw_html == "<strike>stroke text</strike>"

Application.put_env(:floki, :self_closing_tags, original_self_closing_tags)
end

test "raw_html (with default self closing tag that isn't set while custom self closing tags are set must fail)" do
original_self_closing_tags = Application.get_env(:floki, :self_closing_tags)
Application.put_env(:floki, :self_closing_tags, ["page"])

raw_html = Floki.raw_html({"br", [], []})

assert raw_html != "<br/>"

Application.put_env(:floki, :self_closing_tags, original_self_closing_tags)
end

test "raw_html (with script and style tags)" do
tree = {
"body",
Expand Down