Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions cssselect/xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,27 +634,35 @@ def xpath_nth_child_function(

# early-exit condition 2:
# ~~~~~~~~~~~~~~~~~~~~~~~
# an+b-1 siblings with a<0 and (b-1)<0 is not possible
if a < 0 and b_min_1 < 0:
# an+b-1 siblings with a<=0 and (b-1)<0 is not possible
if a <= 0 and b_min_1 < 0:
return xpath.add_condition("0")

# `add_name_test` boolean is inverted and somewhat counter-intuitive:
#
# nth_of_type() calls nth_child(add_name_test=False)
nodetest = "*" if add_name_test else f"{xpath.element}"

# count siblings before or after the element
# siblings before or after the element
if not last:
siblings_count = f"count(preceding-sibling::{nodetest})"
siblings = f"preceding-sibling::{nodetest}"
else:
siblings_count = f"count(following-sibling::{nodetest})"
siblings = f"following-sibling::{nodetest}"
siblings_count = f"count({siblings})"

# count(***-sibling::***) >= k is tested as ***-sibling::***[k],
# which only walks the k nearest siblings instead of all of them.

# special case of fixed position: nth-*(0n+b)
# if a == 0:
# ~~~~~~~~~~
# count(***-sibling::***) = b-1
if a == 0:
return xpath.add_condition(f"{siblings_count} = {b_min_1}")
if b_min_1 == 0:
return xpath.add_condition(f"not({siblings}[1])")
return xpath.add_condition(
f"{siblings}[{b_min_1}] and not({siblings}[{b_min_1 + 1}])"
)

expressions = []

Expand All @@ -663,12 +671,12 @@ def xpath_nth_child_function(
# so if a>0, and (b-1)<=0, an "n" exists to satisfy this,
# therefore, the predicate is only interesting if (b-1)>0
if b_min_1 > 0:
expressions.append(f"{siblings_count} >= {b_min_1}")
expressions.append(f"{siblings}[{b_min_1}]")
else:
# if a<0, and (b-1)<0, no "n" satisfies this,
# this is tested above as an early exit condition
# otherwise,
expressions.append(f"{siblings_count} <= {b_min_1}")
expressions.append(f"not({siblings}[{b_min_1 + 1}])")

# operations modulo 1 or -1 are simpler, one only needs to verify:
#
Expand Down Expand Up @@ -769,31 +777,31 @@ def xpath_scope_pseudo(self, xpath: XPathExpr) -> XPathExpr:
return xpath.add_condition("position() = 1")

def xpath_first_child_pseudo(self, xpath: XPathExpr) -> XPathExpr:
return xpath.add_condition("count(preceding-sibling::*) = 0")
return xpath.add_condition("not(preceding-sibling::*[1])")

def xpath_last_child_pseudo(self, xpath: XPathExpr) -> XPathExpr:
return xpath.add_condition("count(following-sibling::*) = 0")
return xpath.add_condition("not(following-sibling::*[1])")

def xpath_first_of_type_pseudo(self, xpath: XPathExpr) -> XPathExpr:
self._check_of_type_element(xpath, "first-of-type")
return xpath.add_condition(f"count(preceding-sibling::{xpath.element}) = 0")
return xpath.add_condition(f"not(preceding-sibling::{xpath.element}[1])")

def xpath_last_of_type_pseudo(self, xpath: XPathExpr) -> XPathExpr:
self._check_of_type_element(xpath, "last-of-type")
return xpath.add_condition(f"count(following-sibling::{xpath.element}) = 0")
return xpath.add_condition(f"not(following-sibling::{xpath.element}[1])")

def xpath_only_child_pseudo(self, xpath: XPathExpr) -> XPathExpr:
# Count siblings, not the parent's children: the root element has
# Test siblings, not the parent's children: the root element has
# no parent, but it has no siblings either, so it must match.
return xpath.add_condition(
"count(preceding-sibling::*) = 0 and count(following-sibling::*) = 0"
"not(preceding-sibling::*[1]) and not(following-sibling::*[1])"
)

def xpath_only_of_type_pseudo(self, xpath: XPathExpr) -> XPathExpr:
self._check_of_type_element(xpath, "only-of-type")
return xpath.add_condition(
f"count(preceding-sibling::{xpath.element}) = 0 "
f"and count(following-sibling::{xpath.element}) = 0"
f"not(preceding-sibling::{xpath.element}[1]) "
f"and not(following-sibling::{xpath.element}[1])"
)

def xpath_empty_pseudo(self, xpath: XPathExpr) -> XPathExpr:
Expand Down
56 changes: 29 additions & 27 deletions tests/test_cssselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,65 +751,64 @@ def xpath(css: str) -> str:
assert xpath('e[foo="BAR" s]') == "e[@foo = 'BAR']"

# --- nth-* and nth-last-* -------------------------------------
assert xpath("e:nth-child(1)") == ("e[count(preceding-sibling::*) = 0]")
assert xpath("e:nth-child(1)") == ("e[not(preceding-sibling::*[1])]")

# always true
assert xpath("e:nth-child(n)") == ("e")
assert xpath("e:nth-child(n+1)") == ("e")
# always true too
assert xpath("e:nth-child(n-10)") == ("e")
# b=2 is the limit...
assert xpath("e:nth-child(n+2)") == ("e[count(preceding-sibling::*) >= 1]")
assert xpath("e:nth-child(n+2)") == ("e[preceding-sibling::*[1]]")
# always false
assert xpath("e:nth-child(-n)") == ("e[0]")
assert xpath("e:nth-child(0)") == ("e[0]")
# equivalent to first child
assert xpath("e:nth-child(-n+1)") == ("e[count(preceding-sibling::*) <= 0]")
assert xpath("e:nth-child(-n+1)") == ("e[not(preceding-sibling::*[1])]")

assert xpath("e:nth-child(3n+2)") == (
"e[(count(preceding-sibling::*) >= 1) and "
"e[(preceding-sibling::*[1]) and "
"((count(preceding-sibling::*) +2) mod 3 = 0)]"
)
assert xpath("e:nth-child(3n-2)") == (
"e[count(preceding-sibling::*) mod 3 = 0]"
)
assert xpath("e:nth-child(-n+6)") == ("e[count(preceding-sibling::*) <= 5]")
assert xpath("e:nth-child(-n+6)") == ("e[not(preceding-sibling::*[6])]")

assert xpath("e:nth-last-child(1)") == ("e[count(following-sibling::*) = 0]")
assert xpath("e:nth-last-child(1)") == ("e[not(following-sibling::*[1])]")
assert xpath("e:nth-last-child(2n)") == (
"e[(count(following-sibling::*) +1) mod 2 = 0]"
)
assert xpath("e:nth-last-child(2n+1)") == (
"e[count(following-sibling::*) mod 2 = 0]"
)
assert xpath("e:nth-last-child(2n+2)") == (
"e[(count(following-sibling::*) >= 1) and "
"e[(following-sibling::*[1]) and "
"((count(following-sibling::*) +1) mod 2 = 0)]"
)
assert xpath("e:nth-last-child(3n+1)") == (
"e[count(following-sibling::*) mod 3 = 0]"
)
# represents the two last e elements
assert xpath("e:nth-last-child(-n+2)") == (
"e[count(following-sibling::*) <= 1]"
)
assert xpath("e:nth-last-child(-n+2)") == ("e[not(following-sibling::*[2])]")

assert xpath("e:nth-of-type(1)") == ("e[count(preceding-sibling::e) = 0]")
assert xpath("e:nth-last-of-type(1)") == ("e[count(following-sibling::e) = 0]")
assert xpath("e:nth-of-type(1)") == ("e[not(preceding-sibling::e[1])]")
assert xpath("e:nth-last-of-type(1)") == ("e[not(following-sibling::e[1])]")
assert xpath("div e:nth-last-of-type(1) .aclass") == (
"div/descendant::e[count(following-sibling::e) = 0]"
"div/descendant::e[not(following-sibling::e[1])]"
"/descendant::*[@class and contains(@class, 'aclass') and contains("
"concat(' ', normalize-space(@class), ' '), ' aclass ')]"
)

assert xpath("e:first-child") == ("e[count(preceding-sibling::*) = 0]")
assert xpath("e:last-child") == ("e[count(following-sibling::*) = 0]")
assert xpath("e:first-of-type") == ("e[count(preceding-sibling::e) = 0]")
assert xpath("e:last-of-type") == ("e[count(following-sibling::e) = 0]")
assert xpath("e:first-child") == ("e[not(preceding-sibling::*[1])]")
assert xpath("e:last-child") == ("e[not(following-sibling::*[1])]")
assert xpath("e:first-of-type") == ("e[not(preceding-sibling::e[1])]")
assert xpath("e:last-of-type") == ("e[not(following-sibling::e[1])]")
assert xpath("e:only-child") == (
"e[count(preceding-sibling::*) = 0 and count(following-sibling::*) = 0]"
"e[not(preceding-sibling::*[1]) and not(following-sibling::*[1])]"
)
assert xpath("e:only-of-type") == (
"e[count(preceding-sibling::e) = 0 and count(following-sibling::e) = 0]"
"e[not(preceding-sibling::e[1]) and not(following-sibling::e[1])]"
)
assert xpath("e:empty") == ("e[not(*) and not(string-length())]")
assert xpath("e:EmPTY") == ("e[not(*) and not(string-length())]")
Expand Down Expand Up @@ -872,7 +871,7 @@ def xpath(css: str) -> str:
assert xpath("e:has([bar])") == "e[descendant::*[@bar]]"
assert xpath("e:has(:not(f))") == "e[descendant::*[not(self::f)]]"
assert xpath("e:has(f:nth-child(2))") == (
"e[descendant::f[count(preceding-sibling::*) = 1]]"
"e[descendant::f[preceding-sibling::*[1] and not(preceding-sibling::*[2])]]"
)
# Negating :has(): the relative selector must be kept as a predicate,
# not turned into a literal name test.
Expand Down Expand Up @@ -917,20 +916,22 @@ def xpath(css: str) -> str:
)
# Element-reading pseudo-classes chained after :has()
assert xpath("e:has(f):first-of-type") == (
"e[(descendant::f) and (count(preceding-sibling::e) = 0)]"
"e[(descendant::f) and (not(preceding-sibling::e[1]))]"
)
assert xpath("e:has(f):last-of-type") == (
"e[(descendant::f) and (count(following-sibling::e) = 0)]"
"e[(descendant::f) and (not(following-sibling::e[1]))]"
)
assert xpath("e:has(f):nth-of-type(2)") == (
"e[(descendant::f) and (count(preceding-sibling::e) = 1)]"
"e[(descendant::f) and (preceding-sibling::e[1] "
"and not(preceding-sibling::e[2]))]"
)
assert xpath("e:has(f):nth-last-of-type(2)") == (
"e[(descendant::f) and (count(following-sibling::e) = 1)]"
"e[(descendant::f) and (following-sibling::e[1] "
"and not(following-sibling::e[2]))]"
)
assert xpath("e:has(f):only-of-type") == (
"e[(descendant::f) and (count(preceding-sibling::e) = 0 "
"and count(following-sibling::e) = 0)]"
"e[(descendant::f) and (not(preceding-sibling::e[1]) "
"and not(following-sibling::e[1]))]"
)
with pytest.raises(ExpressionError):
xpath("*:has(f):first-of-type")
Expand All @@ -953,7 +954,8 @@ def xpath(css: str) -> str:
)
assert xpath("e ~ f") == ("e/following-sibling::f")
assert xpath("e ~ f:nth-child(3)") == (
"e/following-sibling::f[count(preceding-sibling::*) = 2]"
"e/following-sibling::f[preceding-sibling::*[2] "
"and not(preceding-sibling::*[3])]"
)
assert xpath("div#container p") == ("div[@id = 'container']/descendant::p")
assert xpath("e:where(foo)") == "e[self::foo]"
Expand Down
Loading