Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tests/test_tinycss2.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ def test_nth(input):
return parse_nth(input)


@pytest.mark.parametrize('invalid', ['+', '+/**/', 'n+', 'n +', '-n-', '2n +'])
def test_nth_invalid_does_not_crash(invalid):
# Truncated/invalid An+B fragments must return None per parse_nth's
# documented contract, not raise StopIteration or AttributeError.
assert parse_nth(invalid) is None


def test_nth_leading_plus_whitespace_still_invalid():
# The fix for the above must not make '+ n' (whitespace after a leading
# '+') accidentally valid: only '+n' is a valid nth expression.
assert parse_nth('+n') == (1, 0)
assert parse_nth('+ n') is None


def _number(value):
if value is None:
return 'none'
Expand Down
8 changes: 5 additions & 3 deletions tinycss2/nth.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ def parse_nth(input):
if match:
return parse_end(tokens, 1, int(match.group(1)))
elif token == '+':
token = next(tokens) # Whitespace after an initial '+' is invalid.
if token.type == 'ident':
# Whitespace after an initial '+' is invalid, so the next token is read
# without skipping it. ``None`` is used when the iterator is exhausted.
token = next(tokens, None)
if token is not None and token.type == 'ident':
ident = token.lower_value
if ident == 'n':
return parse_b(tokens, 1)
Expand All @@ -87,7 +89,7 @@ def parse_b(tokens, a):

def parse_signless_b(tokens, a, b_sign):
token = _next_significant(tokens)
if (token.type == 'number' and token.is_integer and
if (token is not None and token.type == 'number' and token.is_integer and
token.representation[0] not in '-+'):
return parse_end(tokens, a, b_sign * token.int_value)

Expand Down
Loading