| 1 |
// Package css is a CSS3 lexer and parser following the specifications at http://www.w3.org/TR/css-syntax-3/. |
| 2 |
package css |
| 3 |
|
| 4 |
// TODO: \uFFFD replacement character for NULL bytes in strings for example, or atleast don't end the string early |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"io" |
| 9 |
"strconv" |
| 10 |
|
| 11 |
"github.com/tdewolff/parse/v2" |
| 12 |
) |
| 13 |
|
| 14 |
// TokenType determines the type of token, eg. a number or a semicolon. |
| 15 |
type TokenType uint32 |
| 16 |
|
| 17 |
// TokenType values. |
| 18 |
const ( |
| 19 |
ErrorToken TokenType = iota // extra token when errors occur |
| 20 |
IdentToken |
| 21 |
FunctionToken // rgb( rgba( ... |
| 22 |
AtKeywordToken // @abc |
| 23 |
HashToken // #abc |
| 24 |
StringToken |
| 25 |
BadStringToken |
| 26 |
URLToken |
| 27 |
BadURLToken |
| 28 |
DelimToken // any unmatched character |
| 29 |
NumberToken // 5 |
| 30 |
PercentageToken // 5% |
| 31 |
DimensionToken // 5em |
| 32 |
UnicodeRangeToken // U+554A |
| 33 |
IncludeMatchToken // ~= |
| 34 |
DashMatchToken // |= |
| 35 |
PrefixMatchToken // ^= |
| 36 |
SuffixMatchToken // $= |
| 37 |
SubstringMatchToken // *= |
| 38 |
ColumnToken // || |
| 39 |
WhitespaceToken // space \t \r \n \f |
| 40 |
CDOToken // <!-- |
| 41 |
CDCToken // --> |
| 42 |
ColonToken // : |
| 43 |
SemicolonToken // ; |
| 44 |
CommaToken // , |
| 45 |
LeftBracketToken // [ |
| 46 |
RightBracketToken // ] |
| 47 |
LeftParenthesisToken // ( |
| 48 |
RightParenthesisToken // ) |
| 49 |
LeftBraceToken // { |
| 50 |
RightBraceToken // } |
| 51 |
CommentToken // extra token for comments |
| 52 |
EmptyToken |
| 53 |
CustomPropertyNameToken |
| 54 |
CustomPropertyValueToken |
| 55 |
) |
| 56 |
|
| 57 |
// String returns the string representation of a TokenType. |
| 58 |
func (tt TokenType) String() string { |
| 59 |
switch tt { |
| 60 |
case ErrorToken: |
| 61 |
return "Error" |
| 62 |
case IdentToken: |
| 63 |
return "Ident" |
| 64 |
case FunctionToken: |
| 65 |
return "Function" |
| 66 |
case AtKeywordToken: |
| 67 |
return "AtKeyword" |
| 68 |
case HashToken: |
| 69 |
return "Hash" |
| 70 |
case StringToken: |
| 71 |
return "String" |
| 72 |
case BadStringToken: |
| 73 |
return "BadString" |
| 74 |
case URLToken: |
| 75 |
return "URL" |
| 76 |
case BadURLToken: |
| 77 |
return "BadURL" |
| 78 |
case DelimToken: |
| 79 |
return "Delim" |
| 80 |
case NumberToken: |
| 81 |
return "Number" |
| 82 |
case PercentageToken: |
| 83 |
return "Percentage" |
| 84 |
case DimensionToken: |
| 85 |
return "Dimension" |
| 86 |
case UnicodeRangeToken: |
| 87 |
return "UnicodeRange" |
| 88 |
case IncludeMatchToken: |
| 89 |
return "IncludeMatch" |
| 90 |
case DashMatchToken: |
| 91 |
return "DashMatch" |
| 92 |
case PrefixMatchToken: |
| 93 |
return "PrefixMatch" |
| 94 |
case SuffixMatchToken: |
| 95 |
return "SuffixMatch" |
| 96 |
case SubstringMatchToken: |
| 97 |
return "SubstringMatch" |
| 98 |
case ColumnToken: |
| 99 |
return "Column" |
| 100 |
case WhitespaceToken: |
| 101 |
return "Whitespace" |
| 102 |
case CDOToken: |
| 103 |
return "CDO" |
| 104 |
case CDCToken: |
| 105 |
return "CDC" |
| 106 |
case ColonToken: |
| 107 |
return "Colon" |
| 108 |
case SemicolonToken: |
| 109 |
return "Semicolon" |
| 110 |
case CommaToken: |
| 111 |
return "Comma" |
| 112 |
case LeftBracketToken: |
| 113 |
return "LeftBracket" |
| 114 |
case RightBracketToken: |
| 115 |
return "RightBracket" |
| 116 |
case LeftParenthesisToken: |
| 117 |
return "LeftParenthesis" |
| 118 |
case RightParenthesisToken: |
| 119 |
return "RightParenthesis" |
| 120 |
case LeftBraceToken: |
| 121 |
return "LeftBrace" |
| 122 |
case RightBraceToken: |
| 123 |
return "RightBrace" |
| 124 |
case CommentToken: |
| 125 |
return "Comment" |
| 126 |
case EmptyToken: |
| 127 |
return "Empty" |
| 128 |
case CustomPropertyNameToken: |
| 129 |
return "CustomPropertyName" |
| 130 |
case CustomPropertyValueToken: |
| 131 |
return "CustomPropertyValue" |
| 132 |
} |
| 133 |
return "Invalid(" + strconv.Itoa(int(tt)) + ")" |
| 134 |
} |
| 135 |
|
| 136 |
//////////////////////////////////////////////////////////////// |
| 137 |
|
| 138 |
// Lexer is the state for the lexer. |
| 139 |
type Lexer struct { |
| 140 |
r *parse.Input |
| 141 |
} |
| 142 |
|
| 143 |
// NewLexer returns a new Lexer for a given io.Reader. |
| 144 |
func NewLexer(r *parse.Input) *Lexer { |
| 145 |
return &Lexer{ |
| 146 |
r: r, |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned. |
| 151 |
func (l *Lexer) Err() error { |
| 152 |
return l.r.Err() |
| 153 |
} |
| 154 |
|
| 155 |
// Next returns the next Token. It returns ErrorToken when an error was encountered. Using Err() one can retrieve the error message. |
| 156 |
func (l *Lexer) Next() (TokenType, []byte) { |
| 157 |
switch l.r.Peek(0) { |
| 158 |
case ' ', '\t', '\n', '\r', '\f': |
| 159 |
l.r.Move(1) |
| 160 |
for l.consumeWhitespace() { |
| 161 |
} |
| 162 |
return WhitespaceToken, l.r.Shift() |
| 163 |
case ':': |
| 164 |
l.r.Move(1) |
| 165 |
return ColonToken, l.r.Shift() |
| 166 |
case ';': |
| 167 |
l.r.Move(1) |
| 168 |
return SemicolonToken, l.r.Shift() |
| 169 |
case ',': |
| 170 |
l.r.Move(1) |
| 171 |
return CommaToken, l.r.Shift() |
| 172 |
case '(', ')', '[', ']', '{', '}': |
| 173 |
if t := l.consumeBracket(); t != ErrorToken { |
| 174 |
return t, l.r.Shift() |
| 175 |
} |
| 176 |
case '#': |
| 177 |
if l.consumeHashToken() { |
| 178 |
return HashToken, l.r.Shift() |
| 179 |
} |
| 180 |
case '"', '\'': |
| 181 |
if t := l.consumeString(); t != ErrorToken { |
| 182 |
return t, l.r.Shift() |
| 183 |
} |
| 184 |
case '.', '+': |
| 185 |
if t := l.consumeNumeric(); t != ErrorToken { |
| 186 |
return t, l.r.Shift() |
| 187 |
} |
| 188 |
case '-': |
| 189 |
if l.consumeCDCToken() { |
| 190 |
return CDCToken, l.r.Shift() |
| 191 |
} else if l.consumeCustomVariableToken() { |
| 192 |
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() |
| 197 |
} |
| 198 |
case '@': |
| 199 |
if l.consumeAtKeywordToken() { |
| 200 |
return AtKeywordToken, l.r.Shift() |
| 201 |
} |
| 202 |
case '$', '*', '^', '~': |
| 203 |
if t := l.consumeMatch(); t != ErrorToken { |
| 204 |
return t, l.r.Shift() |
| 205 |
} |
| 206 |
case '/': |
| 207 |
if l.consumeComment() { |
| 208 |
return CommentToken, l.r.Shift() |
| 209 |
} |
| 210 |
case '<': |
| 211 |
if l.consumeCDOToken() { |
| 212 |
return CDOToken, l.r.Shift() |
| 213 |
} |
| 214 |
case '\\': |
| 215 |
if t := l.consumeIdentlike(); t != ErrorToken { |
| 216 |
return t, l.r.Shift() |
| 217 |
} |
| 218 |
case 'u', 'U': |
| 219 |
if l.consumeUnicodeRangeToken() { |
| 220 |
return UnicodeRangeToken, l.r.Shift() |
| 221 |
} else if t := l.consumeIdentlike(); t != ErrorToken { |
| 222 |
return t, l.r.Shift() |
| 223 |
} |
| 224 |
case '|': |
| 225 |
if t := l.consumeMatch(); t != ErrorToken { |
| 226 |
return t, l.r.Shift() |
| 227 |
} else if l.consumeColumnToken() { |
| 228 |
return ColumnToken, l.r.Shift() |
| 229 |
} |
| 230 |
case 0: |
| 231 |
if l.r.Err() != nil { |
| 232 |
return ErrorToken, nil |
| 233 |
} |
| 234 |
default: |
| 235 |
if t := l.consumeNumeric(); t != ErrorToken { |
| 236 |
return t, l.r.Shift() |
| 237 |
} else if t := l.consumeIdentlike(); t != ErrorToken { |
| 238 |
return t, l.r.Shift() |
| 239 |
} |
| 240 |
} |
| 241 |
// can't be rune because consumeIdentlike consumes that as an identifier |
| 242 |
l.r.Move(1) |
| 243 |
return DelimToken, l.r.Shift() |
| 244 |
} |
| 245 |
|
| 246 |
//////////////////////////////////////////////////////////////// |
| 247 |
|
| 248 |
/* |
| 249 |
The following functions follow the railroad diagrams in http://www.w3.org/TR/css3-syntax/ |
| 250 |
*/ |
| 251 |
|
| 252 |
func (l *Lexer) consumeByte(c byte) bool { |
| 253 |
if l.r.Peek(0) == c { |
| 254 |
l.r.Move(1) |
| 255 |
return true |
| 256 |
} |
| 257 |
return false |
| 258 |
} |
| 259 |
|
| 260 |
func (l *Lexer) consumeComment() bool { |
| 261 |
if l.r.Peek(0) != '/' || l.r.Peek(1) != '*' { |
| 262 |
return false |
| 263 |
} |
| 264 |
l.r.Move(2) |
| 265 |
for { |
| 266 |
c := l.r.Peek(0) |
| 267 |
if c == 0 && l.r.Err() != nil { |
| 268 |
break |
| 269 |
} else if c == '*' && l.r.Peek(1) == '/' { |
| 270 |
l.r.Move(2) |
| 271 |
return true |
| 272 |
} |
| 273 |
l.r.Move(1) |
| 274 |
} |
| 275 |
return true |
| 276 |
} |
| 277 |
|
| 278 |
func (l *Lexer) consumeNewline() bool { |
| 279 |
c := l.r.Peek(0) |
| 280 |
if c == '\n' || c == '\f' { |
| 281 |
l.r.Move(1) |
| 282 |
return true |
| 283 |
} else if c == '\r' { |
| 284 |
if l.r.Peek(1) == '\n' { |
| 285 |
l.r.Move(2) |
| 286 |
} else { |
| 287 |
l.r.Move(1) |
| 288 |
} |
| 289 |
return true |
| 290 |
} |
| 291 |
return false |
| 292 |
} |
| 293 |
|
| 294 |
func (l *Lexer) consumeWhitespace() bool { |
| 295 |
c := l.r.Peek(0) |
| 296 |
if c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' { |
| 297 |
l.r.Move(1) |
| 298 |
return true |
| 299 |
} |
| 300 |
return false |
| 301 |
} |
| 302 |
|
| 303 |
func (l *Lexer) consumeDigit() bool { |
| 304 |
c := l.r.Peek(0) |
| 305 |
if c >= '0' && c <= '9' { |
| 306 |
l.r.Move(1) |
| 307 |
return true |
| 308 |
} |
| 309 |
return false |
| 310 |
} |
| 311 |
|
| 312 |
func (l *Lexer) consumeHexDigit() bool { |
| 313 |
c := l.r.Peek(0) |
| 314 |
if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') { |
| 315 |
l.r.Move(1) |
| 316 |
return true |
| 317 |
} |
| 318 |
return false |
| 319 |
} |
| 320 |
|
| 321 |
func (l *Lexer) consumeEscape() bool { |
| 322 |
if l.r.Peek(0) != '\\' { |
| 323 |
return false |
| 324 |
} |
| 325 |
mark := l.r.Pos() |
| 326 |
l.r.Move(1) |
| 327 |
if l.consumeNewline() { |
| 328 |
l.r.Rewind(mark) |
| 329 |
return false |
| 330 |
} else if l.consumeHexDigit() { |
| 331 |
for k := 1; k < 6; k++ { |
| 332 |
if !l.consumeHexDigit() { |
| 333 |
break |
| 334 |
} |
| 335 |
} |
| 336 |
l.consumeWhitespace() |
| 337 |
return true |
| 338 |
} else { |
| 339 |
c := l.r.Peek(0) |
| 340 |
if c >= 0xC0 { |
| 341 |
_, n := l.r.PeekRune(0) |
| 342 |
l.r.Move(n) |
| 343 |
return true |
| 344 |
} else if c == 0 && l.r.Err() != nil { |
| 345 |
l.r.Rewind(mark) |
| 346 |
return false |
| 347 |
} |
| 348 |
} |
| 349 |
l.r.Move(1) |
| 350 |
return true |
| 351 |
} |
| 352 |
|
| 353 |
func (l *Lexer) consumeIdentToken() bool { |
| 354 |
mark := l.r.Pos() |
| 355 |
custom := false |
| 356 |
if l.r.Peek(0) == '-' { |
| 357 |
l.r.Move(1) |
| 358 |
if l.r.Peek(0) == '-' { |
| 359 |
l.r.Move(1) |
| 360 |
custom = true |
| 361 |
} |
| 362 |
} |
| 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) |
| 372 |
} |
| 373 |
} |
| 374 |
for { |
| 375 |
c := l.r.Peek(0) |
| 376 |
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c >= 0x80) { |
| 377 |
if c != '\\' || !l.consumeEscape() { |
| 378 |
break |
| 379 |
} |
| 380 |
} else { |
| 381 |
l.r.Move(1) |
| 382 |
} |
| 383 |
} |
| 384 |
return true |
| 385 |
} |
| 386 |
|
| 387 |
// support custom variables, https://www.w3.org/TR/css-variables-1/ |
| 388 |
func (l *Lexer) consumeCustomVariableToken() bool { |
| 389 |
// expect to be on a '-' |
| 390 |
if l.r.Peek(1) != '-' { |
| 391 |
return false |
| 392 |
} else if !l.consumeIdentToken() { |
| 393 |
return false |
| 394 |
} |
| 395 |
return true |
| 396 |
} |
| 397 |
|
| 398 |
func (l *Lexer) consumeAtKeywordToken() bool { |
| 399 |
// expect to be on an '@' |
| 400 |
l.r.Move(1) |
| 401 |
if !l.consumeIdentToken() { |
| 402 |
l.r.Move(-1) |
| 403 |
return false |
| 404 |
} |
| 405 |
return true |
| 406 |
} |
| 407 |
|
| 408 |
func (l *Lexer) consumeHashToken() bool { |
| 409 |
// expect to be on a '#' |
| 410 |
mark := l.r.Pos() |
| 411 |
l.r.Move(1) |
| 412 |
c := l.r.Peek(0) |
| 413 |
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c >= 0x80) { |
| 414 |
if c != '\\' || !l.consumeEscape() { |
| 415 |
l.r.Rewind(mark) |
| 416 |
return false |
| 417 |
} |
| 418 |
} else { |
| 419 |
l.r.Move(1) |
| 420 |
} |
| 421 |
for { |
| 422 |
c := l.r.Peek(0) |
| 423 |
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c >= 0x80) { |
| 424 |
if c != '\\' || !l.consumeEscape() { |
| 425 |
break |
| 426 |
} |
| 427 |
} else { |
| 428 |
l.r.Move(1) |
| 429 |
} |
| 430 |
} |
| 431 |
return true |
| 432 |
} |
| 433 |
|
| 434 |
func (l *Lexer) consumeNumberToken() bool { |
| 435 |
mark := l.r.Pos() |
| 436 |
c := l.r.Peek(0) |
| 437 |
if c == '+' || c == '-' { |
| 438 |
l.r.Move(1) |
| 439 |
} |
| 440 |
firstDigit := l.consumeDigit() |
| 441 |
if firstDigit { |
| 442 |
for l.consumeDigit() { |
| 443 |
} |
| 444 |
} |
| 445 |
if l.r.Peek(0) == '.' { |
| 446 |
l.r.Move(1) |
| 447 |
if l.consumeDigit() { |
| 448 |
for l.consumeDigit() { |
| 449 |
} |
| 450 |
} else if firstDigit { |
| 451 |
// . could belong to the next token |
| 452 |
l.r.Move(-1) |
| 453 |
return true |
| 454 |
} else { |
| 455 |
l.r.Rewind(mark) |
| 456 |
return false |
| 457 |
} |
| 458 |
} else if !firstDigit { |
| 459 |
l.r.Rewind(mark) |
| 460 |
return false |
| 461 |
} |
| 462 |
mark = l.r.Pos() |
| 463 |
c = l.r.Peek(0) |
| 464 |
if c == 'e' || c == 'E' { |
| 465 |
l.r.Move(1) |
| 466 |
c = l.r.Peek(0) |
| 467 |
if c == '+' || c == '-' { |
| 468 |
l.r.Move(1) |
| 469 |
} |
| 470 |
if !l.consumeDigit() { |
| 471 |
// e could belong to next token |
| 472 |
l.r.Rewind(mark) |
| 473 |
return true |
| 474 |
} |
| 475 |
for l.consumeDigit() { |
| 476 |
} |
| 477 |
} |
| 478 |
return true |
| 479 |
} |
| 480 |
|
| 481 |
func (l *Lexer) consumeUnicodeRangeToken() bool { |
| 482 |
c := l.r.Peek(0) |
| 483 |
if (c != 'u' && c != 'U') || l.r.Peek(1) != '+' { |
| 484 |
return false |
| 485 |
} |
| 486 |
mark := l.r.Pos() |
| 487 |
l.r.Move(2) |
| 488 |
|
| 489 |
// consume up to 6 hexDigits |
| 490 |
k := 0 |
| 491 |
for l.consumeHexDigit() { |
| 492 |
k++ |
| 493 |
} |
| 494 |
|
| 495 |
// either a minus or a question mark or the end is expected |
| 496 |
if l.consumeByte('-') { |
| 497 |
if k == 0 || 6 < k { |
| 498 |
l.r.Rewind(mark) |
| 499 |
return false |
| 500 |
} |
| 501 |
|
| 502 |
// consume another up to 6 hexDigits |
| 503 |
if l.consumeHexDigit() { |
| 504 |
k = 1 |
| 505 |
for l.consumeHexDigit() { |
| 506 |
k++ |
| 507 |
} |
| 508 |
} else { |
| 509 |
l.r.Rewind(mark) |
| 510 |
return false |
| 511 |
} |
| 512 |
} else if l.consumeByte('?') { |
| 513 |
// could be filled up to 6 characters with question marks or else regular hexDigits |
| 514 |
k++ |
| 515 |
for l.consumeByte('?') { |
| 516 |
k++ |
| 517 |
} |
| 518 |
} |
| 519 |
if k == 0 || 6 < k { |
| 520 |
l.r.Rewind(mark) |
| 521 |
return false |
| 522 |
} |
| 523 |
return true |
| 524 |
} |
| 525 |
|
| 526 |
func (l *Lexer) consumeColumnToken() bool { |
| 527 |
if l.r.Peek(0) == '|' && l.r.Peek(1) == '|' { |
| 528 |
l.r.Move(2) |
| 529 |
return true |
| 530 |
} |
| 531 |
return false |
| 532 |
} |
| 533 |
|
| 534 |
func (l *Lexer) consumeCDOToken() bool { |
| 535 |
if l.r.Peek(0) == '<' && l.r.Peek(1) == '!' && l.r.Peek(2) == '-' && l.r.Peek(3) == '-' { |
| 536 |
l.r.Move(4) |
| 537 |
return true |
| 538 |
} |
| 539 |
return false |
| 540 |
} |
| 541 |
|
| 542 |
func (l *Lexer) consumeCDCToken() bool { |
| 543 |
if l.r.Peek(0) == '-' && l.r.Peek(1) == '-' && l.r.Peek(2) == '>' { |
| 544 |
l.r.Move(3) |
| 545 |
return true |
| 546 |
} |
| 547 |
return false |
| 548 |
} |
| 549 |
|
| 550 |
//////////////////////////////////////////////////////////////// |
| 551 |
|
| 552 |
// consumeMatch consumes any MatchToken. |
| 553 |
func (l *Lexer) consumeMatch() TokenType { |
| 554 |
if l.r.Peek(1) == '=' { |
| 555 |
switch l.r.Peek(0) { |
| 556 |
case '~': |
| 557 |
l.r.Move(2) |
| 558 |
return IncludeMatchToken |
| 559 |
case '|': |
| 560 |
l.r.Move(2) |
| 561 |
return DashMatchToken |
| 562 |
case '^': |
| 563 |
l.r.Move(2) |
| 564 |
return PrefixMatchToken |
| 565 |
case '$': |
| 566 |
l.r.Move(2) |
| 567 |
return SuffixMatchToken |
| 568 |
case '*': |
| 569 |
l.r.Move(2) |
| 570 |
return SubstringMatchToken |
| 571 |
} |
| 572 |
} |
| 573 |
return ErrorToken |
| 574 |
} |
| 575 |
|
| 576 |
// consumeBracket consumes any bracket token. |
| 577 |
func (l *Lexer) consumeBracket() TokenType { |
| 578 |
switch l.r.Peek(0) { |
| 579 |
case '(': |
| 580 |
l.r.Move(1) |
| 581 |
return LeftParenthesisToken |
| 582 |
case ')': |
| 583 |
l.r.Move(1) |
| 584 |
return RightParenthesisToken |
| 585 |
case '[': |
| 586 |
l.r.Move(1) |
| 587 |
return LeftBracketToken |
| 588 |
case ']': |
| 589 |
l.r.Move(1) |
| 590 |
return RightBracketToken |
| 591 |
case '{': |
| 592 |
l.r.Move(1) |
| 593 |
return LeftBraceToken |
| 594 |
case '}': |
| 595 |
l.r.Move(1) |
| 596 |
return RightBraceToken |
| 597 |
} |
| 598 |
return ErrorToken |
| 599 |
} |
| 600 |
|
| 601 |
// consumeNumeric consumes NumberToken, PercentageToken or DimensionToken. |
| 602 |
func (l *Lexer) consumeNumeric() TokenType { |
| 603 |
if l.consumeNumberToken() { |
| 604 |
if l.consumeByte('%') { |
| 605 |
return PercentageToken |
| 606 |
} else if l.consumeIdentToken() { |
| 607 |
return DimensionToken |
| 608 |
} |
| 609 |
return NumberToken |
| 610 |
} |
| 611 |
return ErrorToken |
| 612 |
} |
| 613 |
|
| 614 |
// consumeString consumes a string and may return BadStringToken when a newline is encountered. |
| 615 |
func (l *Lexer) consumeString() TokenType { |
| 616 |
// assume to be on " or ' |
| 617 |
delim := l.r.Peek(0) |
| 618 |
l.r.Move(1) |
| 619 |
for { |
| 620 |
c := l.r.Peek(0) |
| 621 |
if c == 0 && l.r.Err() != nil { |
| 622 |
break |
| 623 |
} else if c == '\n' || c == '\r' || c == '\f' { |
| 624 |
l.r.Move(1) |
| 625 |
return BadStringToken |
| 626 |
} else if c == delim { |
| 627 |
l.r.Move(1) |
| 628 |
break |
| 629 |
} else if c == '\\' { |
| 630 |
if !l.consumeEscape() { |
| 631 |
// either newline or EOF after backslash |
| 632 |
l.r.Move(1) |
| 633 |
l.consumeNewline() |
| 634 |
} |
| 635 |
} else { |
| 636 |
l.r.Move(1) |
| 637 |
} |
| 638 |
} |
| 639 |
return StringToken |
| 640 |
} |
| 641 |
|
| 642 |
func (l *Lexer) consumeUnquotedURL() bool { |
| 643 |
for { |
| 644 |
c := l.r.Peek(0) |
| 645 |
if c == 0 && l.r.Err() != nil || c == ')' { |
| 646 |
break |
| 647 |
} else if c == '"' || c == '\'' || c == '(' || c == '\\' || c == ' ' || c <= 0x1F || c == 0x7F { |
| 648 |
if c != '\\' || !l.consumeEscape() { |
| 649 |
return false |
| 650 |
} |
| 651 |
} else { |
| 652 |
l.r.Move(1) |
| 653 |
} |
| 654 |
} |
| 655 |
return true |
| 656 |
} |
| 657 |
|
| 658 |
// consumeRemnantsBadUrl consumes bytes of a BadUrlToken so that normal tokenization may continue. |
| 659 |
func (l *Lexer) consumeRemnantsBadURL() { |
| 660 |
for { |
| 661 |
if l.consumeByte(')') || l.r.Err() != nil { |
| 662 |
break |
| 663 |
} else if !l.consumeEscape() { |
| 664 |
l.r.Move(1) |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
// consumeIdentlike consumes IdentToken, FunctionToken or UrlToken. |
| 670 |
func (l *Lexer) consumeIdentlike() TokenType { |
| 671 |
if l.consumeIdentToken() { |
| 672 |
if l.r.Peek(0) != '(' { |
| 673 |
return IdentToken |
| 674 |
} else if !parse.EqualFold(bytes.Replace(l.r.Lexeme(), []byte{'\\'}, nil, -1), []byte{'u', 'r', 'l'}) { |
| 675 |
l.r.Move(1) |
| 676 |
return FunctionToken |
| 677 |
} |
| 678 |
l.r.Move(1) |
| 679 |
|
| 680 |
// consume url |
| 681 |
for l.consumeWhitespace() { |
| 682 |
} |
| 683 |
if c := l.r.Peek(0); c == '"' || c == '\'' { |
| 684 |
if l.consumeString() == BadStringToken { |
| 685 |
l.consumeRemnantsBadURL() |
| 686 |
return BadURLToken |
| 687 |
} |
| 688 |
} else if !l.consumeUnquotedURL() && !l.consumeWhitespace() { // if unquoted URL fails due to encountering whitespace, continue |
| 689 |
l.consumeRemnantsBadURL() |
| 690 |
return BadURLToken |
| 691 |
} |
| 692 |
for l.consumeWhitespace() { |
| 693 |
} |
| 694 |
if !l.consumeByte(')') && l.r.Err() != io.EOF { |
| 695 |
l.consumeRemnantsBadURL() |
| 696 |
return BadURLToken |
| 697 |
} |
| 698 |
return URLToken |
| 699 |
} |
| 700 |
return ErrorToken |
| 701 |
} |