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

Restrict HTMLParser interface to only accept binaries #507

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion lib/floki/html_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Floki.HTMLParser do
@default_parser Floki.HTMLParser.Mochiweb

@typep result(success) :: {:ok, success} | {:error, String.t()}
@typep html :: binary() | iodata()
@typep html :: binary()

@callback parse_document(html(), Keyword.t()) :: result(Floki.html_tree())
@callback parse_fragment(html(), Keyword.t()) :: result(Floki.html_tree())
Expand Down
8 changes: 4 additions & 4 deletions lib/floki/html_parser/fast_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ defmodule Floki.HTMLParser.FastHtml do
@moduledoc false

@impl true
def parse_document(html, args) do
execute_with_module(fn module -> module.decode(IO.chardata_to_string(html), args) end)
def parse_document(html, args) when is_binary(html) do
execute_with_module(fn module -> module.decode(html, args) end)
end

@impl true
def parse_fragment(html, args) do
execute_with_module(fn module -> module.decode_fragment(IO.chardata_to_string(html), args) end)
def parse_fragment(html, args) when is_binary(html) do
execute_with_module(fn module -> module.decode_fragment(html, args) end)
end

@impl true
Expand Down
10 changes: 5 additions & 5 deletions lib/floki/html_parser/html5ever.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ defmodule Floki.HTMLParser.Html5ever do
@moduledoc false

@impl true
def parse_document(html, _args) do
def parse_document(html, _args) when is_binary(html) do
case Code.ensure_loaded(Html5ever) do
{:module, module} ->
case apply(module, :parse, [IO.chardata_to_string(html)]) do
case apply(module, :parse, [html]) do
{:ok, result} ->
{:ok, result}

Expand All @@ -22,15 +22,15 @@ defmodule Floki.HTMLParser.Html5ever do

# NOTE: html5ever does not implement parse_fragment yet.
@impl true
def parse_fragment(html, args), do: parse_document(html, args)
def parse_fragment(html, args) when is_binary(html), do: parse_document(html, args)

@impl true
def parse_document_with_attributes_as_maps(html, _args) do
def parse_document_with_attributes_as_maps(html, _args) when is_binary(html) do
apply(ensure_module!(), :parse_with_attributes_as_maps, [html])
end

@impl true
def parse_fragment_with_attributes_as_maps(html, _args) do
def parse_fragment_with_attributes_as_maps(html, _args) when is_binary(html) do
apply(ensure_module!(), :parse_with_attributes_as_maps, [html])
end

Expand Down
10 changes: 5 additions & 5 deletions lib/floki/html_parser/mochiweb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ defmodule Floki.HTMLParser.Mochiweb do
@root_node "floki"

@impl true
def parse_document(html, args) do
html = ["<#{@root_node}>", html, "</#{@root_node}>"]
def parse_document(html, args) when is_binary(html) do
html = "<#{@root_node}>" <> html <> "</#{@root_node}>"
{@root_node, _, parsed} = :floki_mochi_html.parse(html, args)

{:ok, parsed}
end

# NOTE: mochi_html cannot make a distinction of a fragment and document.
@impl true
def parse_fragment(html, args), do: parse_document(html, args)
def parse_fragment(html, args) when is_binary(html), do: parse_document(html, args)

@impl true
def parse_document_with_attributes_as_maps(html, args) do
def parse_document_with_attributes_as_maps(html, args) when is_binary(html) do
parse_document(html, Keyword.put(args, :attributes_as_maps, true))
end

@impl true
def parse_fragment_with_attributes_as_maps(html, args) do
def parse_fragment_with_attributes_as_maps(html, args) when is_binary(html) do
parse_document(html, Keyword.put(args, :attributes_as_maps, true))
end
end
2 changes: 1 addition & 1 deletion test/floki_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,7 @@ defmodule FlokiTest do
end

defp html_body(body) do
["<html><head></head><body>", body, "</body></html>"]
"<html><head></head><body>" <> body <> "</body></html>"
end

defp document!(html_string, opts \\ []) do
Expand Down
Loading