Skip to content

Commit

Permalink
Merge pull request #20 from scrapy/docstrings-for-csstranslator
Browse files Browse the repository at this point in the history
Adding docstrings for csstranslator
  • Loading branch information
kmike committed Aug 24, 2015
2 parents c88d95f + cc9e591 commit 241285c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions parsel/csstranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,27 @@ def join(self, combiner, other):


class TranslatorMixin(object):
"""This mixin adds support to CSS pseudo elements via dynamic dispatch.
Currently supported pseudo-elements are ``::text`` and ``::attr(ATTR_NAME)``.
"""

def xpath_element(self, selector):
xpath = super(TranslatorMixin, self).xpath_element(selector)
return XPathExpr.from_xpath(xpath)

def xpath_pseudo_element(self, xpath, pseudo_element):
"""
Dispatch method that transforms XPath to support pseudo-element
"""
if isinstance(pseudo_element, FunctionalPseudoElement):
method = 'xpath_%s_functional_pseudo_element' % (
pseudo_element.name.replace('-', '_'))
method = _unicode_safe_getattr(self, method, None)
if not method:
raise ExpressionError(
"The functional pseudo-element ::%s() is unknown"
% pseudo_element.name)
% pseudo_element.name)
xpath = method(xpath, pseudo_element)
else:
method = 'xpath_%s_simple_pseudo_element' % (
Expand All @@ -69,12 +76,14 @@ def xpath_pseudo_element(self, xpath, pseudo_element):
return xpath

def xpath_attr_functional_pseudo_element(self, xpath, function):
"""Support selecting attribute values using ::attr() pseudo-element
"""
if function.argument_types() not in (['STRING'], ['IDENT']):
raise ExpressionError(
"Expected a single string or ident for ::attr(), got %r"
% function.arguments)
return XPathExpr.from_xpath(xpath,
attribute=function.arguments[0].value)
attribute=function.arguments[0].value)

def xpath_text_simple_pseudo_element(self, xpath):
"""Support selecting text nodes using ::text pseudo-element"""
Expand All @@ -87,4 +96,3 @@ class GenericTranslator(TranslatorMixin, OriginalGenericTranslator):

class HTMLTranslator(TranslatorMixin, OriginalHTMLTranslator):
pass

0 comments on commit 241285c

Please sign in to comment.