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

Optimize Floki.children #533

Merged
merged 1 commit into from
Feb 9, 2024
Merged
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
20 changes: 11 additions & 9 deletions lib/floki.ex
Original file line number Diff line number Diff line change
Expand Up @@ -609,19 +609,21 @@ defmodule Floki do
"""

@spec children(html_node(), Keyword.t()) :: html_tree() | nil
def children(html_node, opts \\ [include_text: true])

def children(html_node, opts \\ [include_text: true]) do
case html_node do
{_, _, subtree} ->
filter_children(subtree, opts[:include_text])
def children({_, _, subtree}, include_text: false) do
Enum.filter(subtree, &is_tuple/1)
end

_ ->
nil
end
def children({_, _, subtree}, include_text: _) do
subtree
end

def children({_, _, _} = html_node, opts) do
children(html_node, include_text: opts[:include_text])
end

defp filter_children(children, false), do: Enum.filter(children, &is_tuple(&1))
defp filter_children(children, _), do: children
def children(_html_node, _opts), do: nil

@doc """
Returns a list with attribute values for a given selector.
Expand Down
28 changes: 20 additions & 8 deletions test/floki_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1564,10 +1564,17 @@ defmodule FlokiTest do
# Floki.children/2

test "returns the children elements of an element including the text" do
assert Floki.children({"div", [], ["a parent", {"span", [], ["a child"]}]}) == [
"a parent",
{"span", [], ["a child"]}
]
html_node = {"div", [], ["a parent", {"span", [], ["a child"]}]}

expected = [
"a parent",
{"span", [], ["a child"]}
]

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why "unknown_option" is needed here?

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

test "returns the children elements of an element without the text" do
Expand All @@ -1576,14 +1583,19 @@ defmodule FlokiTest do

[elements | _] = Floki.find(html, "body > div")

assert [
{"span", [], ["child 1"]},
{"span", [], ["child 2"]}
] = Floki.children(elements, include_text: false)
expected = [
{"span", [], ["child 1"]},
{"span", [], ["child 2"]}
]

assert Floki.children(elements, include_text: false) == expected
assert Floki.children(elements, include_text: false, unknown_option: true) == expected
philss marked this conversation as resolved.
Show resolved Hide resolved
end

test "returns nil if the given html is not a valid tuple" do
assert Floki.children([]) == nil
assert Floki.children([], include_text: true) == nil
assert Floki.children([], include_text: false) == nil
end

# Floki.attribute/3
Expand Down
Loading