Permalink
Browse files

Add tests for series with whitespace

Together with the previous 2 commits, this fixes #2 and #7
  • Loading branch information...
1 parent c221b7b commit d405f8930b76d39e8f70f6394575f889343b5477 @SimonSapin SimonSapin committed Jun 14, 2012
Showing with 35 additions and 24 deletions.
  1. +7 −11 cssselect/parser.py
  2. +26 −11 cssselect/tests.py
  3. +2 −2 cssselect/xpath.py
View
@@ -487,22 +487,18 @@ def parse_attrib(selector, stream):
return Attrib(selector, namespace, attrib, op, value.value)
-def parse_series_from_tokens(tokens):
- for token in tokens:
- if token.type == 'STRING':
- raise ValueError('String tokens not allowed in series.')
- return parse_series(''.join(token.value for token in tokens))
-
-
-def parse_series(s):
+def parse_series(tokens):
"""
- Parses things like '1n+2', or 'an+b' generally
+ Parses the arguments for :nth-child() and friends.
- :raises: :class:`ValueError`
+ :raises: A list of tokens
:returns: :``(a, b)``
"""
- s = s.strip()
+ for token in tokens:
+ if token.type == 'STRING':
+ raise ValueError('String tokens not allowed in series.')
+ s = ''.join(token.value for token in tokens).strip()
if s == 'odd':
return (2, 1)
elif s == 'even':
View
@@ -435,17 +435,32 @@ def test_unicode_escapes(self):
'''descendant-or-self::*[@aval = "' '"]''')
def test_series(self):
- assert parse_series('1n+3') == (1, 3)
- assert parse_series('n-5') == (1, -5)
- assert parse_series('odd') == (2, 1)
- assert parse_series('even') == (2, 0)
- assert parse_series('3n') == (3, 0)
- assert parse_series('n') == (1, 0)
- assert parse_series('+n') == (1, 0)
- assert parse_series('-n') == (-1, 0)
- assert parse_series('5') == (0, 5)
- self.assertRaises(ValueError, parse_series, 'foo')
- self.assertRaises(ValueError, parse_series, 'n+')
+ def series(css):
+ selector, = parse(':nth-child(%s)' % css)
+ args = selector.parsed_tree.arguments
+ try:
+ return parse_series(args)
+ except ValueError:
+ return None
+
+ assert series('1n+3') == (1, 3)
+ assert series('1n +3') == (1, 3)
+ assert series('1n + 3') == (1, 3)
+ assert series('1n+ 3') == (1, 3)
+ assert series('1n-3') == (1, -3)
+ assert series('1n -3') == (1, -3)
+ assert series('1n - 3') == (1, -3)
+ assert series('1n- 3') == (1, -3)
+ assert series('n-5') == (1, -5)
+ assert series('odd') == (2, 1)
+ assert series('even') == (2, 0)
+ assert series('3n') == (3, 0)
+ assert series('n') == (1, 0)
+ assert series('+n') == (1, 0)
+ assert series('-n') == (-1, 0)
+ assert series('5') == (0, 5)
+ assert series('foo') == None
+ assert series('n+') == None
def test_select(self):
document = etree.fromstring(HTML_IDS)
View
@@ -15,7 +15,7 @@
import sys
import re
-from cssselect.parser import parse, parse_series_from_tokens, SelectorError
+from cssselect.parser import parse, parse_series, SelectorError
if sys.version_info[0] < 3:
@@ -330,7 +330,7 @@ def xpath_indirect_adjacent_combinator(self, left, right):
def xpath_nth_child_function(self, xpath, function, last=False,
add_name_test=True):
try:
- a, b = parse_series_from_tokens(function.arguments)
+ a, b = parse_series(function.arguments)
except ValueError:
raise ExpressionError("Invalid series: '%r'" % function.arguments)
if add_name_test:

0 comments on commit d405f89

Please sign in to comment.