Skip to content

Commit

Permalink
fix #3426: improve invalid url() token parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Oct 17, 2023
1 parent d6973b9 commit 47fc80b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
31 changes: 25 additions & 6 deletions internal/css_parser/css_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,12 +974,31 @@ func (p *parser) parseURLOrString() (string, logger.Range, bool) {
case css_lexer.TFunction:
if strings.EqualFold(p.decoded(), "url") {
matchingLoc := logger.Loc{Start: p.current().Range.End() - 1}
p.advance()
t = p.current()
text := p.decoded()
if p.expect(css_lexer.TString) {
p.expectWithMatchingLoc(css_lexer.TCloseParen, matchingLoc)
return text, t.Range, true
i := p.index + 1

// Skip over whitespace
for p.at(i).Kind == css_lexer.TWhitespace {
i++
}

// Consume a string
if p.at(i).Kind == css_lexer.TString {
stringIndex := i
i++

// Skip over whitespace
for p.at(i).Kind == css_lexer.TWhitespace {
i++
}

// Consume a closing parenthesis
if close := p.at(i).Kind; close == css_lexer.TCloseParen || close == css_lexer.TEndOfFile {
t := p.at(stringIndex)
text := t.DecodedText(p.source.Contents)
p.index = i
p.expectWithMatchingLoc(css_lexer.TCloseParen, matchingLoc)
return text, t.Range, true
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion internal/css_parser/css_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,15 +1302,19 @@ func TestAtImport(t *testing.T) {
expectPrinted(t, "@import url();", "@import \"\";\n", "")
expectPrinted(t, "@import url(foo.css);", "@import \"foo.css\";\n", "")
expectPrinted(t, "@import url(foo.css) ;", "@import \"foo.css\";\n", "")
expectPrinted(t, "@import url( foo.css );", "@import \"foo.css\";\n", "")
expectPrinted(t, "@import url(\"foo.css\");", "@import \"foo.css\";\n", "")
expectPrinted(t, "@import url(\"foo.css\") ;", "@import \"foo.css\";\n", "")
expectPrinted(t, "@import url( \"foo.css\" );", "@import \"foo.css\";\n", "")
expectPrinted(t, "@import url(\"foo.css\") print;", "@import \"foo.css\" print;\n", "")
expectPrinted(t, "@import url(\"foo.css\") screen and (orientation:landscape);", "@import \"foo.css\" screen and (orientation:landscape);\n", "")

expectPrinted(t, "@import;", "@import;\n", "<stdin>: WARNING: Expected URL token but found \";\"\n")
expectPrinted(t, "@import ;", "@import;\n", "<stdin>: WARNING: Expected URL token but found \";\"\n")
expectPrinted(t, "@import \"foo.css\"", "@import \"foo.css\";\n", "<stdin>: WARNING: Expected \";\" but found end of file\n")
expectPrinted(t, "@import url(\"foo.css\";", "@import \"foo.css\";\n", "<stdin>: WARNING: Expected \")\" to go with \"(\"\n<stdin>: NOTE: The unbalanced \"(\" is here:\n")
expectPrinted(t, "@import url(\"foo.css\" extra-stuff);", "@import url(\"foo.css\" extra-stuff);\n", "<stdin>: WARNING: Expected URL token but found \"url(\"\n")
expectPrinted(t, "@import url(\"foo.css\";", "@import url(\"foo.css\";);\n",
"<stdin>: WARNING: Expected URL token but found \"url(\"\n<stdin>: WARNING: Expected \")\" to go with \"(\"\n<stdin>: NOTE: The unbalanced \"(\" is here:\n")
expectPrinted(t, "@import noturl(\"foo.css\");", "@import noturl(\"foo.css\");\n", "<stdin>: WARNING: Expected URL token but found \"noturl(\"\n")
expectPrinted(t, "@import url(foo.css", "@import \"foo.css\";\n", `<stdin>: WARNING: Expected ")" to end URL token
<stdin>: NOTE: The unbalanced "(" is here:
Expand Down

0 comments on commit 47fc80b

Please sign in to comment.