Skip to content

Commit

Permalink
css: fix url token backtracking with leading space
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Oct 17, 2023
1 parent 30a43ec commit d6973b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/css_lexer/css_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,12 +763,25 @@ func (lexer *lexer) consumeIdentLike() T {
if len(name) == 3 {
u, r, l := name[0], name[1], name[2]
if (u == 'u' || u == 'U') && (r == 'r' || r == 'R') && (l == 'l' || l == 'L') {
// Save state
approximateNewlineCount := lexer.approximateNewlineCount
codePoint := lexer.codePoint
tokenRangeLen := lexer.Token.Range.Len
current := lexer.current

// Check to see if this is a URL token instead of a function
for isWhitespace(lexer.codePoint) {
lexer.step()
}
if lexer.codePoint != '"' && lexer.codePoint != '\'' {
return lexer.consumeURL(matchingLoc)
}

// Restore state (i.e. backtrack)
lexer.approximateNewlineCount = approximateNewlineCount
lexer.codePoint = codePoint
lexer.Token.Range.Len = tokenRangeLen
lexer.current = current
}
}
return TFunction
Expand Down
21 changes: 21 additions & 0 deletions internal/css_parser/css_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,27 @@ func TestNumber(t *testing.T) {
}
}

func TestURL(t *testing.T) {
expectPrinted(t, "a { background: url(foo.png) }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url('foo.png') }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\"foo.png\" ) }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\"foo.png\"\t) }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\"foo.png\"\r) }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\"foo.png\"\n) }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\"foo.png\"\f) }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\"foo.png\"\r\n) }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url( \"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\t\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\r\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\n\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\f\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\r\n\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url( \"foo.png\" ) }", "a {\n background: url(foo.png);\n}\n", "")
expectPrinted(t, "a { background: url(\"foo.png\" extra-stuff) }", "a {\n background: url(\"foo.png\" extra-stuff);\n}\n", "")
expectPrinted(t, "a { background: url( \"foo.png\" extra-stuff ) }", "a {\n background: url(\"foo.png\" extra-stuff);\n}\n", "")
}

func TestHexColor(t *testing.T) {
// "#RGBA"

Expand Down

0 comments on commit d6973b9

Please sign in to comment.