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

Find using descendant selector without html tree #537

Merged
Merged
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
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
Loading