Skip to content

Commit

Permalink
Opts validation for remaining functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vittoriabitton committed Feb 21, 2024
1 parent 026a877 commit be546b2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 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
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 be546b2

Please sign in to comment.