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

[WIP] Support *:nth-of-type through XPath extension function #73

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 24 additions & 1 deletion parsel/csstranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from cssselect import HTMLTranslator as OriginalHTMLTranslator
from cssselect.xpath import XPathExpr as OriginalXPathExpr
from cssselect.xpath import _unicode_safe_getattr, ExpressionError
from cssselect.parser import FunctionalPseudoElement
from cssselect.parser import FunctionalPseudoElement, parse_series


class XPathExpr(OriginalXPathExpr):
Expand Down Expand Up @@ -89,6 +89,29 @@ def xpath_text_simple_pseudo_element(self, xpath):
"""Support selecting text nodes using ::text pseudo-element"""
return XPathExpr.from_xpath(xpath, textnode=True)

def xpath_star_nth_of_type_function(self, xpath, function, last=False):
try:
a, b = parse_series(function.arguments)
except ValueError:
raise ExpressionError("Invalid series: '%r'" % function.arguments)
if not last:
xp_format = 'star-nth-of-type({}, {})'
else:
xp_format = 'star-nth-last-of-type({}, {})'
return xpath.add_condition(xp_format.format(a, b))

def xpath_nth_of_type_function(self, xpath, function):
if xpath.element == '*':
return self.xpath_star_nth_of_type_function(xpath, function)
return self.xpath_nth_child_function(xpath, function,
add_name_test=False)

def xpath_nth_last_of_type_function(self, xpath, function):
if xpath.element == '*':
return self.xpath_star_nth_of_type_function(xpath, function,
last=True)
return self.xpath_nth_child_function(xpath, function, last=True,
add_name_test=False)

class GenericTranslator(TranslatorMixin, OriginalGenericTranslator):
pass
Expand Down
17 changes: 17 additions & 0 deletions parsel/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@
from .csstranslator import HTMLTranslator, GenericTranslator


def css_star_nth_of_type(context, a, b, preceding=True):
node = context.context_node
num_siblings = len(list(node.itersiblings(node.tag, preceding=preceding)))
if a:
return (num_siblings // a) == (b-1)
else:
return num_siblings == (b-1)


def css_star_nth_last_of_type(context, a, b):
return css_star_nth_of_type(context, a, b, preceding=False)

ns = etree.FunctionNamespace(None)
ns['star-nth-of-type'] = css_star_nth_of_type
ns['star-nth-last-of-type'] = css_star_nth_of_type


class SafeXMLParser(etree.XMLParser):
def __init__(self, *args, **kwargs):
kwargs.setdefault('resolve_entities', False)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_selector_csstranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ def test_text_pseudo_element(self):
self.assertEqual(self.x('p::text'), [u'lorem ipsum text'])
self.assertEqual(self.x('p ::text'), [u'lorem ipsum text', u'hi', u'there', u'guy'])

def test_text_star_nth_pseudo_class(self):
self.assertEqual(len(self.x('div *:nth-of-type(2)')), 5)
self.assertEqual(self.x('*:nth-last-of-type(7)::attr(id)'),
['checkbox-disabled-checked'])

def test_attribute_function(self):
self.assertEqual(self.x('#p-b2::attr(id)'), [u'p-b2'])
self.assertEqual(self.x('.cool-footer::attr(class)'), [u'cool-footer'])
Expand Down