Skip to content

Commit

Permalink
Fix bug propagating identity encoder in raw_html/2 (#603)
Browse files Browse the repository at this point in the history
* fix bug where encoder for one tag could propagate out to another tag

* simplify test

* add harder test to fix other bug
  • Loading branch information
andyleclair authored Dec 6, 2024
1 parent cabdd7a commit 4700038
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
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

0 comments on commit 4700038

Please sign in to comment.