Skip to content
Closed
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
18 changes: 18 additions & 0 deletions tests/test_tinycss2.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,24 @@ def test_serialize_rules_with_functions():
assert serialize(rules) == source


@pytest.mark.parametrize('source', [
r'1\65 5', # unit "e5"
r'1\45 5', # unit "E5"
r'2\65 3px', # unit "e3px"
])
def test_serialize_dimension_e_unit(source):
# A dimension whose unit starts with e/E followed by a digit must not be
# serialized as a bare "1e5": that re-parses as a number in scientific
# notation, changing the token type (and value). Serialization has to keep
# escaping the leading e so the token round-trips as the same dimension.
(dimension,) = parse_component_value_list(source)
assert dimension.type == 'dimension'
(reparsed,) = parse_component_value_list(serialize([dimension]))
assert reparsed.type == 'dimension'
assert reparsed.value == dimension.value
assert reparsed.lower_unit == dimension.lower_unit


def test_backslash_delim():
source = '\\\nfoo'
tokens = parse_component_value_list(source)
Expand Down
2 changes: 1 addition & 1 deletion tinycss2/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def _serialize_to(self, write):
write(self.representation)
# Disambiguate with scientific notation
unit = self.unit
if unit in ('e', 'E') or unit.startswith(('e-', 'E-')):
if unit[0] in 'eE' and (len(unit) == 1 or unit[1] in '+-0123456789'):
write('\\65 ')
write(serialize_name(unit[1:]))
else:
Expand Down