Skip to content

Commit

Permalink
Merge pull request #64 from scrapy/add-css2xpath
Browse files Browse the repository at this point in the history
[MRG+1] Add a css2xpath util function available at the top-level
  • Loading branch information
kmike committed Nov 14, 2016
2 parents b077ad5 + 27f00ea commit 8aa8a48
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions parsel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
__version__ = '1.0.3'

from parsel.selector import Selector, SelectorList # NOQA
from parsel.csstranslator import css2xpath # NOQA
8 changes: 8 additions & 0 deletions parsel/csstranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ class GenericTranslator(TranslatorMixin, OriginalGenericTranslator):

class HTMLTranslator(TranslatorMixin, OriginalHTMLTranslator):
pass


_translator = HTMLTranslator()


def css2xpath(query):
"Return translated XPath version of a given CSS query"
return _translator.css_to_xpath(query)
8 changes: 8 additions & 0 deletions tests/test_selector_csstranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ def test_unknown_pseudo_class(self):
self.assertRaises(exc, self.c2x, css)


class UtilCss2XPathTest(unittest.TestCase):
def test_css2xpath(self):
from parsel import css2xpath
expected_xpath = (u"descendant-or-self::*[@class and contains("
"concat(' ', normalize-space(@class), ' '), ' some-class ')]")
self.assertEqual(css2xpath('.some-class'), expected_xpath)


class CSSSelectorTest(unittest.TestCase):

sscls = Selector
Expand Down

0 comments on commit 8aa8a48

Please sign in to comment.