Skip to content

Commit 3c99b4a

Browse files
committed
Numbers and percent delimiters need a comment separator.
Otherwise they tokenize as a percentage, causing stuff like this to incorrectly work in Firefox: data:text/html,<div style="--x: 20; width: var(--x)%; height: 50px; background: green"></div> I tried a couple other token types and didn't found anything that tokenized differently, so this is an strict improvement.
1 parent 89476cf commit 3c99b4a

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/css-parsing-tests/component_value_list.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,14 @@
360360
], ["ident", "baz"]
361361
]
362362
]
363-
]
363+
],
364+
365+
"4/**/%", [
366+
["number", "4", 4, "integer"], "%"
367+
],
368+
369+
"-/**/%", [ "-", "%"],
370+
371+
"#/**/%", [ "#", "%"]
364372

365373
]

src/serializer.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ impl TokenSerializationType {
423423
/// so that they are not re-parsed as a single token.
424424
///
425425
/// See https://drafts.csswg.org/css-syntax/#serialization
426+
///
427+
/// See https://github.com/w3c/csswg-drafts/issues/4088 for the
428+
/// `DelimPercent` bits.
426429
pub fn needs_separator_when_before(self, other: TokenSerializationType) -> bool {
427430
use self::TokenSerializationTypeVariants::*;
428431
match self.0 {
@@ -442,17 +445,21 @@ impl TokenSerializationType {
442445
other.0,
443446
Ident | Function | UrlOrBadUrl | DelimMinus | Number | Percentage | Dimension | CDC
444447
),
445-
DelimHash | DelimMinus | Number => matches!(
448+
DelimHash | DelimMinus => matches!(
446449
other.0,
447450
Ident | Function | UrlOrBadUrl | DelimMinus | Number | Percentage | Dimension
448451
),
452+
Number => matches!(
453+
other.0,
454+
Ident | Function | UrlOrBadUrl | DelimMinus | Number | Percentage | DelimPercent | Dimension
455+
),
449456
DelimAt => matches!(other.0, Ident | Function | UrlOrBadUrl | DelimMinus),
450457
DelimDotOrPlus => matches!(other.0, Number | Percentage | Dimension),
451458
DelimAssorted | DelimAsterisk => matches!(other.0, DelimEquals),
452459
DelimBar => matches!(other.0, DelimEquals | DelimBar | DashMatch),
453460
DelimSlash => matches!(other.0, DelimAsterisk | SubstringMatch),
454461
Nothing | WhiteSpace | Percentage | UrlOrBadUrl | Function | CDC | OpenParen
455-
| DashMatch | SubstringMatch | DelimQuestion | DelimEquals | Other => false,
462+
| DashMatch | SubstringMatch | DelimQuestion | DelimEquals | DelimPercent | Other => false,
456463
}
457464
}
458465
}
@@ -482,6 +489,7 @@ enum TokenSerializationTypeVariants {
482489
DelimBar, // '|'
483490
DelimSlash, // '/'
484491
DelimAsterisk, // '*'
492+
DelimPercent, // '%'
485493
Other, // anything else
486494
}
487495

@@ -502,6 +510,7 @@ impl<'a> Token<'a> {
502510
Token::Delim('-') => DelimMinus,
503511
Token::Delim('?') => DelimQuestion,
504512
Token::Delim('$') | Token::Delim('^') | Token::Delim('~') => DelimAssorted,
513+
Token::Delim('%') => DelimPercent,
505514
Token::Delim('=') => DelimEquals,
506515
Token::Delim('|') => DelimBar,
507516
Token::Delim('/') => DelimSlash,

0 commit comments

Comments
 (0)