perf: Optimizing next and previous properties

This commit is contained in:
Karim shoair
2025-08-02 03:37:48 +03:00
parent 548a40d850
commit aec4889d25
+6 -8
View File
@@ -426,10 +426,9 @@ class Selector(SelectorsGeneration):
def next(self) -> Optional["Selector"]: def next(self) -> Optional["Selector"]:
"""Returns the next element of the current element in the children of the parent or ``None`` otherwise.""" """Returns the next element of the current element in the children of the parent or ``None`` otherwise."""
next_element = self._root.getnext() next_element = self._root.getnext()
if next_element is not None: while next_element is not None and isinstance(next_element, html_forbidden):
while isinstance(next_element, html_forbidden): # Ignore HTML comments and unwanted types
# Ignore HTML comments and unwanted types next_element = next_element.getnext()
next_element = next_element.getnext()
return self.__handle_element(next_element) return self.__handle_element(next_element)
@@ -437,10 +436,9 @@ class Selector(SelectorsGeneration):
def previous(self) -> Optional["Selector"]: def previous(self) -> Optional["Selector"]:
"""Returns the previous element of the current element in the children of the parent or ``None`` otherwise.""" """Returns the previous element of the current element in the children of the parent or ``None`` otherwise."""
prev_element = self._root.getprevious() prev_element = self._root.getprevious()
if prev_element is not None: while prev_element is not None and isinstance(prev_element, html_forbidden):
while isinstance(prev_element, html_forbidden): # Ignore HTML comments and unwanted types
# Ignore HTML comments and unwanted types prev_element = prev_element.getprevious()
prev_element = prev_element.getprevious()
return self.__handle_element(prev_element) return self.__handle_element(prev_element)