Mercurial > genshi > mirror
changeset 163:9c023c395e44 trunk
Support for XPath number literals including decimal places.
author | cmlenz |
---|---|
date | Wed, 16 Aug 2006 23:03:58 +0000 |
parents | 456039594db9 |
children | 1f6cb675a66c |
files | markup/path.py markup/tests/path.py |
diffstat | 2 files changed, 8 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/markup/path.py +++ b/markup/path.py @@ -224,14 +224,15 @@ _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 @@ -401,7 +402,7 @@ 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('('):
--- a/markup/tests/path.py +++ b/markup/tests/path.py @@ -345,6 +345,10 @@ 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>')