Skip to content

Commit

Permalink
simplify test + run darker master HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
annbgn committed Jul 21, 2021
1 parent 47f3c11 commit b64eacf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
29 changes: 15 additions & 14 deletions cssselect/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def __repr__(self):

def canonical(self):
if not self.subselector:
subsel = '*'
subsel = "*"
else:
subsel = self.subselector[0].canonical()
if len(subsel) > 1:
Expand All @@ -287,22 +287,24 @@ class Matching(object):
"""
Represents selector:is(selector_list)
"""

def __init__(self, selector, selector_list):
self.selector = selector
self.selector_list = selector_list

def __repr__(self):
return '%s[%r:is(%s)]' % (
self.__class__.__name__, self.selector, ", ".join(
map(repr, self.selector_list)))
return "%s[%r:is(%s)]" % (
self.__class__.__name__,
self.selector,
", ".join(map(repr, self.selector_list)),
)

def canonical(self):
selector_arguments = []
for s in self.selector_list:
selarg = s.canonical()
selector_arguments.append(selarg.lstrip('*'))
return '%s:is(%s)' % (self.selector.canonical(),
", ".join(map(str, selector_arguments)))
selector_arguments.append(selarg.lstrip("*"))
return "%s:is(%s)" % (self.selector.canonical(), ", ".join(map(str, selector_arguments)))

def specificity(self):
return max([x.specificity() for x in self.selector_list])
Expand Down Expand Up @@ -600,7 +602,7 @@ def parse_simple_selector(stream, inside_negation=False):
elif ident.lower() == "has":
arguments = parse_relative_selector(stream)
result = Relation(result, arguments)
elif ident.lower() in ('matches', 'is'):
elif ident.lower() in ("matches", "is"):
selectors = parse_simple_selector_arguments(stream)
result = Matching(result, selectors)
else:
Expand Down Expand Up @@ -654,20 +656,19 @@ def parse_simple_selector_arguments(stream):
result, pseudo_element = parse_simple_selector(stream, True)
if pseudo_element:
raise SelectorSyntaxError(
'Got pseudo-element ::%s inside function'
% (pseudo_element, ))
"Got pseudo-element ::%s inside function" % (pseudo_element,)
)
stream.skip_whitespace()
next = stream.next()
if next in (('EOF', None), ('DELIM', ',')):
if next in (("EOF", None), ("DELIM", ",")):
stream.next()
stream.skip_whitespace()
arguments.append(result)
elif next == ('DELIM', ')'):
elif next == ("DELIM", ")"):
arguments.append(result)
break
else:
raise SelectorSyntaxError(
"Expected an argument, got %s" % (next,))
raise SelectorSyntaxError("Expected an argument, got %s" % (next,))
return arguments


Expand Down
20 changes: 6 additions & 14 deletions cssselect/xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def __str__(self):
def __repr__(self):
return '%s[%s]' % (self.__class__.__name__, self)

def add_condition(self, condition, conjuction='and'):
def add_condition(self, condition, conjuction="and"):
if self.condition:
self.condition = '(%s) %s (%s)' % (self.condition, conjuction, condition)
self.condition = "(%s) %s (%s)" % (self.condition, conjuction, condition)
else:
self.condition = condition
return self
Expand All @@ -83,9 +83,7 @@ def join(self, combiner, other, closing_combiner=None):
if other.path != '*/':
path += other.path
self.path = path
self.element = (
other.element + closing_combiner if closing_combiner else other.element
)
self.element = other.element + closing_combiner if closing_combiner else other.element
self.condition = other.condition
return self

Expand Down Expand Up @@ -295,7 +293,7 @@ def xpath_matching(self, matching):
for e in exprs:
e.add_name_test()
if e.condition:
xpath.add_condition(e.condition, 'or')
xpath.add_condition(e.condition, "or")
return xpath

def xpath_function(self, function):
Expand Down Expand Up @@ -405,14 +403,8 @@ def xpath_relation_child_combinator(self, left, right):

def xpath_relation_direct_adjacent_combinator(self, left, right):
"""right is a sibling immediately after left; select left"""
left_copy = copy.copy(left)
xpath = left.join("/following-sibling::", right)
xpath.add_name_test()
xpath.add_condition("position() = 1")

xpath = xpath.join("/preceding-sibling::", left_copy)
xpath.add_name_test()
return xpath.add_condition("position() = 1")
xpath = left.add_condition("following-sibling::{}[position() = 1]".format(right.element))
return xpath

def xpath_relation_indirect_adjacent_combinator(self, left, right):
"""right is a sibling after left, immediately or not; select left"""
Expand Down
3 changes: 1 addition & 2 deletions tests/test_cssselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,7 @@ def xpath(css):
assert xpath("e:has(~ f)") == "e[following-sibling::f]"
assert (
xpath("e:has(+ f)")
== "e/following-sibling::*[(name() = 'f') and (position() = 1)]"
"/preceding-sibling::*[(name() = 'e') and (position() = 1)]"
== "e[following-sibling::f[position() = 1]]"
)
assert xpath('e f') == (
"e/descendant-or-self::*/f")
Expand Down

0 comments on commit b64eacf

Please sign in to comment.