refactor(parser/find_by_text): Better implementation for ~20% speed boost

This commit is contained in:
Karim shoair
2025-01-31 01:12:00 +02:00
parent 9ea245eab4
commit 2c6142d96d
+4 -11
View File
@@ -907,11 +907,10 @@ class Adaptor(SelectorsGeneration):
if not case_sensitive: if not case_sensitive:
text = text.lower() text = text.lower()
def _traverse(node: Adaptor) -> None: # This selector gets all elements with text content
for node in self.__handle_elements(self._root.xpath('//*[normalize-space(text())]')):
"""Check if element matches given text otherwise, traverse the children tree and iterate""" """Check if element matches given text otherwise, traverse the children tree and iterate"""
node_text = node.text node_text = node.text
# if there's already no text in this node, dodge it to save CPU cycles and time
if node_text:
if clean_match: if clean_match:
node_text = node_text.clean() node_text = node_text.clean()
@@ -924,15 +923,9 @@ class Adaptor(SelectorsGeneration):
elif text == node_text: elif text == node_text:
results.append(node) results.append(node)
if results and first_match: if first_match and results:
# we got an element so we should stop # we got an element so we should stop
return break
for branch in node.children:
_traverse(branch)
# This will block until we traverse all children/branches
_traverse(self)
if first_match: if first_match:
if results: if results: