From f7491c65f8289bc45840c7cb0caedb452c525146 Mon Sep 17 00:00:00 2001 From: Yuri Pereira Constante Date: Wed, 3 Jan 2024 11:54:39 -0300 Subject: [PATCH 1/3] Optimize id matching --- lib/floki/selector.ex | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/floki/selector.ex b/lib/floki/selector.ex index b40a96ca..c77472a0 100644 --- a/lib/floki/selector.ex +++ b/lib/floki/selector.ex @@ -103,13 +103,14 @@ 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: attributes}, id) when is_map(attributes) do + id_attr_value = attributes["id"] + id_attr_value == id end defp namespace_match?(_node, namespace) when is_wildcard(namespace), do: true From a2172ab9fa446117369e51bbf1f92f56a1f4d834 Mon Sep 17 00:00:00 2001 From: Yuri Pereira Constante Date: Thu, 4 Jan 2024 11:00:48 -0300 Subject: [PATCH 2/3] Use pattern matching to get id value --- lib/floki/selector.ex | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/floki/selector.ex b/lib/floki/selector.ex index c77472a0..c50be1a7 100644 --- a/lib/floki/selector.ex +++ b/lib/floki/selector.ex @@ -108,11 +108,12 @@ defmodule Floki.Selector do id_attr_value == id end - defp id_match?(%HTMLNode{attributes: attributes}, id) when is_map(attributes) do - id_attr_value = attributes["id"] + defp id_match?(%HTMLNode{attributes: %{"id" => id_attr_value}}, id) do id_attr_value == id end + 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 From a754b67fbc5b9a241efbda68704f94b0c67f98ff Mon Sep 17 00:00:00 2001 From: Yuri Pereira Constante Date: Thu, 4 Jan 2024 12:46:32 -0300 Subject: [PATCH 3/3] Check equality in the pattern match --- lib/floki/selector.ex | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/floki/selector.ex b/lib/floki/selector.ex index c50be1a7..42290d1e 100644 --- a/lib/floki/selector.ex +++ b/lib/floki/selector.ex @@ -108,9 +108,7 @@ defmodule Floki.Selector do id_attr_value == id end - defp id_match?(%HTMLNode{attributes: %{"id" => id_attr_value}}, id) do - id_attr_value == id - end + defp id_match?(%HTMLNode{attributes: %{"id" => id}}, id), do: true defp id_match?(_node, _id), do: false