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

Allow attribute values to not be escaped #422

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 23 additions & 10 deletions lib/floki/raw_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule Floki.RawHTML do
encoder =
case Keyword.fetch(options, :encode) do
{:ok, true} -> @encoder
{:ok, false} -> & &1
{:ok, false} -> &Function.identity/1
:error -> default_encoder()
end

Expand Down Expand Up @@ -69,7 +69,7 @@ defmodule Floki.RawHTML do
defp build_raw_html([{:pi, tag, attrs} | tail], html, encoder, padding) do
build_raw_html(
tail,
[html, leftpad(padding), "<?", tag, " ", tag_attrs(attrs), "?>"],
[html, leftpad(padding), "<?", tag, " ", tag_attrs(attrs, encoder), "?>"],
encoder,
padding
)
Expand Down Expand Up @@ -100,15 +100,21 @@ defmodule Floki.RawHTML do
)
end

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

defp tag_with_attrs(type, [], children, padding),
defp tag_with_attrs(type, [], children, padding, _encoder),
do: [leftpad(padding), "<", type | close_open_tag(type, children)]

defp tag_with_attrs(type, attrs, children, padding),
do: [leftpad(padding), "<", type, ?\s, tag_attrs(attrs) | close_open_tag(type, children)]
defp tag_with_attrs(type, attrs, children, padding, encoder),
do: [
leftpad(padding),
"<",
type,
?\s,
tag_attrs(attrs, encoder) | close_open_tag(type, children)
]

defp close_open_tag(type, children) do
case {type in self_closing_tags(), children} do
Expand All @@ -124,8 +130,15 @@ defmodule Floki.RawHTML do
end
end

defp build_attrs({attr, value}), do: [attr, "=\"", html_escape(value) | "\""]
defp build_attrs(attr), do: attr
defp build_attrs({attr, value}, encoder) do
if Function.info(encoder) == Function.info(&Function.identity/1) do
[attr, "=\"", value | "\""]
else
[attr, "=\"", html_escape(value) | "\""]
end
end

defp build_attrs(attr, _encoder), do: attr

defp tag_for(type, attrs, children, encoder, padding) do
encoder =
Expand All @@ -136,7 +149,7 @@ defmodule Floki.RawHTML do
end

[
tag_with_attrs(type, attrs, children, padding),
tag_with_attrs(type, attrs, children, padding, encoder),
line_ending(padding),
build_raw_html(children, "", encoder, pad_increase(padding)),
close_end_tag(type, children, padding)
Expand Down
7 changes: 7 additions & 0 deletions test/floki_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,13 @@ defmodule FlokiTest do
assert Floki.raw_html(tree, encode: false) == input
end

test "raw_html encode: false does not encode attribute values" do
input = "<html><head></head><body class=\"1 > 0\">< \"test\" ></body></html>"
tree = document!(input)

assert Floki.raw_html(tree, encode: false) == input
end

test "raw_html pretty with doctype" do
html = """
<!doctype html>
Expand Down