Gael Pasgrimaud avatar Gael Pasgrimaud committed 5aced0f

bump to 1.2.1; fixed #44

Comments (0)

Files changed (5)

+1.2.1
+-----
+
+Allow to use a custom css translator.
+
+Fix issue 44: case problem with xml documents
+
 1.2
 ---
 

pyquery/cssselectpatch.py

         return res
 
 cssselect_xpath.XPathExpr = XPathExpr
-
-
-def selector_to_xpath(selector, prefix='descendant-or-self::'):
-    """JQuery selector to xpath.
-    """
-    selector = selector.replace('[@', '[')
-    return JQueryTranslator().css_to_xpath(selector, prefix)

pyquery/pyquery.py

 # Copyright (C) 2008 - Olivier Lauzanne <olauzanne@gmail.com>
 #
 # Distributed under the BSD license, see LICENSE.txt
-from .cssselectpatch import selector_to_xpath
+from .cssselectpatch import JQueryTranslator
 from copy import deepcopy
 from lxml import etree
 import lxml.html
         else:
             self._parent = no_default
 
+        if 'css_translator' in kwargs:
+            self._translator = kwargs.pop('css_translator')
+        elif self.parser in ('xml',):
+            self._translator = JQueryTranslator(xhtml=True)
+        elif self._parent is not no_default:
+            self._translator = self._parent._translator
+        else:
+            self._translator = JQueryTranslator(xhtml=False)
+
         namespaces = kwargs.get('namespaces', {})
         if 'namespaces' in kwargs:
             del kwargs['namespaces']
 
             # select nodes
             if elements and selector is not no_default:
-                xpath = selector_to_xpath(selector)
+                xpath = self._css_to_xpath(selector)
                 results = [tag.xpath(xpath, namespaces=namespaces) \
                                                     for tag in elements]
                 # Flatten the results
 
         list.__init__(self, elements)
 
+    def _css_to_xpath(self, selector, prefix='descendant-or-self::'):
+        selector = selector.replace('[@', '[')
+        return self._translator.css_to_xpath(selector, prefix)
+
     def __call__(self, *args, **kwargs):
         """return a new PyQuery instance
         """
         if selector is None:
             results = elements
         else:
-            xpath = selector_to_xpath(selector, 'self::')
+            xpath = self._css_to_xpath(selector, 'self::')
             results = []
             for tag in elements:
                 results.extend(tag.xpath(xpath))
             >>> d('p').eq(1).find('em')
             [<em>]
         """
-        xpath = selector_to_xpath(selector)
+        xpath = self._css_to_xpath(selector)
         results = [child.xpath(xpath) for tag in self \
                         for child in tag.getchildren()]
         # Flatten the results
         path = os.path.join(docs, filename)
         exec('%s = type("%s", (TestReadme,), dict(path=path))' % (klass_name, klass_name))
 
+
 class TestTests(doctest.DocFileCase):
     path = os.path.join(dirname, 'tests.txt')
 
         test = parser.get_doctest(doc, globals(), '', self.path, 0)
         doctest.DocFileCase.__init__(self, test, optionflags=doctest.ELLIPSIS)
 
+
 class TestUnicode(unittest.TestCase):
 
     def test_unicode(self):
             self.assertEqual(str(xml), '<p>&#233;</p>')
 
 
+class TestAttributeCase(unittest.TestCase):
+
+    def test_xml_upper_element_name(self):
+        xml = pq('<X>foo</X>', parser='xml')
+        self.assertEqual(len(xml('X')), 1)
+        self.assertEqual(len(xml('x')), 0)
+
+    def test_html_upper_element_name(self):
+        xml = pq('<X>foo</X>', parser='html')
+        self.assertEqual(len(xml('X')), 1)
+        self.assertEqual(len(xml('x')), 1)
+
+
 class TestSelector(unittest.TestCase):
     klass = pq
     html = """
 
 """ % read('README', 'CHANGES')
 
-version = '1.2'
+version = '1.2.1'
 
 setup(name='pyquery',
       version=version,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.