Commits

Anonymous committed 9c023c3

Support for XPath number literals including decimal places.

  • Participants
  • Parent commits 4560395
  • Branches trunk

Comments (0)

Files changed (2)

File markup/path.py

     _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('('):

File 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>')