Skip to content

Commit

Permalink
Find with descendant selector without html tree (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
ypconstante authored Feb 17, 2024
1 parent 5e7ec16 commit 0840a05
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions lib/floki/finder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ defmodule Floki.Finder do
# - single selector
# - no composite selector
# - no pseudo classes
defp traverse_html_tuples?([%Selector{combinator: nil, pseudo_classes: []}]), do: true
defp traverse_html_tuples?([selector]), do: traverse_html_tuples?(selector)
defp traverse_html_tuples?(selectors) when is_list(selectors), do: false
defp traverse_html_tuples?(%Selector{pseudo_classes: [_ | _]}), do: false
defp traverse_html_tuples?(%Selector{combinator: nil}), do: true

defp traverse_html_tuples?(%Selector{combinator: combinator}),
do: traverse_html_tuples?(combinator)

defp traverse_html_tuples?(%Selector.Combinator{match_type: :descendant, selector: selector}),
do: traverse_html_tuples?(selector)

defp traverse_html_tuples?(_), do: false

# The stack serves as accumulator when there is another combinator to traverse.
Expand Down Expand Up @@ -131,7 +141,34 @@ defmodule Floki.Finder do
defp traverse_html_tuples(
[
{
%Selector{combinator: nil} = selector,
%Selector{
combinator: %Selector.Combinator{
match_type: :descendant,
selector: combinator_selector
}
} = selector,
[{_type, _attributes, children} = html_tuple | selector_rest]
}
| stack
],
acc
) do
stack = [{selector, selector_rest} | stack]

stack =
if Selector.match?(html_tuple, selector, nil) do
[{combinator_selector, children} | stack]
else
[{selector, children} | stack]
end

traverse_html_tuples(stack, acc)
end

defp traverse_html_tuples(
[
{
selector,
[_ | selector_rest]
}
| stack
Expand Down

0 comments on commit 0840a05

Please sign in to comment.