Skip to content

Commit

Permalink
Optimize id matching (#519)
Browse files Browse the repository at this point in the history
* Optimize id matching

* Use pattern matching to get id value

* Check equality in the pattern match
  • Loading branch information
ypconstante authored Jan 4, 2024
1 parent 725e530 commit 18a2cf8
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/floki/selector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ defmodule Floki.Selector do
defp id_match?(%HTMLNode{attributes: []}, _), do: false
defp id_match?(%HTMLNode{type: :pi}, _), do: false

defp id_match?(%HTMLNode{attributes: attributes}, id) do
Enum.any?(attributes, fn attribute ->
case attribute do
{"id", ^id} -> true
_ -> false
end
end)
defp id_match?(%HTMLNode{attributes: attributes}, id) when is_list(attributes) do
id_attr_value = :proplists.get_value("id", attributes, nil)
id_attr_value == id
end

defp id_match?(%HTMLNode{attributes: %{"id" => id}}, id), do: true

defp id_match?(_node, _id), do: false

defp namespace_match?(_node, namespace) when is_wildcard(namespace), do: true
defp namespace_match?(%HTMLNode{type: :pi}, _type), do: false

Expand Down

0 comments on commit 18a2cf8

Please sign in to comment.