Skip to content

Commit fc2ee9b

Browse files
committed
css: adjust parser to allow --foo: {...}
1 parent 209db54 commit fc2ee9b

3 files changed

Lines changed: 65 additions & 13 deletions

File tree

‎internal/css_parser/css_parser.go‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,50 @@ func (p *parser) scanForEndOfRule() (endOfRuleScan, int) {
23092309
var initialStack [4]css_lexer.T
23102310
stack := initialStack[:0]
23112311

2312+
// Small state machine to recognize "--foo:"
2313+
const (
2314+
customPropIdent = iota
2315+
customPropColon
2316+
customPropAccept
2317+
customPropReject
2318+
)
2319+
customProp := customPropIdent
2320+
23122321
for i, t := range p.tokens[p.index:] {
2322+
// People sometimes use CSS custom properties to store JSON. This is
2323+
// problematic because the "{...}" braces look like a CSS block unless
2324+
// this edge case is special-cased. See the (quite long) discussion on
2325+
// https://github.com/w3c/csswg-drafts/issues/7961 for more information.
2326+
//
2327+
// I couldn't find any actual specification for how this is supposed to
2328+
// work. Just a lot of discussion on GitHub. I did find this text, from
2329+
// https://drafts.csswg.org/css-syntax/#consume-a-declaration:
2330+
//
2331+
// Otherwise, if decl’s value contains a top-level simple block with
2332+
// an associated token of <{-token>, and also contains any other non-
2333+
// <whitespace-token> value, return nothing. (That is, a top-level {}-
2334+
// block is only allowed as the entire value of a non-custom property.)
2335+
//
2336+
// However, current browsers appear to accept custom properties with a
2337+
// {}-block contained anywhere inside them, not just as the entire value.
2338+
// So that's what I'm attempting to do here.
2339+
if customProp < customPropAccept {
2340+
switch customProp {
2341+
case customPropIdent:
2342+
if t.Kind == css_lexer.TIdent && strings.HasPrefix(p.source.Contents[t.Range.Loc.Start:], "--") {
2343+
customProp = customPropColon
2344+
} else if t.Kind != css_lexer.TWhitespace {
2345+
customProp = customPropReject
2346+
}
2347+
case customPropColon:
2348+
if t.Kind == css_lexer.TColon {
2349+
customProp = customPropAccept
2350+
} else if t.Kind != css_lexer.TWhitespace {
2351+
customProp = customPropReject
2352+
}
2353+
}
2354+
}
2355+
23132356
switch t.Kind {
23142357
case css_lexer.TSemicolon:
23152358
if len(stack) == 0 {
@@ -2323,7 +2366,7 @@ func (p *parser) scanForEndOfRule() (endOfRuleScan, int) {
23232366
stack = append(stack, css_lexer.TCloseBracket)
23242367

23252368
case css_lexer.TOpenBrace:
2326-
if len(stack) == 0 {
2369+
if len(stack) == 0 && customProp != customPropAccept {
23272370
return endOfRuleOpenBrace, p.index + i
23282371
}
23292372
stack = append(stack, css_lexer.TCloseBrace)

‎internal/css_parser/css_parser_test.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,15 @@ func TestNestedSelector(t *testing.T) {
13611361
"::placeholder {\n opacity: 0.5;\n}\n@layer base {\n ::placeholder {\n color: green;\n }\n}\n", "")
13621362
}
13631363

1364+
func TestCustomPropertyContainingBraces(t *testing.T) {
1365+
// CSS custom properties must be able to store JSON-like values
1366+
// See: https://github.com/w3c/csswg-drafts/issues/7961
1367+
expectPrinted(t, "div { --foo:hover{color:red}zoom:2; }", "div {\n --foo:hover{color:red}zoom:2;\n}\n", "")
1368+
expectPrinted(t, "div { --foo:hover { color: red } zoom: 2; }", "div {\n --foo:hover { color: red } zoom: 2;\n}\n", "")
1369+
expectPrinted(t, "div { --foo : hover { color : red } zoom : 2 ; }", "div {\n --foo: hover { color : red } zoom : 2 ;\n}\n", "")
1370+
expectPrinted(t, "div { --foo + hover { color : red } zoom : 2 ; }", "div {\n --foo + hover {\n color: red;\n }\n zoom: 2;\n}\n", "")
1371+
}
1372+
13641373
func TestBadQualifiedRules(t *testing.T) {
13651374
expectPrinted(t, "$bad: rule;", "$bad: rule; {\n}\n", "<stdin>: WARNING: Unexpected \"$\"\n")
13661375
expectPrinted(t, "$bad: rule; div { color: red }", "$bad: rule; div {\n color: red;\n}\n", "<stdin>: WARNING: Unexpected \"$\"\n")

‎internal/css_printer/css_printer_test.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,19 @@ func TestVerbatimWhitespace(t *testing.T) {
277277
expectPrintedMinify(t, "* { --x:[y ]; }", "*{--x:[y ]}")
278278
expectPrintedMinify(t, "* { --x:[ y]; }", "*{--x:[ y]}")
279279

280-
// Note: These cases now behave like qualified rules
281-
expectPrinted(t, "* { --x:{y}; }", "* {\n --x: {\n y;\n }\n}\n")
282-
expectPrinted(t, "* { --x:{y} ; }", "* {\n --x: {\n y;\n }\n}\n")
283-
expectPrinted(t, "* { --x: {y}; }", "* {\n --x: {\n y;\n }\n}\n")
284-
expectPrinted(t, "* { --x:{y }; }", "* {\n --x: {\n y;\n }\n}\n")
285-
expectPrinted(t, "* { --x:{ y}; }", "* {\n --x: {\n y;\n }\n}\n")
286-
287-
// Note: These cases now behave like qualified rules
280+
// These cases temporarily behaved like qualified rules, but now they're back to being declarations
281+
expectPrinted(t, "* { --x:{y}; }", "* {\n --x:{y};\n}\n")
282+
expectPrinted(t, "* { --x:{y} ; }", "* {\n --x:{y} ;\n}\n")
283+
expectPrinted(t, "* { --x: {y}; }", "* {\n --x: {y};\n}\n")
284+
expectPrinted(t, "* { --x:{y }; }", "* {\n --x:{y };\n}\n")
285+
expectPrinted(t, "* { --x:{ y}; }", "* {\n --x:{ y};\n}\n")
286+
287+
// These cases temporarily behaved like qualified rules, but now they're back to being declarations
288288
expectPrintedMinify(t, "* { --x:{y}; }", "*{--x:{y}}")
289-
expectPrintedMinify(t, "* { --x:{y} ; }", "*{--x:{y}}")
290-
expectPrintedMinify(t, "* { --x: {y}; }", "*{--x:{y}}")
291-
expectPrintedMinify(t, "* { --x:{y }; }", "*{--x:{y}}")
292-
expectPrintedMinify(t, "* { --x:{ y}; }", "*{--x:{y}}")
289+
expectPrintedMinify(t, "* { --x:{y} ; }", "*{--x:{y} }")
290+
expectPrintedMinify(t, "* { --x: {y}; }", "*{--x: {y}}")
291+
expectPrintedMinify(t, "* { --x:{y }; }", "*{--x:{y }}")
292+
expectPrintedMinify(t, "* { --x:{ y}; }", "*{--x:{ y}}")
293293

294294
expectPrintedMinify(t, "@supports ( --x : y , z ) { a { color: red; } }", "@supports ( --x : y , z ){a{color:red}}")
295295
expectPrintedMinify(t, "@supports ( --x : ) { a { color: red; } }", "@supports ( --x : ){a{color:red}}")

0 commit comments

Comments
 (0)