Skip to content

Commit

Permalink
Skip when selector requires children and node doesn't have one
Browse files Browse the repository at this point in the history
  • Loading branch information
ypconstante committed Dec 22, 2023
1 parent 99e6a32 commit 26b07ff
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/floki/selector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,26 @@ defmodule Floki.Selector do
def match?(%Comment{}, _selector, _tree), do: false

def match?(html_node, selector, tree) do
id_match?(html_node, selector.id) && namespace_match?(html_node, selector.namespace) &&
type_match?(html_node, selector.type) && classes_matches?(html_node, selector.classes) &&
can_match_combinator?(html_node, selector.combinator) &&
id_match?(html_node, selector.id) &&
namespace_match?(html_node, selector.namespace) &&
type_match?(html_node, selector.type) &&
classes_matches?(html_node, selector.classes) &&
attributes_matches?(html_node, selector.attributes) &&
pseudo_classes_match?(html_node, selector.pseudo_classes, tree)
end

defp can_match_combinator?(_node, nil), do: true

defp can_match_combinator?(
%HTMLNode{children_nodes_ids: []},
%Selector.Combinator{match_type: match_type}
) when match_type in [:child, :descendant] do
false
end

defp can_match_combinator?(_node, _combinator), do: true

defp id_match?(_node, nil), do: true
defp id_match?(%HTMLNode{attributes: []}, _), do: false
defp id_match?(%HTMLNode{type: :pi}, _), do: false
Expand Down

0 comments on commit 26b07ff

Please sign in to comment.