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

Fix bug propagating identity encoder in raw_html/2 #603

Merged
merged 3 commits into from
Dec 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions lib/floki/raw_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,6 @@ defmodule Floki.RawHTML do
self_closing_tags,
line_ending
) do
encoder =
case type do
"script" -> @no_encoder
"style" -> @no_encoder
"title" -> @no_encoder
_ -> encoder
end

open_tag_content = [
tag_with_attrs(type, attrs, children, pad, encoder, self_closing_tags),
line_ending
Expand All @@ -156,10 +148,19 @@ defmodule Floki.RawHTML do
_ ->
children = List.wrap(children)

curr_encoder =
case type do
"script" -> @no_encoder
"style" -> @no_encoder
"title" -> @no_encoder
_ -> encoder
end

build_raw_html(
children,
acc,
encoder,
# Need to make sure to pass the encoder for the current node
curr_encoder,
pad_increase(pad),
self_closing_tags,
line_ending
Expand All @@ -168,6 +169,7 @@ defmodule Floki.RawHTML do

close_tag_content = close_end_tag(type, children, pad, self_closing_tags, line_ending)
acc = [close_tag_content | acc]
# Return the original encoder here, we don't want to propagate that
build_raw_html(tail, acc, encoder, pad, self_closing_tags, line_ending)
end

Expand Down
31 changes: 31 additions & 0 deletions test/floki_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,37 @@ defmodule FlokiTest do

tree = document!(html_body("<span data-stuff=\"&quot;'\"></span>"))
assert Floki.raw_html(tree) == expected_html

expected_html = ~S"""
<html>
<head>
</head>
<body>
<div>
<style data-attrs-test="{&quot;event&quot;:&quot;buggy software&quot;,&quot;properties&quot;:{&quot;_builderButtonEvent&quot;:true}}">
</style>
<a data-attrs-event="{&quot;event&quot;:&quot;buggy software&quot;,&quot;properties&quot;:{&quot;_builderButtonEvent&quot;:true}}">
Next
</a>
</div>
</body>
</html>
"""

tree =
document!(
html_body(~S"""
<div>
<style data-attrs-test="{&quot;event&quot;:&quot;buggy software&quot;,&quot;properties&quot;:{&quot;_builderButtonEvent&quot;:true}}">
</style>
<a data-attrs-event="{&quot;event&quot;:&quot;buggy software&quot;,&quot;properties&quot;:{&quot;_builderButtonEvent&quot;:true}}">
Next
</a>
</div>
""")
)

assert Floki.raw_html(tree, pretty: true) == expected_html
end

test "raw_html (with >)" do
Expand Down