From 4d12d291f3156608b67972ee340ad4cc404ef77e Mon Sep 17 00:00:00 2001 From: Kirill Mokevnin Date: Tue, 10 Nov 2026 22:08:01 -0500 Subject: [PATCH] CSS: a stray closing token no longer drops the level below zero parseDeclarationError and parseAtRuleUnknown decremented the nesting level on every ')', ']' or '}', also on one without its opening token. The level went below zero, so the ';' or '}' that should end the bad declaration or the at-rule no longer did, and everything after it was swallowed: "color:red);display:none" lost display:none, and ".foo{color:red);display:none}.bar{color:red}" lost the rest of the stylesheet. A closing token now lowers the level only when one is open. Co-Authored-By: Claude Opus 5.5 --- css/parse.go | 6 ++++-- css/parse_test.go | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/css/parse.go b/css/parse.go index 0c8788a..0b64c10 100644 --- a/css/parse.go +++ b/css/parse.go @@ -327,7 +327,8 @@ func (p *Parser) parseAtRuleUnknown() GrammarType { } if p.tt == LeftParenthesisToken || p.tt == LeftBraceToken || p.tt == LeftBracketToken || p.tt == FunctionToken { p.level++ - } else if p.tt == RightParenthesisToken || p.tt == RightBraceToken || p.tt == RightBracketToken { + } else if (p.tt == RightParenthesisToken || p.tt == RightBraceToken || p.tt == RightBracketToken) && 0 < p.level { + // a closing token without its opening one is not a level p.level-- } return TokenGrammar @@ -490,7 +491,8 @@ func (p *Parser) parseDeclarationError(tt TokenType, data []byte) GrammarType { return ErrorGrammar } else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken { p.level++ - } else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken { + } else if (tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken) && 0 < p.level { + // a closing token without its opening one (the offending token of the error) is not a level p.level-- } diff --git a/css/parse_test.go b/css/parse_test.go index f77547c..46ce545 100644 --- a/css/parse_test.go +++ b/css/parse_test.go @@ -114,6 +114,10 @@ func TestParse(t *testing.T) { {false, ".foo { visibility: hidden;” } .bar { color:red; }", ".foo{visibility:hidden;ERROR(expected colon in declaration)}.bar{color:red;}"}, {false, ".foo { baddecl (; color:red; }", ".foo{ERROR(expected colon in declaration)"}, {true, "color=blue;", "ERROR(expected colon in declaration)"}, + {true, "color:red);display:none", "ERROR(unexpected ending in declaration)display:none;"}, + {true, "color:red];display:none", "ERROR(unexpected ending in declaration)display:none;"}, + {false, ".foo { color:red); display:none } .bar { color:red; }", ".foo{ERROR(unexpected ending in declaration)display:none;}.bar{color:red;}"}, + {false, "@foo { a) } .bar { color:red; }", "@foo{a) }.bar{color:red;}"}, // issues {false, "@media print {.class{width:5px;}}", "@media print{.class{width:5px;}}"}, // #6