Anonymous avatar Anonymous committed 87f9b38

fix cssselectpatch to match the newer implementation of cssselect. Fixes issue #62, #52 and #59

Comments (0)

Files changed (1)

pyquery/cssselectpatch.py

 #
 # Distributed under the BSD license, see LICENSE.txt
 from cssselect import xpath as cssselect_xpath
+from cssselect.xpath import ExpressionError
 
 
 class JQueryTranslator(cssselect_xpath.HTMLTranslator):
     def xpath_eq_function(self, xpath, function):
         """Matches a single element by its index.
         """
+        if function.argument_types() != ['NUMBER']:
+            raise ExpressionError(
+                    "Expected a single integer for :eq(), got %r"
+                    % function.arguments
+                    )
+        value = int(function.arguments[0].value)
         xpath.add_post_condition(
-                'position() = %s' % (int(function.arguments) + 1))
+                'position() = %s' % (value + 1))
         return xpath
 
     def xpath_gt_function(self, xpath, function):
         """Matches all elements with an index over the given one.
         """
+        if function.argument_types() != ['NUMBER']:
+            raise ExpressionError(
+                    "Expected a single integer for :gt(), got %r"
+                    % function.arguments
+                    )
+        value = int(function.arguments[0].value)
         xpath.add_post_condition(
-                'position() > %s' % (int(function.arguments) + 1))
+                'position() > %s' % (value + 1))
         return xpath
 
     def xpath_lt_function(self, xpath, function):
         """Matches all elements with an index below the given one.
         """
+        if function.argument_types() != ['NUMBER']:
+            raise ExpressionError(
+                    "Expected a single integer for :gt(), got %r"
+                    % function.arguments
+                    )
+        value = int(function.arguments[0].value)
         xpath.add_post_condition(
-                'position() < %s' % (int(function.arguments) + 1))
+                'position() < %s' % (value + 1))
         return xpath
 
     def xpath_contains_function(self, xpath, function):
         """Matches all elements that contain the given text
         """
+        if function.argument_types() != ['STRING']:
+            raise ExpressionError(
+                    "Expected a single string for :contains(), got %r"
+                    % function.arguments
+                    )
+        value = str(function.arguments[0].value)
         xpath.add_post_condition(
-                "contains(text(), '%s')" % str(function.arguments))
+                "contains(text(), '%s')" % value)
         return xpath
 
 
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.