tinycss2 icon indicating copy to clipboard operation
tinycss2 copied to clipboard

Insert separator for more token pairs that would otherwise merge

Open Sanjays2402 opened this issue 5 months ago • 1 comments

Summary

serialize() documents that it "takes care of corner cases ... [and] consecutive identifiers that would otherwise parse back as the same token". It does this by inserting a /**/ comment between any adjacent pair of component values listed in BAD_PAIRS.

BAD_PAIRS is missing seven pairs from the CSS Syntax serialization table. When two such tokens end up adjacent — e.g. after a transform drops the whitespace between them, which is a routine operation — serialize() emits CSS that re-parses to a different token stream, silently changing the meaning of the input.

Reproduction

from tinycss2 import parse_component_value_list, serialize

tokens = parse_component_value_list('1 %')   # number, whitespace, '%' delim
del tokens[1]                                # drop the whitespace (e.g. a minifier)
print(serialize(tokens))                     # -> '1%'
print([t.type for t in parse_component_value_list('1%')])  # -> ['percentage']  (!!)

The two-token list [<number 1>, <delim %>] serializes to 1%, which parses back as a single percentage token.

Affected pairs

All are in the spec serialization table but were absent from BAD_PAIRS:

tokens serialize() re-parses as
number + % delim 1% percentage
number + --> (CDC) 1--> dimension 1-- + >
# delim + - delim #- hash #-
# delim + --> (CDC) #--> hash #-- + >
- delim + - delim -- ident --
- delim + --> (CDC) ---> ident --- + >
@ delim + --> (CDC) @--> at-keyword @-- + >

Root cause

A lone - delimiter merges after #/- (forming a hash or ident); a CDC (-->) merges after #/-/number/@ because its leading -- is consumed as hash / ident / dimension-unit / at-keyword characters; and a % delimiter merges into a preceding number to form a percentage. dimension+CDC and the number+ident/function/url pairs were already covered — these seven were the gap.

I derived the exact gap by transcribing the spec's 12-column serialization table and diffing it against BAD_PAIRS; the diff was precisely these seven pairs, and an exhaustive pairwise round-trip check over all token types flagged the same seven (and nothing else).

Fix

Add the seven missing pairs, mirroring the existing spec-table style already used in BAD_PAIRS (+9 lines in serializer.py, comment included).

Tests

New parametrized test_serialize_adjacent_tokens_do_not_merge (modeled on the existing test_backslash_delim): for each pair it parses the whitespace-separated source, deletes the whitespace token so the two component values are adjacent, serializes, re-parses, and asserts the token types and serializations are unchanged.

Proof the test guards the fix (stashing only the serializer.py change):

  • without the fix: 9 failed
  • with the fix: 9 passed
  • full suite: 16984 passed (was 16975), no regressions
  • ruff check clean on both changed files

Diff is +34 / -0 across tinycss2/serializer.py and tests/test_tinycss2.py.

Sanjays2402 avatar Jul 03 '26 06:07 Sanjays2402

Hi!

Thanks for this pull request.

I’m curious: what’s your use case? We don’t meet people who serialize their manually created list of tokens that often! 😆

liZe avatar Jul 03 '26 07:07 liZe

This came from a parse → transform → serialize round trip rather than a downstream production bug report. For example, a transformer or minifier can parse 1 %, remove the whitespace token, then serialize the remaining number and % delimiter; the current output 1% reparses as one percentage token instead of the original two tokens. The other added pairs are the same class of omission from the CSS Syntax serialization table.

I don't have a concrete downstream package currently hitting this. If preserving manually transformed token streams is outside tinycss2's intended serializer contract, closing this is reasonable.

Sanjays2402 avatar Jul 14 '26 07:07 Sanjays2402

I get what’s going on. The specification changed in 2014, after the serializer has been added in tinycss2. The new spec has better definitions with a nice table, we should follow it to fill BAD_PAIRS in a way that’s close to this table, instead of adding small workarounds to the older list.

liZe avatar Jul 20 '26 10:07 liZe