From 47000382e8cf9b65653bdd785cc6ae9632394476 Mon Sep 17 00:00:00 2001 From: Andy LeClair Date: Thu, 5 Dec 2024 22:33:22 -0500 Subject: [PATCH] Fix bug propagating identity encoder in `raw_html/2` (#603) * fix bug where encoder for one tag could propagate out to another tag * simplify test * add harder test to fix other bug --- lib/floki/raw_html.ex | 20 +++++++++++--------- test/floki_test.exs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/lib/floki/raw_html.ex b/lib/floki/raw_html.ex index 034f18bd..5a5b722e 100644 --- a/lib/floki/raw_html.ex +++ b/lib/floki/raw_html.ex @@ -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 @@ -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 @@ -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..6acda66f 100644 --- a/test/floki_test.exs +++ b/test/floki_test.exs @@ -443,6 +443,37 @@ 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