Skip to content

Commit

Permalink
Improve opts validation with Keyword.validate!/2 (#542)
Browse files Browse the repository at this point in the history
* Keyword validation for html_parser

* Opts validation for remaining functions
  • Loading branch information
vittoriabitton committed Feb 21, 2024
1 parent 9a44219 commit 3215f53
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
5 changes: 3 additions & 2 deletions lib/floki.ex
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,7 @@ defmodule Floki do
def text(html, opts \\ []) do
defaults = [deep: true, js: false, style: true, sep: "", include_inputs: false]

# We can use `Keyword.validate!` when require Elixir 1.13
opts = Keyword.merge(defaults, opts)
opts = Keyword.validate!(opts, defaults)

cleaned_html_tree =
html
Expand Down Expand Up @@ -617,6 +616,8 @@ defmodule Floki do
end

def children({_, _, _} = html_node, opts) do
opts = Keyword.validate!(opts, include_text: true)

children(html_node, include_text: opts[:include_text])
end

Expand Down
18 changes: 12 additions & 6 deletions lib/floki/html_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ defmodule Floki.HTMLParser do
result(Floki.html_tree())

def parse_document(html, opts \\ []) do
{parser_args, opts} = Keyword.pop(opts, :parser_args, [])
opts =
Keyword.validate!(opts, attributes_as_maps: false, html_parser: parser(), parser_args: [])

parser = parser(opts)
parser_args = opts[:parser_args]

parser = opts[:html_parser]

if opts[:attributes_as_maps] do
parser.parse_document_with_attributes_as_maps(html, parser_args)
Expand All @@ -48,9 +51,12 @@ defmodule Floki.HTMLParser do
end

def parse_fragment(html, opts \\ []) do
{parser_args, opts} = Keyword.pop(opts, :parser_args, [])
opts =
Keyword.validate!(opts, attributes_as_maps: false, html_parser: parser(), parser_args: [])

parser_args = opts[:parser_args]

parser = parser(opts)
parser = opts[:html_parser]

if opts[:attributes_as_maps] do
parser.parse_fragment_with_attributes_as_maps(html, parser_args)
Expand All @@ -59,7 +65,7 @@ defmodule Floki.HTMLParser do
end
end

defp parser(opts) do
opts[:html_parser] || Application.get_env(:floki, :html_parser, @default_parser)
defp parser do
Application.get_env(:floki, :html_parser, @default_parser)
end
end
14 changes: 8 additions & 6 deletions lib/floki/raw_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ defmodule Floki.RawHTML do
@encoder &Floki.Entities.encode/1
@no_encoder &Function.identity/1

def raw_html(html_tree, options) do
def raw_html(html_tree, opts) do
opts = Keyword.validate!(opts, encode: true, pretty: false)

encoder =
case Keyword.fetch(options, :encode) do
{:ok, true} -> @encoder
{:ok, false} -> @no_encoder
case opts[:encode] do
true -> @encoder
false -> @no_encoder
:error -> default_encoder()
end

padding =
case Keyword.fetch(options, :pretty) do
{:ok, true} -> %{pad: "", pad_increase: " ", line_ending: "\n", depth: 0}
case opts[:pretty] do
true -> %{pad: "", pad_increase: " ", line_ending: "\n", depth: 0}
_ -> :noop
end

Expand Down
15 changes: 12 additions & 3 deletions test/floki_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,8 +1606,14 @@ defmodule FlokiTest do

assert Floki.children(html_node) == expected
assert Floki.children(html_node, include_text: true) == expected
assert Floki.children(html_node, include_text: true, unknown_option: true) == expected
assert Floki.children(html_node, unknown_option: true) == expected

assert_raise ArgumentError, fn ->
Floki.children(html_node, include_text: true, unknown_option: true)
end

assert_raise ArgumentError, fn ->
Floki.children(html_node, unknown_option: true)
end
end

test "returns the children elements of an element without the text" do
Expand All @@ -1622,7 +1628,10 @@ defmodule FlokiTest do
]

assert Floki.children(elements, include_text: false) == expected
assert Floki.children(elements, include_text: false, unknown_option: true) == expected

assert_raise ArgumentError, fn ->
Floki.children(elements, include_text: false, unknown_option: true)
end
end

test "returns nil if the given html is not a valid tuple" do
Expand Down

0 comments on commit 3215f53

Please sign in to comment.