Skip to content

Commit 62f737b

Browse files
author
annbgn
committed
fix review remarks
1 parent d0f4116 commit 62f737b

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

cssselect/parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ def __repr__(self):
267267
)
268268

269269
def canonical(self):
270-
subsel = self.subselector.canonical()
270+
if not self.subselector:
271+
subsel = '*'
272+
else:
273+
subsel = self.subselector[0].canonical()
271274
if len(subsel) > 1:
272275
subsel = subsel.lstrip("*")
273276
return "%s:has(%s)" % (self.selector.canonical(), subsel)

cssselect/xpath.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,7 @@ def xpath_indirect_adjacent_combinator(self, left, right):
388388

389389
def xpath_relation_descendant_combinator(self, left, right):
390390
"""right is a child, grand-child or further descendant of left; select left"""
391-
return left.join(
392-
"/descendant-or-self::",
393-
right,
394-
closing_combiner="/ancestor-or-self::" + left.element,
395-
)
391+
return left.join("[descendant::", right, closing_combiner="]")
396392

397393
def xpath_relation_child_combinator(self, left, right):
398394
"""right is an immediate child of left; select left"""
@@ -411,11 +407,7 @@ def xpath_relation_direct_adjacent_combinator(self, left, right):
411407

412408
def xpath_relation_indirect_adjacent_combinator(self, left, right):
413409
"""right is a sibling after left, immediately or not; select left"""
414-
return left.join(
415-
"/following-sibling::",
416-
right,
417-
closing_combiner="/preceding-sibling::" + left.element,
418-
)
410+
return left.join("[following-sibling::", right, closing_combiner="]")
419411

420412
# Function: dispatch by function/pseudo-class name
421413

tests/test_cssselect.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,8 @@ def css2css(css, res=None):
305305
css2css(':not(*[foo])', ':not([foo])')
306306
css2css(':not(:empty)')
307307
css2css(':not(#foo)')
308-
# css2css(':has(*)')
309-
# css2css(':has(foo)')
310-
# css2css(':has(*.foo)', ':has(.foo)')
311-
# css2css(':has(*[foo])', ':has([foo])')
312-
# css2css(':has(:empty)')
313-
# css2css(':has(#foo)')
308+
css2css(':has(*)')
309+
css2css(':has(foo)')
314310
css2css('foo:empty')
315311
css2css('foo::before')
316312
css2css('foo:empty::before')
@@ -504,8 +500,8 @@ def xpath(css):
504500
assert xpath('e:nOT(*)') == (
505501
"e[0]") # never matches
506502
assert xpath('e:has(> f)') == 'e[./f]'
507-
assert xpath('e:has(f)') == 'e/descendant-or-self::f/ancestor-or-self::e'
508-
assert xpath('e:has(~ f)') == 'e/following-sibling::f/preceding-sibling::e'
503+
assert xpath('e:has(f)') == 'e[descendant::f]'
504+
assert xpath('e:has(~ f)') == 'e[following-sibling::f]'
509505
assert xpath('e:has(+ f)') == "e/following-sibling::*[(name() = 'f') and (position() = 1)]/preceding-sibling::*[(name() = 'e') and (position() = 1)]"
510506
assert xpath('e f') == (
511507
"e/descendant-or-self::*/f")
@@ -878,9 +874,7 @@ def pcss(main, *selectors, **kwargs):
878874
assert pcss('ol :Not(li[class])') == [
879875
'first-li', 'second-li', 'li-div',
880876
'fifth-li', 'sixth-li', 'seventh-li']
881-
# assert pcss('link:has(*)') == []
882-
# assert pcss('link:has([href])') == ['link-href']
883-
# assert pcss('ol:has(div)') == ['first-ol']
877+
assert pcss('ol:has(div)') == ['first-ol']
884878
assert pcss('ol.a.b.c > li.c:nth-child(3)') == ['third-li']
885879

886880
# Invalid characters in XPath element names, should not crash

0 commit comments

Comments
 (0)