From 46227f3eca6d7e397bdf721d5e0980e7aefaa94a Mon Sep 17 00:00:00 2001 From: Siraj saheb Date: Wed, 28 Oct 2026 20:49:07 +0530 Subject: [PATCH 1/2] avoid exponential :not() XPath in _xpath_match_condition _xpath_match_condition() nested each reverse combinator step inside the previous step's predicate, and libxml2 evaluates a nested predicate again for every node the outer axis reaches, so a complex :not() argument with k combinators took time exponential in k on deep or wide documents (4.4 s for :not(x * * * * * * * *) on a 30-deep chain). Emit the steps as one location path instead, which selects the same elements and lets each step's node-set be merged. --- cssselect/xpath.py | 20 +++++++++++++++----- tests/test_cssselect.py | 13 ++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/cssselect/xpath.py b/cssselect/xpath.py index c46c500..5fd9cb6 100644 --- a/cssselect/xpath.py +++ b/cssselect/xpath.py @@ -329,12 +329,22 @@ def _xpath_match_condition(self, selector: Tree) -> str | None: axes for combinators. """ if isinstance(selector, CombinedSelector): - axis = self._reverse_combinator_mapping[selector.combinator] - left = self._xpath_match_condition(selector.selector) - if left is not None: - axis = f"{axis}[{left}]" + # Walk the combinators as a single location path, not as nested + # predicates: a nested predicate is evaluated again for each node + # its axis reaches, which takes time exponential in the number of + # combinators (e.g. :not(x * * * * * *) in a deep document). + steps = [] + left: Tree = selector + while isinstance(left, CombinedSelector): + axis = self._reverse_combinator_mapping[left.combinator] + left = left.selector + condition = self._xpath_match_condition( + left.subselector if isinstance(left, CombinedSelector) else left + ) + steps.append(axis if condition is None else f"{axis}[{condition}]") + path = "/".join(steps) right = self._xpath_match_condition(selector.subselector) - return axis if right is None else f"({right}) and {axis}" + return path if right is None else f"({right}) and {path}" sub_xpath = self.xpath(selector) sub_xpath.add_name_test() return sub_xpath.condition or None diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py index 266bbca..55ee8cc 100644 --- a/tests/test_cssselect.py +++ b/tests/test_cssselect.py @@ -892,7 +892,16 @@ def xpath(css: str) -> str: "e[not((self::b) and preceding-sibling::*[self::a])]" ) assert xpath("e:not(a > b > c)") == ( - "e[not((self::c) and parent::*[(self::b) and parent::*[self::a]])]" + "e[not((self::c) and parent::*[self::b]/parent::*[self::a])]" + ) + # Further combinators extend a single location path: nested + # predicates would take exponential time to evaluate. + assert xpath("e:not(a b ~ c + d > f)") == ( + "e[not((self::f) and parent::*[self::d]/preceding-sibling::*[1][self::c]" + "/preceding-sibling::*[self::b]/ancestor::*[self::a])]" + ) + assert xpath("e:not(a * * f)") == ( + "e[not((self::f) and ancestor::*/ancestor::*/ancestor::*[self::a])]" ) assert xpath("e:not(.a > #b)") == ( "e[not((@id = 'b') and parent::*[@class and contains(@class, 'a') and contains(" @@ -1760,6 +1769,8 @@ def pcss(main: str, *selectors: str, **kwargs: bool) -> list[str]: "seventh-li", ] assert pcss("li:not(#second-li ~ li)") == ["first-li", "second-li"] + assert pcss("li:not(div > ol li + li ~ li)") == ["first-li", "second-li"] + assert pcss("div:not(body > * * *)") == ["outer-div", "foobar-div"] assert pcss("li:has(div):nth-of-type(2)") == ["second-li"] assert pcss("li:has(div):first-of-type") == [] assert pcss("ol:has(li):first-of-type") == ["first-ol"] From 1c389d3b1ecf4f3642504d1aa9c8eb3aaaabc2b0 Mon Sep 17 00:00:00 2001 From: Siraj saheb Date: Thu, 29 Oct 2026 22:53:49 +0530 Subject: [PATCH 2/2] drop comments about nested predicates --- cssselect/xpath.py | 4 ---- tests/test_cssselect.py | 2 -- 2 files changed, 6 deletions(-) diff --git a/cssselect/xpath.py b/cssselect/xpath.py index 5fd9cb6..5f24e33 100644 --- a/cssselect/xpath.py +++ b/cssselect/xpath.py @@ -329,10 +329,6 @@ def _xpath_match_condition(self, selector: Tree) -> str | None: axes for combinators. """ if isinstance(selector, CombinedSelector): - # Walk the combinators as a single location path, not as nested - # predicates: a nested predicate is evaluated again for each node - # its axis reaches, which takes time exponential in the number of - # combinators (e.g. :not(x * * * * * *) in a deep document). steps = [] left: Tree = selector while isinstance(left, CombinedSelector): diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py index 55ee8cc..2a65b1f 100644 --- a/tests/test_cssselect.py +++ b/tests/test_cssselect.py @@ -894,8 +894,6 @@ def xpath(css: str) -> str: assert xpath("e:not(a > b > c)") == ( "e[not((self::c) and parent::*[self::b]/parent::*[self::a])]" ) - # Further combinators extend a single location path: nested - # predicates would take exponential time to evaluate. assert xpath("e:not(a b ~ c + d > f)") == ( "e[not((self::f) and parent::*[self::d]/preceding-sibling::*[1][self::c]" "/preceding-sibling::*[self::b]/ancestor::*[self::a])]"