Skip to content

Commit 6986d62

Browse files
committed
CSS: fix parsing custom identifier tokens, such as --0
1 parent 6d3ca97 commit 6986d62

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

css/lex.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ func (l *Lexer) Next() (TokenType, []byte) {
186186
return t, l.r.Shift()
187187
}
188188
case '-':
189-
if t := l.consumeNumeric(); t != ErrorToken {
190-
return t, l.r.Shift()
191-
} else if t := l.consumeIdentlike(); t != ErrorToken {
192-
return t, l.r.Shift()
193-
} else if l.consumeCDCToken() {
189+
if l.consumeCDCToken() {
194190
return CDCToken, l.r.Shift()
195191
} else if l.consumeCustomVariableToken() {
196192
return CustomPropertyNameToken, l.r.Shift()
193+
} else if t := l.consumeIdentlike(); t != ErrorToken {
194+
return t, l.r.Shift()
195+
} else if t := l.consumeNumeric(); t != ErrorToken {
196+
return t, l.r.Shift()
197197
}
198198
case '@':
199199
if l.consumeAtKeywordToken() {
@@ -352,17 +352,24 @@ func (l *Lexer) consumeEscape() bool {
352352

353353
func (l *Lexer) consumeIdentToken() bool {
354354
mark := l.r.Pos()
355+
custom := false
355356
if l.r.Peek(0) == '-' {
356357
l.r.Move(1)
358+
if l.r.Peek(0) == '-' {
359+
l.r.Move(1)
360+
custom = true
361+
}
357362
}
358-
c := l.r.Peek(0)
359-
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c >= 0x80) {
360-
if c != '\\' || !l.consumeEscape() {
361-
l.r.Rewind(mark)
362-
return false
363+
if !custom {
364+
c := l.r.Peek(0)
365+
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c >= 0x80) {
366+
if c != '\\' || !l.consumeEscape() {
367+
l.r.Rewind(mark)
368+
return false
369+
}
370+
} else {
371+
l.r.Move(1)
363372
}
364-
} else {
365-
l.r.Move(1)
366373
}
367374
for {
368375
c := l.r.Peek(0)
@@ -380,13 +387,9 @@ func (l *Lexer) consumeIdentToken() bool {
380387
// support custom variables, https://www.w3.org/TR/css-variables-1/
381388
func (l *Lexer) consumeCustomVariableToken() bool {
382389
// expect to be on a '-'
383-
l.r.Move(1)
384-
if l.r.Peek(0) != '-' {
385-
l.r.Move(-1)
390+
if l.r.Peek(1) != '-' {
386391
return false
387-
}
388-
if !l.consumeIdentToken() {
389-
l.r.Move(-1)
392+
} else if !l.consumeIdentToken() {
390393
return false
391394
}
392395
return true

css/lex_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func TestTokens(t *testing.T) {
3737
{"U+1234", TTs{UnicodeRangeToken}, []string{"U+1234"}},
3838
{"5.2 .4 4e-22", TTs{NumberToken, NumberToken, NumberToken}, []string{"5.2", ".4", "4e-22"}},
3939
{"--custom-variable", TTs{CustomPropertyNameToken}, []string{"--custom-variable"}},
40+
{"--0", TTs{CustomPropertyNameToken}, []string{"--0"}},
4041

4142
// unexpected ending
4243
{"ident", TTs{IdentToken}, []string{"ident"}},

0 commit comments

Comments
 (0)