Skip to content

Commit d6973b9

Browse files
committedOct 17, 2023
css: fix url token backtracking with leading space
1 parent 30a43ec commit d6973b9

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎internal/css_lexer/css_lexer.go

+13
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,25 @@ func (lexer *lexer) consumeIdentLike() T {
763763
if len(name) == 3 {
764764
u, r, l := name[0], name[1], name[2]
765765
if (u == 'u' || u == 'U') && (r == 'r' || r == 'R') && (l == 'l' || l == 'L') {
766+
// Save state
767+
approximateNewlineCount := lexer.approximateNewlineCount
768+
codePoint := lexer.codePoint
769+
tokenRangeLen := lexer.Token.Range.Len
770+
current := lexer.current
771+
772+
// Check to see if this is a URL token instead of a function
766773
for isWhitespace(lexer.codePoint) {
767774
lexer.step()
768775
}
769776
if lexer.codePoint != '"' && lexer.codePoint != '\'' {
770777
return lexer.consumeURL(matchingLoc)
771778
}
779+
780+
// Restore state (i.e. backtrack)
781+
lexer.approximateNewlineCount = approximateNewlineCount
782+
lexer.codePoint = codePoint
783+
lexer.Token.Range.Len = tokenRangeLen
784+
lexer.current = current
772785
}
773786
}
774787
return TFunction

‎internal/css_parser/css_parser_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,27 @@ func TestNumber(t *testing.T) {
375375
}
376376
}
377377

378+
func TestURL(t *testing.T) {
379+
expectPrinted(t, "a { background: url(foo.png) }", "a {\n background: url(foo.png);\n}\n", "")
380+
expectPrinted(t, "a { background: url('foo.png') }", "a {\n background: url(foo.png);\n}\n", "")
381+
expectPrinted(t, "a { background: url(\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
382+
expectPrinted(t, "a { background: url(\"foo.png\" ) }", "a {\n background: url(foo.png);\n}\n", "")
383+
expectPrinted(t, "a { background: url(\"foo.png\"\t) }", "a {\n background: url(foo.png);\n}\n", "")
384+
expectPrinted(t, "a { background: url(\"foo.png\"\r) }", "a {\n background: url(foo.png);\n}\n", "")
385+
expectPrinted(t, "a { background: url(\"foo.png\"\n) }", "a {\n background: url(foo.png);\n}\n", "")
386+
expectPrinted(t, "a { background: url(\"foo.png\"\f) }", "a {\n background: url(foo.png);\n}\n", "")
387+
expectPrinted(t, "a { background: url(\"foo.png\"\r\n) }", "a {\n background: url(foo.png);\n}\n", "")
388+
expectPrinted(t, "a { background: url( \"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
389+
expectPrinted(t, "a { background: url(\t\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
390+
expectPrinted(t, "a { background: url(\r\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
391+
expectPrinted(t, "a { background: url(\n\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
392+
expectPrinted(t, "a { background: url(\f\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
393+
expectPrinted(t, "a { background: url(\r\n\"foo.png\") }", "a {\n background: url(foo.png);\n}\n", "")
394+
expectPrinted(t, "a { background: url( \"foo.png\" ) }", "a {\n background: url(foo.png);\n}\n", "")
395+
expectPrinted(t, "a { background: url(\"foo.png\" extra-stuff) }", "a {\n background: url(\"foo.png\" extra-stuff);\n}\n", "")
396+
expectPrinted(t, "a { background: url( \"foo.png\" extra-stuff ) }", "a {\n background: url(\"foo.png\" extra-stuff);\n}\n", "")
397+
}
398+
378399
func TestHexColor(t *testing.T) {
379400
// "#RGBA"
380401

0 commit comments

Comments
 (0)