Skip to content

Commit 520b09c

Browse files
authored
Merge pull request #63 from Synrom/fix-escapes-in-dimension-tokens-and-function-blocks
Fix tokenizer crash on escaped Dimension units and Function names
2 parents bfe945e + bbc9fec commit 520b09c

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

tests/test_tinycss2.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,27 @@ def test_backslash_delim():
474474
assert serialize(tokens) == source
475475

476476

477-
def test_bad_unicode():
478-
parse_one_declaration('background:\udca9')
479-
parse_rule_list('@\udca9')
477+
def test_escape_in_at_rule():
478+
at_rule, = parse_rule_list('@\udca9')
479+
assert at_rule.type == 'at-rule'
480+
assert at_rule.at_keyword == '\udca9'
481+
482+
483+
def test_escape_in_ident():
484+
declaration = parse_one_declaration('background:\udca9')
485+
assert declaration.type == 'declaration'
486+
value, = declaration.value
487+
assert value.value == '\udca9'
488+
489+
490+
def test_escape_in_dimension_token():
491+
dimension, = parse_component_value_list('0\\dddf')
492+
assert dimension.type == 'dimension'
493+
assert dimension.int_value == 0
494+
assert dimension.unit == '\udddf'
495+
496+
497+
def test_escape_in_function_name():
498+
function, = parse_component_value_list('\\dddf()')
499+
assert function.type == 'function'
500+
assert function.name == '\udddf'

tinycss2/ast.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,10 @@ def __init__(self, line, column, value, int_value, representation, unit):
559559
self.is_integer = int_value is not None
560560
self.representation = representation
561561
self.unit = unit
562-
self.lower_unit = ascii_lower(unit)
562+
try:
563+
self.lower_unit = ascii_lower(unit)
564+
except UnicodeEncodeError:
565+
self.lower_unit = unit
563566

564567
def _serialize_to(self, write):
565568
write(self.representation)
@@ -693,7 +696,10 @@ class FunctionBlock(Node):
693696
def __init__(self, line, column, name, arguments):
694697
Node.__init__(self, line, column)
695698
self.name = name
696-
self.lower_name = ascii_lower(name)
699+
try:
700+
self.lower_name = ascii_lower(name)
701+
except UnicodeEncodeError:
702+
self.lower_name = name
697703
self.arguments = arguments
698704

699705
def _serialize_to(self, write):

tinycss2/tokenizer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ def parse_component_value_list(css, skip_comments=False):
7070
tokens.append(IdentToken(line, column, value))
7171
continue
7272
pos += 1 # Skip the '('
73-
if ascii_lower(value) == 'url':
73+
try:
74+
is_url = ascii_lower(value) == 'url'
75+
except UnicodeEncodeError:
76+
is_url = False
77+
if is_url:
7478
url_pos = pos
7579
while css.startswith((' ', '\n', '\t'), url_pos):
7680
url_pos += 1

0 commit comments

Comments
 (0)