Debian Codegraph @

vendor/wikimedia/css-sanitizer/src/Parser/DataSourceTokenizer.php

raw
1 <?php
2 /**
3 * @file
4 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
5 */
6
7 namespace Wikimedia\CSS\Parser;
8
9 use InvalidArgumentException;
10 use UnexpectedValueException;
11 use UtfNormal\Constants;
12 use UtfNormal\Utils;
13 use Wikimedia\CSS\Objects\Token;
14
15 /**
16 * Parse CSS into tokens
17 *
18 * This implements the tokenizer from the CSS Syntax Module Level 3 candidate recommendation.
19 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/
20 */
21 class DataSourceTokenizer implements Tokenizer {
22
23 /** @var DataSource */
24 protected $source;
25
26 /** @var int line in the input */
27 protected $line = 1;
28
29 /** @var int position in the line in the input */
30 protected $pos = 0;
31
32 /** @var string|null|\stdClass The most recently consumed character */
33 protected $currentCharacter = null;
34
35 /** @var string|null The next character to be consumed */
36 protected $nextCharacter = null;
37
38 /** @var array Parse errors. Each error is [ string $tag, int $line, int $pos ] */
39 protected $parseErrors = [];
40
41 /**
42 * @param DataSource $source
43 * @param array $options Configuration options.
44 * (none currently defined)
45 */
46 public function __construct( DataSource $source, array $options = [] ) {
47 $this->source = $source;
48 }
49
50 /**
51 * Read a character from the data source
52 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#input-preprocessing
53 * @return string One UTF-8 character, or empty string on EOF
54 */
55 protected function nextChar() {
56 $char = $this->source->readCharacter();
57
58 // Perform transformations per the spec
59
60 // Any U+0000 or surrogate code point becomes U+FFFD
61 if ( $char === "\0" || ( $char >= "\u{D800}" && $char <= "\u{DFFF}" ) ) {
62 return Constants::UTF8_REPLACEMENT;
63 }
64
65 // Any U+000D, U+000C, or pair of U+000D + U+000A becomes U+000A
66 if ( $char === "\f" ) {
67 // U+000C
68 return "\n";
69 }
70
71 if ( $char === "\r" ) {
72 // Either U+000D + U+000A or a lone U+000D
73 $char2 = $this->source->readCharacter();
74 if ( $char2 !== "\n" ) {
75 $this->source->putBackCharacter( $char2 );
76 }
77 return "\n";
78 }
79
80 return $char;
81 }
82
83 /**
84 * Update the current and next character fields
85 */
86 protected function consumeCharacter() {
87 if ( $this->currentCharacter === "\n" ) {
88 $this->line++;
89 $this->pos = 1;
90 } elseif ( $this->currentCharacter !== DataSource::EOF ) {
91 $this->pos++;
92 }
93
94 $this->currentCharacter = $this->nextChar();
95 $this->nextCharacter = $this->nextChar();
96 $this->source->putBackCharacter( $this->nextCharacter );
97 }
98
99 /**
100 * Reconsume the next character
101 *
102 * In more normal terms, this pushes a character back onto the data source,
103 * so it will be read again for the next call to self::consumeCharacter().
104 */
105 protected function reconsumeCharacter() {
106 // @codeCoverageIgnoreStart
107 if ( !is_string( $this->currentCharacter ) ) {
108 throw new UnexpectedValueException( "[$this->line:$this->pos] Can't reconsume" );
109 }
110 // @codeCoverageIgnoreEnd
111
112 if ( $this->currentCharacter === DataSource::EOF ) {
113 // Huh?
114 return;
115 }
116
117 $this->source->putBackCharacter( $this->currentCharacter );
118 $this->nextCharacter = $this->currentCharacter;
119 $this->currentCharacter = (object)[];
120 $this->pos--;
121 }
122
123 /**
124 * Look ahead at the next three characters
125 * @return string[] Three characters
126 */
127 protected function lookAhead() {
128 $ret = [
129 $this->nextChar(),
130 $this->nextChar(),
131 $this->nextChar(),
132 ];
133 $this->source->putBackCharacter( $ret[2] );
134 $this->source->putBackCharacter( $ret[1] );
135 $this->source->putBackCharacter( $ret[0] );
136
137 return $ret;
138 }
139
140 /** @inheritDoc */
141 public function getParseErrors() {
142 return $this->parseErrors;
143 }
144
145 /** @inheritDoc */
146 public function clearParseErrors() {
147 $this->parseErrors = [];
148 }
149
150 /**
151 * Record a parse error
152 * @param string $tag Error tag
153 * @param array|null $position Report the error as starting at this
154 * position instead of at the current position.
155 * @param array $data Extra data about the error.
156 */
157 protected function parseError( $tag, ?array $position = null, array $data = [] ) {
158 if ( $position ) {
159 if ( isset( $position['position'] ) ) {
160 $position = $position['position'];
161 }
162 if ( count( $position ) !== 2 || !is_int( $position[0] ) || !is_int( $position[1] ) ) {
163 // @codeCoverageIgnoreStart
164 throw new InvalidArgumentException( 'Invalid position' );
165 // @codeCoverageIgnoreEnd
166 }
167 $err = [ $tag, $position[0], $position[1] ];
168 } else {
169 $err = [ $tag, $this->line, $this->pos ];
170 }
171 $this->parseErrors[] = array_merge( $err, $data );
172 }
173
174 /**
175 * Read a token from the data source
176 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#consume-token
177 * @return Token
178 * @suppress PhanPluginDuplicateAdjacentStatement,PhanPluginDuplicateSwitchCaseLooseEquality
179 */
180 public function consumeToken() {
181 // We "consume comments" inline below, see `case '/'`.
182
183 $this->consumeCharacter();
184 $pos = [ 'position' => [ $this->line, $this->pos ] ];
185
186 switch ( (string)$this->currentCharacter ) {
187 case "\n":
188 case "\t":
189 case ' ':
190 // Whitespace token
191 while ( self::isWhitespace( $this->nextCharacter ) ) {
192 $this->consumeCharacter();
193 }
194 return new Token( Token::T_WHITESPACE, $pos );
195
196 case '"':
197 case '\'':
198 // String token
199 return $this->consumeStringToken( $this->currentCharacter, $pos );
200
201 case '#':
202 [ $next, $next2, $next3 ] = $this->lookAhead();
203 if ( self::isNameCharacter( $this->nextCharacter ) ||
204 self::isValidEscape( $next, $next2 )
205 ) {
206 return new Token( Token::T_HASH, $pos + [
207 'typeFlag' => self::wouldStartIdentifier( $next, $next2, $next3 ) ? 'id' : 'unrestricted',
208 'value' => $this->consumeName(),
209 ] );
210 }
211
212 return new Token( Token::T_DELIM, $pos + [ 'value' => $this->currentCharacter ] );
213
214 case '(':
215 return new Token( Token::T_LEFT_PAREN, $pos );
216
217 case ')':
218 return new Token( Token::T_RIGHT_PAREN, $pos );
219
220 case '+':
221 case '.':
222 [ $next, $next2, ] = $this->lookAhead();
223 if ( self::wouldStartNumber( $this->currentCharacter, $next, $next2 ) ) {
224 $this->reconsumeCharacter();
225 return $this->consumeNumericToken( $pos );
226 }
227
228 return new Token( Token::T_DELIM, $pos + [ 'value' => $this->currentCharacter ] );
229
230 case ',':
231 return new Token( Token::T_COMMA, $pos );
232
233 case '-':
234 [ $next, $next2, ] = $this->lookAhead();
235 if ( self::wouldStartNumber( $this->currentCharacter, $next, $next2 ) ) {
236 $this->reconsumeCharacter();
237 return $this->consumeNumericToken( $pos );
238 }
239
240 if ( $next === '-' && $next2 === '>' ) {
241 $this->consumeCharacter();
242 $this->consumeCharacter();
243 return new Token( Token::T_CDC, $pos );
244 }
245
246 if ( self::wouldStartIdentifier( $this->currentCharacter, $next, $next2 ) ) {
247 $this->reconsumeCharacter();
248 return $this->consumeIdentLikeToken( $pos );
249 }
250
251 return new Token( Token::T_DELIM, $pos + [ 'value' => $this->currentCharacter ] );
252
253 case '/':
254 if ( $this->nextCharacter === '*' ) {
255 $this->consumeCharacter();
256 $this->consumeCharacter();
257 while ( $this->currentCharacter !== DataSource::EOF &&
258 // phan doesn't see the side effects of ::consumeCharacter
259 // @phan-suppress-next-line PhanImpossibleValueComparisonInLoop
260 !( $this->currentCharacter === '*' && $this->nextCharacter === '/' )
261 ) {
262 $this->consumeCharacter();
263 }
264 if ( $this->currentCharacter === DataSource::EOF ) {
265 $this->parseError( 'unclosed-comment', $pos );
266 }
267 $this->consumeCharacter();
268 // @phan-suppress-next-line PhanPossiblyInfiniteRecursionSameParams
269 return $this->consumeToken();
270 }
271
272 return new Token( Token::T_DELIM, $pos + [ 'value' => $this->currentCharacter ] );
273
274 case ':':
275 return new Token( Token::T_COLON, $pos );
276
277 case ';':
278 return new Token( Token::T_SEMICOLON, $pos );
279
280 case '<':
281 [ $next, $next2, $next3 ] = $this->lookAhead();
282 if ( $next === '!' && $next2 === '-' && $next3 === '-' ) {
283 $this->consumeCharacter();
284 $this->consumeCharacter();
285 $this->consumeCharacter();
286 return new Token( Token::T_CDO, $pos );
287 }
288
289 return new Token( Token::T_DELIM, $pos + [ 'value' => $this->currentCharacter ] );
290
291 case '@':
292 [ $next, $next2, $next3 ] = $this->lookAhead();
293 if ( self::wouldStartIdentifier( $next, $next2, $next3 ) ) {
294 return new Token( Token::T_AT_KEYWORD, $pos + [ 'value' => $this->consumeName() ] );
295 }
296
297 return new Token( Token::T_DELIM, $pos + [ 'value' => $this->currentCharacter ] );
298
299 case '[':
300 return new Token( Token::T_LEFT_BRACKET, $pos );
301
302 case '\\':
303 if ( self::isValidEscape( $this->currentCharacter, $this->nextCharacter ) ) {
304 $this->reconsumeCharacter();
305 return $this->consumeIdentLikeToken( $pos );
306 }
307
308 $this->parseError( 'bad-escape' );
309 return new Token( Token::T_DELIM, $pos + [ 'value' => $this->currentCharacter ] );
310
311 case ']':
312 return new Token( Token::T_RIGHT_BRACKET, $pos );
313
314 case '{':
315 return new Token( Token::T_LEFT_BRACE, $pos );
316
317 case '}':
318 return new Token( Token::T_RIGHT_BRACE, $pos );
319
320 case '0':
321 case '1':
322 case '2':
323 case '3':
324 case '4':
325 case '5':
326 case '6':
327 case '7':
328 case '8':
329 case '9':
330 $this->reconsumeCharacter();
331 return $this->consumeNumericToken( $pos );
332
333 case DataSource::EOF:
334 return new Token( Token::T_EOF, $pos );
335
336 default:
337 if ( self::isNameStartCharacter( $this->currentCharacter ) ) {
338 $this->reconsumeCharacter();
339 return $this->consumeIdentLikeToken( $pos );
340 }
341
342 return new Token( Token::T_DELIM, $pos + [ 'value' => $this->currentCharacter ] );
343 }
344 }
345
346 /**
347 * Consume a numeric token
348 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#consume-numeric-token
349 * @param array $data Data for the new token (typically contains just 'position')
350 * @return Token
351 */
352 protected function consumeNumericToken( array $data ) {
353 [ $data['representation'], $data['value'], $data['typeFlag'] ] = $this->consumeNumber();
354
355 [ $next, $next2, $next3 ] = $this->lookAhead();
356 if ( self::wouldStartIdentifier( $next, $next2, $next3 ) ) {
357 return new Token( Token::T_DIMENSION, $data + [ 'unit' => $this->consumeName() ] );
358 } elseif ( $this->nextCharacter === '%' ) {
359 $this->consumeCharacter();
360 return new Token( Token::T_PERCENTAGE, $data );
361 } else {
362 return new Token( Token::T_NUMBER, $data );
363 }
364 }
365
366 /**
367 * Consume an ident-like token
368 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#consume-ident-like-token
369 * @param array $data Data for the new token (typically contains just 'position')
370 * @return Token
371 */
372 protected function consumeIdentLikeToken( array $data ) {
373 $name = $this->consumeName();
374
375 if ( $this->nextCharacter === '(' ) {
376 $this->consumeCharacter();
377
378 if ( !strcasecmp( $name, 'url' ) ) {
379 while ( true ) {
380 [ $next, $next2 ] = $this->lookAhead();
381 if ( !self::isWhitespace( $next ) || !self::isWhitespace( $next2 ) ) {
382 break;
383 }
384 $this->consumeCharacter();
385 }
386 if ( $next !== '"' && $next !== '\'' &&
387 !( self::isWhitespace( $next ) && ( $next2 === '"' || $next2 === '\'' ) )
388 ) {
389 return $this->consumeUrlToken( $data );
390 }
391 }
392
393 return new Token( Token::T_FUNCTION, $data + [ 'value' => $name ] );
394 }
395
396 return new Token( Token::T_IDENT, $data + [ 'value' => $name ] );
397 }
398
399 /**
400 * Consume a string token
401 *
402 * This assumes the leading quote or apostrophe has already been consumed.
403 *
404 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#consume-string-token
405 * @param string $endChar Ending character of the string
406 * @param array $data Data for the new token (typically contains just 'position')
407 * @return Token
408 */
409 protected function consumeStringToken( $endChar, array $data ) {
410 $data['value'] = '';
411
412 while ( true ) {
413 $this->consumeCharacter();
414 switch ( $this->currentCharacter ) {
415 case DataSource::EOF:
416 $this->parseError( 'unclosed-string', $data );
417 break 2;
418
419 case $endChar:
420 break 2;
421
422 case "\n":
423 $this->parseError( 'newline-in-string' );
424 $this->reconsumeCharacter();
425 return new Token( Token::T_BAD_STRING, [ 'value' => '' ] + $data );
426
427 case '\\':
428 if ( $this->nextCharacter === DataSource::EOF ) {
429 // Do nothing
430 } elseif ( $this->nextCharacter === "\n" ) {
431 // Consume it
432 $this->consumeCharacter();
433 } elseif ( self::isValidEscape( $this->currentCharacter, $this->nextCharacter ) ) {
434 $data['value'] .= $this->consumeEscape();
435 } else {
436 // @codeCoverageIgnoreStart
437 throw new UnexpectedValueException( "[$this->line:$this->pos] Unexpected state" );
438 // @codeCoverageIgnoreEnd
439 }
440 break;
441
442 default:
443 $data['value'] .= $this->currentCharacter;
444 break;
445 }
446 }
447
448 // @phan-suppress-next-line PhanPluginUnreachableCode Reached by break 2
449 return new Token( Token::T_STRING, $data );
450 }
451
452 /**
453 * Consume a URL token
454 *
455 * This assumes the leading "url(" has already been consumed.
456 *
457 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#consume-url-token
458 * @param array $data Data for the new token (typically contains just 'position')
459 * @return Token
460 */
461 protected function consumeUrlToken( array $data ) {
462 // 1.
463 $data['value'] = '';
464
465 // 2.
466 while ( self::isWhitespace( $this->nextCharacter ) ) {
467 $this->consumeCharacter();
468 }
469
470 // 3.
471 while ( true ) {
472 $this->consumeCharacter();
473 switch ( $this->currentCharacter ) {
474 case DataSource::EOF:
475 $this->parseError( 'unclosed-url', $data );
476 break 2;
477
478 // @codeCoverageIgnoreStart
479 case ')':
480 // @codeCoverageIgnoreEnd
481 break 2;
482
483 // @codeCoverageIgnoreStart
484 case "\n":
485 case "\t":
486 case ' ':
487 // @codeCoverageIgnoreEnd
488 while ( self::isWhitespace( $this->nextCharacter ) ) {
489 $this->consumeCharacter();
490 }
491 if ( $this->nextCharacter === ')' ) {
492 $this->consumeCharacter();
493 break 2;
494 } elseif ( $this->nextCharacter === DataSource::EOF ) {
495 $this->consumeCharacter();
496 $this->parseError( 'unclosed-url', $data );
497 break 2;
498 } else {
499 $this->consumeBadUrlRemnants();
500 return new Token( Token::T_BAD_URL, [ 'value' => '' ] + $data );
501 }
502
503 // @codeCoverageIgnoreStart
504 case '"':
505 case '\'':
506 case '(':
507 // @codeCoverageIgnoreEnd
508 $this->parseError( 'bad-character-in-url' );
509 $this->consumeBadUrlRemnants();
510 return new Token( Token::T_BAD_URL, [ 'value' => '' ] + $data );
511
512 // @codeCoverageIgnoreStart
513 case '\\':
514 // @codeCoverageIgnoreEnd
515 if ( self::isValidEscape( $this->currentCharacter, $this->nextCharacter ) ) {
516 $data['value'] .= $this->consumeEscape();
517 } else {
518 $this->parseError( 'bad-escape' );
519 $this->consumeBadUrlRemnants();
520 return new Token( Token::T_BAD_URL, [ 'value' => '' ] + $data );
521 }
522 break;
523
524 default:
525 if ( self::isNonPrintable( $this->currentCharacter ) ) {
526 $this->parseError( 'bad-character-in-url' );
527 $this->consumeBadUrlRemnants();
528 return new Token( Token::T_BAD_URL, [ 'value' => '' ] + $data );
529 }
530
531 $data['value'] .= $this->currentCharacter;
532 break;
533 }
534 }
535
536 // @phan-suppress-next-line PhanPluginUnreachableCode Reached by break 2
537 return new Token( Token::T_URL, $data );
538 }
539
540 /**
541 * Clean up after finding an error in a URL
542 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#consume-remnants-of-bad-url
543 */
544 protected function consumeBadUrlRemnants() {
545 while ( true ) {
546 $this->consumeCharacter();
547 if ( $this->currentCharacter === ')' || $this->currentCharacter === DataSource::EOF ) {
548 break;
549 }
550 if ( self::isValidEscape( $this->currentCharacter, $this->nextCharacter ) ) {
551 $this->consumeEscape();
552 }
553 }
554 }
555
556 /**
557 * Indicate if a character is whitespace
558 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#whitespace
559 * @param string $char A single UTF-8 character
560 * @return bool
561 */
562 protected static function isWhitespace( $char ) {
563 return $char === "\n" || $char === "\t" || $char === " ";
564 }
565
566 /**
567 * Indicate if a character is a name-start code point
568 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#name-start-code-point
569 * @param string $char A single UTF-8 character
570 * @return bool
571 */
572 protected static function isNameStartCharacter( $char ) {
573 // Every non-ASCII character is a name start character, so we can just
574 // check the first byte.
575 $char = ord( $char[0] );
576 return ( $char >= 0x41 && $char <= 0x5a ) ||
577 ( $char >= 0x61 && $char <= 0x7a ) ||
578 $char >= 0x80 || $char === 0x5f;
579 }
580
581 /**
582 * Indicate if a character is a name code point
583 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#name-code-point
584 * @param string $char A single UTF-8 character
585 * @return bool
586 */
587 protected static function isNameCharacter( $char ) {
588 // Every non-ASCII character is a name character, so we can just check
589 // the first byte.
590 $char = ord( $char[0] );
591 return ( $char >= 0x41 && $char <= 0x5a ) ||
592 ( $char >= 0x61 && $char <= 0x7a ) ||
593 ( $char >= 0x30 && $char <= 0x39 ) ||
594 $char >= 0x80 || $char === 0x5f || $char === 0x2d;
595 }
596
597 /**
598 * Indicate if a character is non-printable
599 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#non-printable-code-point
600 * @param string $char A single UTF-8 character
601 * @return bool
602 */
603 protected static function isNonPrintable( $char ) {
604 // No non-ASCII character is non-printable, so we can just check the
605 // first byte.
606 $char = ord( $char[0] );
607 return ( $char >= 0x00 && $char <= 0x08 ) ||
608 $char === 0x0b ||
609 ( $char >= 0x0e && $char <= 0x1f ) ||
610 $char === 0x7f;
611 }
612
613 /**
614 * Indicate if a character is a digit
615 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#digit
616 * @param string $char A single UTF-8 character
617 * @return bool
618 */
619 protected static function isDigit( $char ) {
620 // No non-ASCII character is a digit, so we can just check the first
621 // byte.
622 $char = ord( $char[0] );
623 return $char >= 0x30 && $char <= 0x39;
624 }
625
626 /**
627 * Indicate if a character is a hex digit
628 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#hex-digit
629 * @param string $char A single UTF-8 character
630 * @return bool
631 */
632 protected static function isHexDigit( $char ) {
633 // No non-ASCII character is a hex digit, so we can just check the
634 // first byte.
635 $char = ord( $char[0] );
636 return ( $char >= 0x30 && $char <= 0x39 ) ||
637 ( $char >= 0x41 && $char <= 0x46 ) ||
638 ( $char >= 0x61 && $char <= 0x66 );
639 }
640
641 /**
642 * Determine if two characters constitute a valid escape
643 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#starts-with-a-valid-escape
644 * @param string $char1
645 * @param string $char2
646 * @return bool
647 */
648 protected static function isValidEscape( $char1, $char2 ) {
649 return $char1 === '\\' && $char2 !== "\n";
650 }
651
652 /**
653 * Determine if three characters would start an identifier
654 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#would-start-an-identifier
655 * @param string $char1
656 * @param string $char2
657 * @param string $char3
658 * @return bool
659 */
660 protected static function wouldStartIdentifier( $char1, $char2, $char3 ) {
661 if ( $char1 === DataSource::EOF ) {
662 return false;
663 } elseif ( $char1 === '-' && $char2 !== DataSource::EOF ) {
664 return self::isNameStartCharacter( $char2 ) || $char2 === '-' ||
665 self::isValidEscape( $char2, $char3 );
666 } elseif ( self::isNameStartCharacter( $char1 ) ) {
667 return true;
668 } elseif ( $char1 === '\\' ) {
669 return self::isValidEscape( $char1, $char2 );
670 } else {
671 return false;
672 }
673 }
674
675 /**
676 * Determine if three characters would start a number
677 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#starts-with-a-number
678 * @param string $char1
679 * @param string $char2
680 * @param string $char3
681 * @return bool
682 */
683 protected static function wouldStartNumber( $char1, $char2, $char3 ) {
684 if ( ( $char1 === '+' || $char1 === '-' ) && $char2 !== DataSource::EOF ) {
685 return self::isDigit( $char2 ) ||
686 ( $char2 === '.' && self::isDigit( $char3 ) );
687 } elseif ( $char1 === '.' && $char2 !== DataSource::EOF ) {
688 return self::isDigit( $char2 );
689 // @codeCoverageIgnoreStart
690 // Nothing reaches this code
691 } else {
692 return self::isDigit( $char1 );
693 }
694 // @codeCoverageIgnoreEnd
695 }
696
697 /**
698 * Consume a valid escape
699 *
700 * This assumes the leading backslash is consumed.
701 *
702 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#consume-escaped-code-point
703 * @return string Escaped character
704 */
705 protected function consumeEscape() {
706 $position = [ 'position' => [ $this->line, $this->pos ] ];
707
708 $this->consumeCharacter();
709
710 // 1-6 hexits, plus one optional whitespace character
711 if ( $this->currentCharacter !== DataSource::EOF && self::isHexDigit( $this->currentCharacter ) ) {
712 $num = $this->currentCharacter;
713 while ( strlen( $num ) < 6 &&
714 $this->nextCharacter !== DataSource::EOF &&
715 self::isHexDigit( $this->nextCharacter )
716 ) {
717 $this->consumeCharacter();
718 $num .= $this->currentCharacter;
719 }
720 if ( self::isWhitespace( $this->nextCharacter ) ) {
721 $this->consumeCharacter();
722 }
723
724 $num = intval( $num, 16 );
725 if ( $num === 0 || ( $num >= 0xd800 && $num <= 0xdfff ) || $num > 0x10ffff ) {
726 return Constants::UTF8_REPLACEMENT;
727 }
728 return Utils::codepointToUtf8( $num );
729 }
730
731 if ( $this->currentCharacter === DataSource::EOF ) {
732 $this->parseError( 'bad-escape', $position );
733 return Constants::UTF8_REPLACEMENT;
734 }
735
736 return $this->currentCharacter;
737 }
738
739 /**
740 * Consume a name
741 *
742 * Note this does not do validation on the input stream. Call
743 * self::wouldStartIdentifier() or the like before calling the method if
744 * necessary.
745 *
746 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#consume-name
747 * @return string Name
748 */
749 protected function consumeName() {
750 $name = '';
751
752 while ( true ) {
753 $this->consumeCharacter();
754
755 if ( $this->currentCharacter === DataSource::EOF ) {
756 break;
757 } elseif ( self::isNameCharacter( $this->currentCharacter ) ) {
758 $name .= $this->currentCharacter;
759 } elseif ( self::isValidEscape( $this->currentCharacter, $this->nextCharacter ) ) {
760 $name .= $this->consumeEscape();
761 } else {
762 $this->reconsumeCharacter();
763 break;
764 }
765 }
766
767 return $name;
768 }
769
770 /**
771 * Consume a number
772 *
773 * Note this does not do validation on the input stream. Call
774 * self::wouldStartNumber() before calling the method if necessary.
775 *
776 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#consume-number
777 * @return array [ string $value, int|float $number, string $type ('integer' or 'number') ]
778 * @suppress PhanPluginDuplicateAdjacentStatement
779 */
780 protected function consumeNumber() {
781 // 1.
782 $repr = '';
783 $type = 'integer';
784
785 // 2.
786 if ( $this->nextCharacter === '+' || $this->nextCharacter === '-' ) {
787 $this->consumeCharacter();
788 $repr .= $this->currentCharacter;
789 }
790
791 // 3.
792 while ( $this->nextCharacter !== DataSource::EOF && self::isDigit( $this->nextCharacter ) ) {
793 $this->consumeCharacter();
794 $repr .= $this->currentCharacter;
795 }
796
797 // 4.
798 if ( $this->nextCharacter === '.' ) {
799 [ $next, $next2, ] = $this->lookAhead();
800 if ( self::isDigit( $next2 ) ) {
801 // 4.1.
802 $this->consumeCharacter();
803 $this->consumeCharacter();
804 // 4.2.
805 $repr .= $next . $next2;
806 // 4.3.
807 $type = 'number';
808 // 4.4.
809 while ( $this->nextCharacter !== DataSource::EOF && self::isDigit( $this->nextCharacter ) ) {
810 $this->consumeCharacter();
811 $repr .= $this->currentCharacter;
812 }
813 }
814 }
815
816 // 5.
817 if ( $this->nextCharacter === 'e' || $this->nextCharacter === 'E' ) {
818 [ $next, $next2, $next3 ] = $this->lookAhead();
819 $ok = false;
820 if ( ( $next2 === '+' || $next2 === '-' ) && self::isDigit( $next3 ) ) {
821 $ok = true;
822 // 5.1.
823 $this->consumeCharacter();
824 $this->consumeCharacter();
825 $this->consumeCharacter();
826 // 5.2.
827 $repr .= $next . $next2 . $next3;
828 } elseif ( self::isDigit( $next2 ) ) {
829 $ok = true;
830 // 5.1.
831 $this->consumeCharacter();
832 $this->consumeCharacter();
833 // 5.2.
834 $repr .= $next . $next2;
835 }
836 if ( $ok ) {
837 // 5.3.
838 $type = 'number';
839 // 5.4.
840 while ( $this->nextCharacter !== DataSource::EOF && self::isDigit( $this->nextCharacter ) ) {
841 $this->consumeCharacter();
842 $repr .= $this->currentCharacter;
843 }
844 }
845 }
846
847 // 6. We assume PHP's casting follows the same rules as
848 // https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#convert-string-to-number
849 $value = $type === 'integer' ? (int)$repr : (float)$repr;
850
851 // 7.
852 return [ $repr, $value, $type ];
853 }
854 }