Skip to content
Open
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
4 changes: 1 addition & 3 deletions cssselect/xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,7 @@ def xpath_direct_adjacent_combinator(
self, left: XPathExpr, right: XPathExpr
) -> XPathExpr:
"""right is a sibling immediately after left"""
xpath = left.join("/following-sibling::", right)
xpath.add_name_test()
return xpath.add_condition("position() = 1")
return left.join("/following-sibling::*[1]/self::", right)

def xpath_indirect_adjacent_combinator(
self, left: XPathExpr, right: XPathExpr
Expand Down
11 changes: 3 additions & 8 deletions tests/test_cssselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,7 @@ def xpath(css: str) -> str:
assert xpath("e:has(f g)") == "e[descendant::f/descendant::g]"
assert xpath("e:has(> f > g)") == "e[./f/g]"
assert xpath("e:has(~ f + g)") == (
"e[following-sibling::f/following-sibling::*"
"[(self::g) and (position() = 1)]]"
"e[following-sibling::f/following-sibling::*[1]/self::g]"
)
# A leading "+" applies to the first step of the argument, so that any
# further step is walked from the adjacent sibling.
Expand Down Expand Up @@ -959,9 +958,7 @@ def xpath(css: str) -> str:
assert xpath("e f") == ("e/descendant::f")
assert xpath("e *") == ("e/descendant-or-self::*/*")
assert xpath("e > f") == ("e/f")
assert xpath("e + f") == (
"e/following-sibling::*[(self::f) and (position() = 1)]"
)
assert xpath("e + f") == ("e/following-sibling::*[1]/self::f")
assert xpath("e ~ f") == ("e/following-sibling::f")
assert xpath("e ~ f:nth-child(3)") == (
"e/following-sibling::f[preceding-sibling::*[2] "
Expand Down Expand Up @@ -1002,9 +999,7 @@ def xpath(css: str) -> str:
assert xpath("e:is(ns|f)") == "e[self::ns:f]"
assert xpath("*:not(ns|f)") == "*[not(self::ns:f)]"
assert xpath("*:not(ns|f > f)") == "*[not((self::f) and parent::*[self::ns:f])]"
assert xpath("x + ns|f") == (
"x/following-sibling::*[(self::ns:f) and (position() = 1)]"
)
assert xpath("x + ns|f") == "x/following-sibling::*[1]/self::ns:f"

# Invalid characters in XPath element names
assert xpath(r"di\a0 v") == ("*[name() = 'di v']") # di\xa0v
Expand Down
Loading