Skip to content

Commit

Permalink
CSS: improve parsing of bad/unknown syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Sep 4, 2023
1 parent 1870183 commit f4d0fde
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions css/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ func (p *Parser) parseAtRule() GrammarType {
} else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken {
p.level++
} else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken {
if p.level == 0 {
// TODO: buggy
p.pushBuf(tt, data)
if 1 < len(p.state) {
p.state = p.state[:len(p.state)-1]
}
p.err, p.errPos = "CSS parse error: unexpected ending in at rule", p.l.r.Offset()
return ErrorGrammar
}
p.level--
}
if first {
Expand Down Expand Up @@ -344,6 +353,15 @@ func (p *Parser) parseQualifiedRule() GrammarType {
} else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken {
p.level++
} else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken {
if p.level == 0 {
// TODO: buggy
p.pushBuf(tt, data)
if 1 < len(p.state) {
p.state = p.state[:len(p.state)-1]
}
p.err, p.errPos = "CSS parse error: unexpected ending in qualified rule", p.l.r.Offset()
return ErrorGrammar
}
p.level--
}
if len(data) == 1 && (data[0] == ',' || data[0] == '>' || data[0] == '+' || data[0] == '~') {
Expand Down Expand Up @@ -399,6 +417,13 @@ func (p *Parser) parseDeclaration() GrammarType {
} else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken {
p.level++
} else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken {
if p.level == 0 {
// TODO: buggy
p.err, p.errPos = "CSS parse error: unexpected ending in declaration", p.l.r.Offset()
p.pushBuf(ttName, dataName)
p.pushBuf(ColonToken, []byte{':'})
return p.parseDeclarationError(tt, data)
}
p.level--
}
if len(data) == 1 && (data[0] == ',' || data[0] == '/' || data[0] == ':' || data[0] == '!' || data[0] == '=') {
Expand Down Expand Up @@ -455,6 +480,12 @@ func (p *Parser) parseCustomProperty() GrammarType {
} else if tt == LeftParenthesisToken || tt == LeftBraceToken || tt == LeftBracketToken || tt == FunctionToken {
p.level++
} else if tt == RightParenthesisToken || tt == RightBraceToken || tt == RightBracketToken {
if p.level == 0 {
// TODO: buggy
p.pushBuf(tt, data)
p.err, p.errPos = "CSS parse error: unexpected ending in custom property", p.l.r.Offset()
return ErrorGrammar
}
p.level--
}
val = append(val, data...)
Expand Down
1 change: 1 addition & 0 deletions css/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestParse(t *testing.T) {
{false, "[class*=\"column\"]+[class*=\"column\"]:last-child{a:b;}", "[class*=\"column\"]+[class*=\"column\"]:last-child{a:b;}"},
{false, "@media { @viewport }", "@media{@viewport;}"},
{false, "table { @unknown }", "table{@unknown;}"},
{false, "a{@media{width:70%;} b{width:60%;}}", "a{@media{ERROR(width:70%;})ERROR(b{width:60%;})}"},

// early endings
{false, "selector{", "selector{"},
Expand Down

0 comments on commit f4d0fde

Please sign in to comment.