From 026a877d2c2a87084f313bcdb0c1d046a9e18e90 Mon Sep 17 00:00:00 2001 From: Vittoria Bitton Date: Sun, 18 Feb 2024 21:45:18 -0300 Subject: [PATCH 1/2] Keyword validation for html_parser --- lib/floki/html_parser.ex | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/floki/html_parser.ex b/lib/floki/html_parser.ex index 54362ab6..1814a471 100644 --- a/lib/floki/html_parser.ex +++ b/lib/floki/html_parser.ex @@ -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) @@ -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) @@ -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 From be546b2099b8e0318a359051e571e851ec5c597b Mon Sep 17 00:00:00 2001 From: Vittoria Bitton Date: Sun, 18 Feb 2024 21:46:23 -0300 Subject: [PATCH 2/2] Opts validation for remaining functions --- lib/floki.ex | 5 +++-- lib/floki/raw_html.ex | 14 ++++++++------ test/floki_test.exs | 15 ++++++++++++--- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/floki.ex b/lib/floki.ex index 8353b7c0..de1af63c 100644 --- a/lib/floki.ex +++ b/lib/floki.ex @@ -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 @@ -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 diff --git a/lib/floki/raw_html.ex b/lib/floki/raw_html.ex index 42c4fe84..41e72b9c 100644 --- a/lib/floki/raw_html.ex +++ b/lib/floki/raw_html.ex @@ -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 diff --git a/test/floki_test.exs b/test/floki_test.exs index 0bfb66a5..876716c0 100644 --- a/test/floki_test.exs +++ b/test/floki_test.exs @@ -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 @@ -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