Skip to content

Commit 3215f53

Browse files
Improve opts validation with Keyword.validate!/2 (#542)
* Keyword validation for html_parser * Opts validation for remaining functions
1 parent 9a44219 commit 3215f53

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

lib/floki.ex

+3-2
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,7 @@ defmodule Floki do
570570
def text(html, opts \\ []) do
571571
defaults = [deep: true, js: false, style: true, sep: "", include_inputs: false]
572572

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

576575
cleaned_html_tree =
577576
html
@@ -617,6 +616,8 @@ defmodule Floki do
617616
end
618617

619618
def children({_, _, _} = html_node, opts) do
619+
opts = Keyword.validate!(opts, include_text: true)
620+
620621
children(html_node, include_text: opts[:include_text])
621622
end
622623

lib/floki/html_parser.ex

+12-6
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ defmodule Floki.HTMLParser do
3636
result(Floki.html_tree())
3737

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

41-
parser = parser(opts)
42+
parser_args = opts[:parser_args]
43+
44+
parser = opts[:html_parser]
4245

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

5053
def parse_fragment(html, opts \\ []) do
51-
{parser_args, opts} = Keyword.pop(opts, :parser_args, [])
54+
opts =
55+
Keyword.validate!(opts, attributes_as_maps: false, html_parser: parser(), parser_args: [])
56+
57+
parser_args = opts[:parser_args]
5258

53-
parser = parser(opts)
59+
parser = opts[:html_parser]
5460

5561
if opts[:attributes_as_maps] do
5662
parser.parse_fragment_with_attributes_as_maps(html, parser_args)
@@ -59,7 +65,7 @@ defmodule Floki.HTMLParser do
5965
end
6066
end
6167

62-
defp parser(opts) do
63-
opts[:html_parser] || Application.get_env(:floki, :html_parser, @default_parser)
68+
defp parser do
69+
Application.get_env(:floki, :html_parser, @default_parser)
6470
end
6571
end

lib/floki/raw_html.ex

+8-6
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ defmodule Floki.RawHTML do
3434
@encoder &Floki.Entities.encode/1
3535
@no_encoder &Function.identity/1
3636

37-
def raw_html(html_tree, options) do
37+
def raw_html(html_tree, opts) do
38+
opts = Keyword.validate!(opts, encode: true, pretty: false)
39+
3840
encoder =
39-
case Keyword.fetch(options, :encode) do
40-
{:ok, true} -> @encoder
41-
{:ok, false} -> @no_encoder
41+
case opts[:encode] do
42+
true -> @encoder
43+
false -> @no_encoder
4244
:error -> default_encoder()
4345
end
4446

4547
padding =
46-
case Keyword.fetch(options, :pretty) do
47-
{:ok, true} -> %{pad: "", pad_increase: " ", line_ending: "\n", depth: 0}
48+
case opts[:pretty] do
49+
true -> %{pad: "", pad_increase: " ", line_ending: "\n", depth: 0}
4850
_ -> :noop
4951
end
5052

test/floki_test.exs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1606,8 +1606,14 @@ defmodule FlokiTest do
16061606

16071607
assert Floki.children(html_node) == expected
16081608
assert Floki.children(html_node, include_text: true) == expected
1609-
assert Floki.children(html_node, include_text: true, unknown_option: true) == expected
1610-
assert Floki.children(html_node, unknown_option: true) == expected
1609+
1610+
assert_raise ArgumentError, fn ->
1611+
Floki.children(html_node, include_text: true, unknown_option: true)
1612+
end
1613+
1614+
assert_raise ArgumentError, fn ->
1615+
Floki.children(html_node, unknown_option: true)
1616+
end
16111617
end
16121618

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

16241630
assert Floki.children(elements, include_text: false) == expected
1625-
assert Floki.children(elements, include_text: false, unknown_option: true) == expected
1631+
1632+
assert_raise ArgumentError, fn ->
1633+
Floki.children(elements, include_text: false, unknown_option: true)
1634+
end
16261635
end
16271636

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

0 commit comments

Comments
 (0)