From a23a9db781a70ce6961e6ae070fdbbed88e92ef1 Mon Sep 17 00:00:00 2001 From: Yuri Pereira Constante Date: Thu, 14 Dec 2023 23:56:58 -0300 Subject: [PATCH] Enable parse of IO data html --- lib/floki/html_parser/fast_html.ex | 4 ++-- lib/floki/html_parser/html5ever.ex | 2 +- lib/floki/html_parser/mochiweb.ex | 2 +- test/floki_test.exs | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/floki/html_parser/fast_html.ex b/lib/floki/html_parser/fast_html.ex index d1bd949b..33229ce7 100644 --- a/lib/floki/html_parser/fast_html.ex +++ b/lib/floki/html_parser/fast_html.ex @@ -4,12 +4,12 @@ defmodule Floki.HTMLParser.FastHtml do @impl true def parse_document(html, args) do - execute_with_module(fn module -> module.decode(html, args) end) + execute_with_module(fn module -> module.decode(IO.chardata_to_string(html), args) end) end @impl true def parse_fragment(html, args) do - execute_with_module(fn module -> module.decode_fragment(html, args) end) + execute_with_module(fn module -> module.decode_fragment(IO.chardata_to_string(html), args) end) end @impl true diff --git a/lib/floki/html_parser/html5ever.ex b/lib/floki/html_parser/html5ever.ex index 38251ae3..4d760848 100644 --- a/lib/floki/html_parser/html5ever.ex +++ b/lib/floki/html_parser/html5ever.ex @@ -7,7 +7,7 @@ defmodule Floki.HTMLParser.Html5ever do def parse_document(html, _args) do case Code.ensure_loaded(Html5ever) do {:module, module} -> - case apply(module, :parse, [html]) do + case apply(module, :parse, [IO.chardata_to_string(html)]) do {:ok, result} -> {:ok, result} diff --git a/lib/floki/html_parser/mochiweb.ex b/lib/floki/html_parser/mochiweb.ex index 56955aca..624e9568 100644 --- a/lib/floki/html_parser/mochiweb.ex +++ b/lib/floki/html_parser/mochiweb.ex @@ -6,7 +6,7 @@ defmodule Floki.HTMLParser.Mochiweb do @impl true def parse_document(html, args) do - html = "<#{@root_node}>#{html}" + html = ["<#{@root_node}>", html, ""] {@root_node, _, parsed} = :floki_mochi_html.parse(html, args) {:ok, parsed} diff --git a/test/floki_test.exs b/test/floki_test.exs index c4cb4d8c..9ff30e28 100644 --- a/test/floki_test.exs +++ b/test/floki_test.exs @@ -262,7 +262,7 @@ defmodule FlokiTest do raw_html = Floki.raw_html(document!(html)) - assert raw_html == html + assert raw_html == IO.chardata_to_string(html) html_with_doctype = [ {:doctype, "html", "", ""}, @@ -283,7 +283,7 @@ defmodule FlokiTest do parsed = document!(span_with_entities) - assert Floki.raw_html(parsed) == span_with_entities + assert Floki.raw_html(parsed) == IO.chardata_to_string(span_with_entities) end test "raw_html/1 with plain text" do @@ -1858,7 +1858,7 @@ defmodule FlokiTest do end) |> Floki.raw_html() - assert result == expects + assert result == IO.chardata_to_string(expects) end test "changing attribute don't change the order of nodes" do @@ -1986,7 +1986,7 @@ defmodule FlokiTest do end defp html_body(body) do - "#{body}" + ["", body, ""] end defp document!(html_string, opts \\ []) do