Skip to content

Commit b64eacf

Browse files
author
annbgn
committed
simplify test + run darker master HEAD
1 parent 47f3c11 commit b64eacf

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

cssselect/parser.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def __repr__(self):
268268

269269
def canonical(self):
270270
if not self.subselector:
271-
subsel = '*'
271+
subsel = "*"
272272
else:
273273
subsel = self.subselector[0].canonical()
274274
if len(subsel) > 1:
@@ -287,22 +287,24 @@ class Matching(object):
287287
"""
288288
Represents selector:is(selector_list)
289289
"""
290+
290291
def __init__(self, selector, selector_list):
291292
self.selector = selector
292293
self.selector_list = selector_list
293294

294295
def __repr__(self):
295-
return '%s[%r:is(%s)]' % (
296-
self.__class__.__name__, self.selector, ", ".join(
297-
map(repr, self.selector_list)))
296+
return "%s[%r:is(%s)]" % (
297+
self.__class__.__name__,
298+
self.selector,
299+
", ".join(map(repr, self.selector_list)),
300+
)
298301

299302
def canonical(self):
300303
selector_arguments = []
301304
for s in self.selector_list:
302305
selarg = s.canonical()
303-
selector_arguments.append(selarg.lstrip('*'))
304-
return '%s:is(%s)' % (self.selector.canonical(),
305-
", ".join(map(str, selector_arguments)))
306+
selector_arguments.append(selarg.lstrip("*"))
307+
return "%s:is(%s)" % (self.selector.canonical(), ", ".join(map(str, selector_arguments)))
306308

307309
def specificity(self):
308310
return max([x.specificity() for x in self.selector_list])
@@ -600,7 +602,7 @@ def parse_simple_selector(stream, inside_negation=False):
600602
elif ident.lower() == "has":
601603
arguments = parse_relative_selector(stream)
602604
result = Relation(result, arguments)
603-
elif ident.lower() in ('matches', 'is'):
605+
elif ident.lower() in ("matches", "is"):
604606
selectors = parse_simple_selector_arguments(stream)
605607
result = Matching(result, selectors)
606608
else:
@@ -654,20 +656,19 @@ def parse_simple_selector_arguments(stream):
654656
result, pseudo_element = parse_simple_selector(stream, True)
655657
if pseudo_element:
656658
raise SelectorSyntaxError(
657-
'Got pseudo-element ::%s inside function'
658-
% (pseudo_element, ))
659+
"Got pseudo-element ::%s inside function" % (pseudo_element,)
660+
)
659661
stream.skip_whitespace()
660662
next = stream.next()
661-
if next in (('EOF', None), ('DELIM', ',')):
663+
if next in (("EOF", None), ("DELIM", ",")):
662664
stream.next()
663665
stream.skip_whitespace()
664666
arguments.append(result)
665-
elif next == ('DELIM', ')'):
667+
elif next == ("DELIM", ")"):
666668
arguments.append(result)
667669
break
668670
else:
669-
raise SelectorSyntaxError(
670-
"Expected an argument, got %s" % (next,))
671+
raise SelectorSyntaxError("Expected an argument, got %s" % (next,))
671672
return arguments
672673

673674

cssselect/xpath.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ def __str__(self):
5555
def __repr__(self):
5656
return '%s[%s]' % (self.__class__.__name__, self)
5757

58-
def add_condition(self, condition, conjuction='and'):
58+
def add_condition(self, condition, conjuction="and"):
5959
if self.condition:
60-
self.condition = '(%s) %s (%s)' % (self.condition, conjuction, condition)
60+
self.condition = "(%s) %s (%s)" % (self.condition, conjuction, condition)
6161
else:
6262
self.condition = condition
6363
return self
@@ -83,9 +83,7 @@ def join(self, combiner, other, closing_combiner=None):
8383
if other.path != '*/':
8484
path += other.path
8585
self.path = path
86-
self.element = (
87-
other.element + closing_combiner if closing_combiner else other.element
88-
)
86+
self.element = other.element + closing_combiner if closing_combiner else other.element
8987
self.condition = other.condition
9088
return self
9189

@@ -295,7 +293,7 @@ def xpath_matching(self, matching):
295293
for e in exprs:
296294
e.add_name_test()
297295
if e.condition:
298-
xpath.add_condition(e.condition, 'or')
296+
xpath.add_condition(e.condition, "or")
299297
return xpath
300298

301299
def xpath_function(self, function):
@@ -405,14 +403,8 @@ def xpath_relation_child_combinator(self, left, right):
405403

406404
def xpath_relation_direct_adjacent_combinator(self, left, right):
407405
"""right is a sibling immediately after left; select left"""
408-
left_copy = copy.copy(left)
409-
xpath = left.join("/following-sibling::", right)
410-
xpath.add_name_test()
411-
xpath.add_condition("position() = 1")
412-
413-
xpath = xpath.join("/preceding-sibling::", left_copy)
414-
xpath.add_name_test()
415-
return xpath.add_condition("position() = 1")
406+
xpath = left.add_condition("following-sibling::{}[position() = 1]".format(right.element))
407+
return xpath
416408

417409
def xpath_relation_indirect_adjacent_combinator(self, left, right):
418410
"""right is a sibling after left, immediately or not; select left"""

tests/test_cssselect.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,7 @@ def xpath(css):
516516
assert xpath("e:has(~ f)") == "e[following-sibling::f]"
517517
assert (
518518
xpath("e:has(+ f)")
519-
== "e/following-sibling::*[(name() = 'f') and (position() = 1)]"
520-
"/preceding-sibling::*[(name() = 'e') and (position() = 1)]"
519+
== "e[following-sibling::f[position() = 1]]"
521520
)
522521
assert xpath('e f') == (
523522
"e/descendant-or-self::*/f")

0 commit comments

Comments
 (0)