Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for :has(<relative operator>) #115

Merged
merged 18 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
83 changes: 70 additions & 13 deletions cssselect/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,30 +250,66 @@ def specificity(self):
return a1 + a2, b1 + b2, c1 + c2


class Relation(object):
"""
Represents selector:has(subselector)
"""

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

def __repr__(self):
return "%s[%r:has(%r)]" % (
self.__class__.__name__,
self.selector,
self.subselector,
)

def canonical(self):
if not self.subselector:
subsel = "*"
else:
subsel = self.subselector[0].canonical()
if len(subsel) > 1:
subsel = subsel.lstrip("*")
return "%s:has(%s)" % (self.selector.canonical(), subsel)

def specificity(self):
a1, b1, c1 = self.selector.specificity()
a2 = b2 = c2 = 0
if self.subselector:
a2, b2, c2 = self.subselector[-1].specificity()
return a1 + a2, b1 + b2, c1 + c2


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])


class Attrib(object):
"""
Represents selector[namespace|attrib operator value]
Expand Down Expand Up @@ -563,7 +599,10 @@ def parse_simple_selector(stream, inside_negation=False):
if next != ('DELIM', ')'):
raise SelectorSyntaxError("Expected ')', got %s" % (next,))
result = Negation(result, argument)
elif ident.lower() in ('matches', 'is'):
elif ident.lower() == "has":
arguments = parse_relative_selector(stream)
result = Relation(result, arguments)
elif ident.lower() in ("matches", "is"):
selectors = parse_simple_selector_arguments(stream)
result = Matching(result, selectors)
else:
Expand All @@ -585,6 +624,25 @@ def parse_arguments(stream):
if next.type in ('IDENT', 'STRING', 'NUMBER') or next in [
('DELIM', '+'), ('DELIM', '-')]:
arguments.append(next)
elif next == ("DELIM", ")"):
return arguments
else:
raise SelectorSyntaxError("Expected an argument, got %s" % (next,))


def parse_relative_selector(stream):
arguments = []
stream.skip_whitespace()
next = stream.next()
if next in [("DELIM", "+"), ("DELIM", "-"), ("DELIM", ">"), ("DELIM", "~")]:
arguments.append(next)
elif next.type in ("IDENT", "STRING", "NUMBER"):
arguments.append(Element(element=next.value))
while 1:
stream.skip_whitespace()
next = stream.next()
if next.type in ("IDENT", "STRING", "NUMBER"):
arguments.append(Element(element=next.value))
elif next == ('DELIM', ')'):
return arguments
else:
Expand All @@ -598,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
41 changes: 36 additions & 5 deletions cssselect/xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import sys
import re
import copy

from cssselect.parser import parse, parse_series, SelectorError

Expand Down Expand Up @@ -54,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 @@ -76,13 +77,13 @@ def add_star_prefix(self):
"""
self.path += '*/'

def join(self, combiner, other):
def join(self, combiner, other, closing_combiner=None):
path = _unicode(self) + combiner
# Any "star prefix" is redundant when joining.
if other.path != '*/':
path += other.path
self.path = path
self.element = other.element
self.element = other.element + closing_combiner if closing_combiner else other.element
self.condition = other.condition
return self

Expand Down Expand Up @@ -272,13 +273,27 @@ def xpath_negation(self, negation):
else:
return xpath.add_condition('0')

def xpath_relation(self, relation):
xpath = self.xpath(relation.selector)
combinator, *subselector = relation.subselector
if not subselector:
combinator.value = " "
right = self.xpath(combinator)
else:
right = self.xpath(subselector[0])
method = getattr(
self,
"xpath_relation_%s_combinator" % self.combinator_mapping[combinator.value],
)
return method(xpath, right)

def xpath_matching(self, matching):
xpath = self.xpath(matching.selector)
exprs = [self.xpath(selector) for selector in matching.selector_list]
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 @@ -378,6 +393,22 @@ def xpath_indirect_adjacent_combinator(self, left, right):
"""right is a sibling after left, immediately or not"""
return left.join('/following-sibling::', right)

def xpath_relation_descendant_combinator(self, left, right):
"""right is a child, grand-child or further descendant of left; select left"""
return left.join("[descendant::", right, closing_combiner="]")

def xpath_relation_child_combinator(self, left, right):
"""right is an immediate child of left; select left"""
return left.join("[./", right, closing_combiner="]")

def xpath_relation_direct_adjacent_combinator(self, left, right):
"""right is a sibling immediately after left; select left"""
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"""
return left.join("[following-sibling::", right, closing_combiner="]")

# Function: dispatch by function/pseudo-class name

Expand Down
14 changes: 14 additions & 0 deletions tests/test_cssselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ def specificity(css):
assert specificity(':not(:empty)') == (0, 1, 0)
assert specificity(':not(#foo)') == (1, 0, 0)

assert specificity(":has(*)") == (0, 0, 0)
assert specificity(":has(foo)") == (0, 0, 1)
assert specificity(":has(> foo)") == (0, 0, 1)

assert specificity(':is(.foo, #bar)') == (1, 0, 0)
assert specificity(':is(:hover, :visited)') == (0, 1, 0)

Expand Down Expand Up @@ -307,6 +311,8 @@ def css2css(css, res=None):
css2css(':not(*[foo])', ':not([foo])')
css2css(':not(:empty)')
css2css(':not(#foo)')
css2css(":has(*)")
css2css(":has(foo)")
css2css(':is(#bar, .foo)')
css2css(':is(:focused, :visited)')
css2css('foo:empty')
Expand Down Expand Up @@ -505,6 +511,13 @@ def xpath(css):
"e[not(count(preceding-sibling::*) mod 2 = 0)]")
assert xpath('e:nOT(*)') == (
"e[0]") # never matches
assert xpath("e:has(> f)") == "e[./f]"
assert xpath("e:has(f)") == "e[descendant::f]"
assert xpath("e:has(~ f)") == "e[following-sibling::f]"
assert (
xpath("e:has(+ f)")
wRAR marked this conversation as resolved.
Show resolved Hide resolved
== "e[following-sibling::f[position() = 1]]"
)
assert xpath('e f') == (
"e/descendant-or-self::*/f")
assert xpath('e > f') == (
Expand Down Expand Up @@ -876,6 +889,7 @@ def pcss(main, *selectors, **kwargs):
assert pcss('ol :Not(li[class])') == [
'first-li', 'second-li', 'li-div',
'fifth-li', 'sixth-li', 'seventh-li']
assert pcss("ol:has(div)") == ["first-ol"]
assert pcss(':is(#first-li, #second-li)') == [
'first-li', 'second-li']
assert pcss('a:is(#name-anchor, #tag-anchor)') == [
Expand Down