Commits

Anonymous committed 9c023c3

Support for XPath number literals including decimal places.

Comments (0)

Files changed (2)

     _QUOTES = (("'", "'"), ('"', '"'))
     _TOKENS = ('::', ':', '..', '.', '//', '/', '[', ']', '()', '(', ')', '@',
                '=', '!=', '!', '|', ',', '>=', '>', '<=', '<')
-    _tokenize = re.compile('("[^"]*")|(\'[^\']*\')|(%s)|([^%s\s]+)|\s+' % (
+    _tokenize = re.compile('("[^"]*")|(\'[^\']*\')|((?:\d+)?\.\d+)|(%s)|([^%s\s]+)|\s+' % (
                            '|'.join([re.escape(t) for t in _TOKENS]),
                            ''.join([re.escape(t[0]) for t in _TOKENS]))).findall
 
     def __init__(self, text, filename=None, lineno=-1):
         self.filename = filename
         self.lineno = lineno
-        self.tokens = filter(None, [a or b or c or d for a, b, c, d in
+        self.tokens = filter(None, [dqstr or sqstr or number or token or name
+                                    for dqstr, sqstr, number, token, name in
                                     self._tokenize(text)])
         self.pos = 0
 
         if len(token) > 1 and (token[0], token[-1]) in self._QUOTES:
             self.next_token()
             return StringLiteral(token[1:-1])
-        elif token[0].isdigit():
+        elif token[0].isdigit() or token[0] == '.':
             self.next_token()
             return NumberLiteral(float(token))
         elif not self.at_end and self.peek_token().startswith('('):

markup/tests/path.py

         xml = XML('<root><foo>bar</foo></root>')
         path = Path('*[number("3.0")=3]')
         self.assertEqual('<foo>bar</foo>', path.select(xml).render())
+        path = Path('*[number("3.0")=3.0]')
+        self.assertEqual('<foo>bar</foo>', path.select(xml).render())
+        path = Path('*[number("0.1")=.1]')
+        self.assertEqual('<foo>bar</foo>', path.select(xml).render())
 
     def test_predicate_round_function(self):
         xml = XML('<root><foo>bar</foo></root>')
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.