From 9804cbca9f5559b32b80e38365e1fbe6bcac0cfe Mon Sep 17 00:00:00 2001 From: Andy LeClair Date: Thu, 5 Dec 2024 16:19:55 -0500 Subject: [PATCH 1/3] fix bug where encoder for one tag could propagate out to another tag --- lib/floki/raw_html.ex | 8 +++--- test/floki_test.exs | 57 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/lib/floki/raw_html.ex b/lib/floki/raw_html.ex index 034f18bd..fb04ffeb 100644 --- a/lib/floki/raw_html.ex +++ b/lib/floki/raw_html.ex @@ -133,7 +133,7 @@ defmodule Floki.RawHTML do self_closing_tags, line_ending ) do - encoder = + curr_encoder = case type do "script" -> @no_encoder "style" -> @no_encoder @@ -142,7 +142,7 @@ defmodule Floki.RawHTML do end open_tag_content = [ - tag_with_attrs(type, attrs, children, pad, encoder, self_closing_tags), + tag_with_attrs(type, attrs, children, pad, curr_encoder, self_closing_tags), line_ending ] @@ -159,7 +159,8 @@ defmodule Floki.RawHTML do 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 @@ -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 diff --git a/test/floki_test.exs b/test/floki_test.exs index 04696b73..48b51002 100644 --- a/test/floki_test.exs +++ b/test/floki_test.exs @@ -443,6 +443,63 @@ defmodule FlokiTest do tree = document!(html_body("")) assert Floki.raw_html(tree) == expected_html + + expected_html = ~S""" + + + + +
+
+
+
+
+
+
+ + + Next + +
+
+
+
+
+
+
+ + + """ + + tree = + document!( + html_body(~S""" +
+
+
+
+
+
+
+ + + Next + +
+
+
+
+
+
+
+ """) + ) + + assert Floki.raw_html(tree, pretty: true) == expected_html end test "raw_html (with >)" do From 355364ffc7e223a14befff0406487500ccd54a3e Mon Sep 17 00:00:00 2001 From: Andy LeClair Date: Thu, 5 Dec 2024 16:31:11 -0500 Subject: [PATCH 2/3] simplify test --- test/floki_test.exs | 50 +++++++++++---------------------------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/test/floki_test.exs b/test/floki_test.exs index 48b51002..be2d1acd 100644 --- a/test/floki_test.exs +++ b/test/floki_test.exs @@ -449,25 +449,12 @@ defmodule FlokiTest do -
-
-
-
-
-
-
- - - Next - -
-
-
-
-
-
+
+ + + Next +
@@ -476,25 +463,12 @@ defmodule FlokiTest do tree = document!( html_body(~S""" -
-
-
-
-
-
-
- - - Next - -
-
-
-
-
-
+
+ + + Next +
""") ) From 036413253cb43d2c0f8bc9a9a8ef93c97f651db6 Mon Sep 17 00:00:00 2001 From: Andy LeClair Date: Thu, 5 Dec 2024 16:57:05 -0500 Subject: [PATCH 3/3] add harder test to fix other bug --- lib/floki/raw_html.ex | 18 +++++++++--------- test/floki_test.exs | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/floki/raw_html.ex b/lib/floki/raw_html.ex index fb04ffeb..5a5b722e 100644 --- a/lib/floki/raw_html.ex +++ b/lib/floki/raw_html.ex @@ -133,16 +133,8 @@ defmodule Floki.RawHTML do self_closing_tags, line_ending ) do - curr_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, curr_encoder, self_closing_tags), + tag_with_attrs(type, attrs, children, pad, encoder, self_closing_tags), line_ending ] @@ -156,6 +148,14 @@ 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, diff --git a/test/floki_test.exs b/test/floki_test.exs index be2d1acd..6acda66f 100644 --- a/test/floki_test.exs +++ b/test/floki_test.exs @@ -450,7 +450,7 @@ defmodule FlokiTest do