• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MyIntervals / PHP-CSS-Parser / 21338071049

25 Jan 2026 07:14PM UTC coverage: 71.315% (+0.6%) from 70.738%
21338071049

Pull #1478

github

web-flow
Merge a293ba132 into 416f6a7fe
Pull Request #1478: [TASK] Add `SelectorComponent` interface

1432 of 2008 relevant lines covered (71.31%)

30.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.92
/src/Parsing/ParserState.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\Parsing;
6

7
use Sabberworm\CSS\Comment\Comment;
8
use Sabberworm\CSS\Settings;
9

10
use function Safe\iconv;
11
use function Safe\preg_match;
12
use function Safe\preg_split;
13

14
/**
15
 * @internal since 8.7.0
16
 */
17
class ParserState
18
{
19
    public const EOF = null;
20

21
    /**
22
     * @var Settings
23
     */
24
    private $parserSettings;
25

26
    /**
27
     * @var string
28
     */
29
    private $text;
30

31
    /**
32
     * @var array<int, string>
33
     */
34
    private $characters;
35

36
    /**
37
     * @var int<0, max>
38
     */
39
    private $currentPosition = 0;
40

41
    /**
42
     * will only be used if the CSS does not contain an `@charset` declaration
43
     *
44
     * @var string
45
     */
46
    private $charset;
47

48
    /**
49
     * @var int<1, max> $lineNumber
50
     */
51
    private $lineNumber;
52

53
    /**
54
     * @param string $text the complete CSS as text (i.e., usually the contents of a CSS file)
55
     * @param int<1, max> $lineNumber
56
     */
57
    public function __construct(string $text, Settings $parserSettings, int $lineNumber = 1)
102✔
58
    {
59
        $this->parserSettings = $parserSettings;
102✔
60
        $this->text = $text;
102✔
61
        $this->lineNumber = $lineNumber;
102✔
62
        $this->setCharset($this->parserSettings->getDefaultCharset());
102✔
63
    }
102✔
64

65
    /**
66
     * Sets the charset to be used if the CSS does not contain an `@charset` declaration.
67
     */
68
    public function setCharset(string $charset): void
102✔
69
    {
70
        $this->charset = $charset;
102✔
71
        $this->characters = $this->strsplit($this->text);
102✔
72
    }
102✔
73

74
    /**
75
     * @return int<1, max>
76
     */
77
    public function currentLine(): int
1✔
78
    {
79
        return $this->lineNumber;
1✔
80
    }
81

82
    /**
83
     * @return int<0, max>
84
     */
85
    public function currentColumn(): int
×
86
    {
87
        return $this->currentPosition;
×
88
    }
89

90
    public function getSettings(): Settings
×
91
    {
92
        return $this->parserSettings;
×
93
    }
94

95
    public function anchor(): Anchor
×
96
    {
97
        return new Anchor($this->currentPosition, $this);
×
98
    }
99

100
    /**
101
     * @param int<0, max> $position
102
     */
103
    public function setPosition(int $position): void
×
104
    {
105
        $this->currentPosition = $position;
×
106
    }
×
107

108
    /**
109
     * @return non-empty-string
110
     *
111
     * @throws UnexpectedTokenException
112
     */
113
    public function parseIdentifier(bool $ignoreCase = true): string
×
114
    {
115
        if ($this->isEnd()) {
×
116
            throw new UnexpectedEOFException('', '', 'identifier', $this->lineNumber);
×
117
        }
118
        $result = $this->parseCharacter(true);
×
119
        if ($result === null) {
×
120
            throw new UnexpectedTokenException('', $this->peek(5), 'identifier', $this->lineNumber);
×
121
        }
122
        $character = null;
×
123
        while (!$this->isEnd() && ($character = $this->parseCharacter(true)) !== null) {
×
124
            if (preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $character) !== 0) {
×
125
                $result .= $character;
×
126
            } else {
127
                $result .= '\\' . $character;
×
128
            }
129
        }
130
        if ($ignoreCase) {
×
131
            $result = $this->strtolower($result);
×
132
        }
133

134
        return $result;
×
135
    }
136

137
    /**
138
     * @throws UnexpectedEOFException
139
     * @throws UnexpectedTokenException
140
     */
141
    public function parseCharacter(bool $isForIdentifier): ?string
×
142
    {
143
        if ($this->peek() === '\\') {
×
144
            $this->consume('\\');
×
145
            if ($this->comes('\\n') || $this->comes('\\r')) {
×
146
                return '';
×
147
            }
148
            if (preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
×
149
                return $this->consume(1);
×
150
            }
151
            $hexCodePoint = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6);
×
152
            if ($this->strlen($hexCodePoint) < 6) {
×
153
                // Consume whitespace after incomplete unicode escape
154
                if (preg_match('/\\s/isSu', $this->peek()) !== 0) {
×
155
                    if ($this->comes('\\r\\n')) {
×
156
                        $this->consume(2);
×
157
                    } else {
158
                        $this->consume(1);
×
159
                    }
160
                }
161
            }
162
            $codePoint = \intval($hexCodePoint, 16);
×
163
            $utf32EncodedCharacter = '';
×
164
            for ($i = 0; $i < 4; ++$i) {
×
165
                $utf32EncodedCharacter .= \chr($codePoint & 0xff);
×
166
                $codePoint = $codePoint >> 8;
×
167
            }
168
            return iconv('utf-32le', $this->charset, $utf32EncodedCharacter);
×
169
        }
170
        if ($isForIdentifier) {
×
171
            $peek = \ord($this->peek());
×
172
            // Ranges: a-z A-Z 0-9 - _
173
            if (
174
                ($peek >= 97 && $peek <= 122)
×
175
                || ($peek >= 65 && $peek <= 90)
×
176
                || ($peek >= 48 && $peek <= 57)
×
177
                || ($peek === 45)
×
178
                || ($peek === 95)
×
179
                || ($peek > 0xa1)
×
180
            ) {
181
                return $this->consume(1);
×
182
            }
183
        } else {
184
            return $this->consume(1);
×
185
        }
186

187
        return null;
×
188
    }
189

190
    /**
191
     * Consumes whitespace and/or comments until the next non-whitespace character that isn't a slash opening a comment.
192
     *
193
     * @param list<Comment> $comments Any comments consumed will be appended to this array.
194
     *
195
     * @return string the whitespace consumed, without the comments
196
     *
197
     * @throws UnexpectedEOFException
198
     * @throws UnexpectedTokenException
199
     *
200
     * @phpstan-impure
201
     * This method may change the state of the object by advancing the internal position;
202
     * it does not simply 'get' a value.
203
     */
204
    public function consumeWhiteSpace(array &$comments = []): string
91✔
205
    {
206
        $consumed = '';
91✔
207
        do {
208
            while (preg_match('/\\s/isSu', $this->peek()) === 1) {
91✔
209
                $consumed .= $this->consume(1);
68✔
210
            }
211
            if ($this->parserSettings->usesLenientParsing()) {
91✔
212
                try {
213
                    $comment = $this->consumeComment();
91✔
214
                } catch (UnexpectedEOFException $e) {
×
215
                    $this->currentPosition = \count($this->characters);
×
216
                    break;
91✔
217
                }
218
            } else {
219
                $comment = $this->consumeComment();
×
220
            }
221
            if ($comment instanceof Comment) {
91✔
222
                $comments[] = $comment;
49✔
223
            }
224
        } while ($comment instanceof Comment);
91✔
225

226
        return $consumed;
91✔
227
    }
228

229
    /**
230
     * @param non-empty-string $string
231
     */
232
    public function comes(string $string, bool $caseInsensitive = false): bool
97✔
233
    {
234
        $peek = $this->peek(\strlen($string));
97✔
235

236
        return ($peek !== '') && $this->streql($peek, $string, $caseInsensitive);
97✔
237
    }
238

239
    /**
240
     * @param int<1, max> $length
241
     * @param int<0, max> $offset
242
     */
243
    public function peek(int $length = 1, int $offset = 0): string
99✔
244
    {
245
        $offset += $this->currentPosition;
99✔
246
        if ($offset >= \count($this->characters)) {
99✔
247
            return '';
19✔
248
        }
249

250
        return $this->substr($offset, $length);
98✔
251
    }
252

253
    /**
254
     * @param string|int<1, max> $value
255
     *
256
     * @throws UnexpectedEOFException
257
     * @throws UnexpectedTokenException
258
     */
259
    public function consume($value = 1): string
90✔
260
    {
261
        if (\is_string($value)) {
90✔
262
            $numberOfLines = \substr_count($value, "\n");
×
263
            $length = $this->strlen($value);
×
264
            if (!$this->streql($this->substr($this->currentPosition, $length), $value)) {
×
265
                throw new UnexpectedTokenException(
×
266
                    $value,
×
267
                    $this->peek(\max($length, 5)),
×
268
                    'literal',
×
269
                    $this->lineNumber
×
270
                );
271
            }
272

273
            $this->lineNumber += $numberOfLines;
×
274
            $this->currentPosition += $this->strlen($value);
×
275
            $result = $value;
×
276
        } else {
277
            if ($this->currentPosition + $value > \count($this->characters)) {
90✔
278
                throw new UnexpectedEOFException((string) $value, $this->peek(5), 'count', $this->lineNumber);
×
279
            }
280

281
            $result = $this->substr($this->currentPosition, $value);
90✔
282
            $numberOfLines = \substr_count($result, "\n");
90✔
283
            $this->lineNumber += $numberOfLines;
90✔
284
            $this->currentPosition += $value;
90✔
285
        }
286

287
        return $result;
90✔
288
    }
289

290
    /**
291
     * If the possibly-expected next content is next, consume it.
292
     *
293
     * @param non-empty-string $nextContent
294
     *
295
     * @return bool whether the possibly-expected content was found and consumed
296
     */
297
    public function consumeIfComes(string $nextContent): bool
5✔
298
    {
299
        $length = $this->strlen($nextContent);
5✔
300
        if (!$this->streql($this->substr($this->currentPosition, $length), $nextContent)) {
5✔
301
            return false;
2✔
302
        }
303

304
        $numberOfLines = \substr_count($nextContent, "\n");
3✔
305
        $this->lineNumber += $numberOfLines;
3✔
306
        $this->currentPosition += $this->strlen($nextContent);
3✔
307

308
        return true;
3✔
309
    }
310

311
    /**
312
     * @param string $expression
313
     * @param int<1, max>|null $maximumLength
314
     *
315
     * @throws UnexpectedEOFException
316
     * @throws UnexpectedTokenException
317
     */
318
    public function consumeExpression(string $expression, ?int $maximumLength = null): string
×
319
    {
320
        $matches = null;
×
321
        $input = ($maximumLength !== null) ? $this->peek($maximumLength) : $this->inputLeft();
×
322
        if (preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) {
×
323
            throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber);
×
324
        }
325

326
        return $this->consume($matches[0][0]);
×
327
    }
328

329
    /**
330
     * @return Comment|false
331
     */
332
    public function consumeComment()
97✔
333
    {
334
        $lineNumber = $this->lineNumber;
97✔
335
        $comment = null;
97✔
336

337
        if ($this->comes('/*')) {
97✔
338
            $this->consume(1);
55✔
339
            $comment = '';
55✔
340
            while (($char = $this->consume(1)) !== '') {
55✔
341
                $comment .= $char;
55✔
342
                if ($this->comes('*/')) {
55✔
343
                    $this->consume(2);
55✔
344
                    break;
55✔
345
                }
346
            }
347
        }
348

349
        // We skip the * which was included in the comment.
350
        return \is_string($comment) ? new Comment(\substr($comment, 1), $lineNumber) : false;
97✔
351
    }
352

353
    public function isEnd(): bool
6✔
354
    {
355
        return $this->currentPosition >= \count($this->characters);
6✔
356
    }
357

358
    /**
359
     * @param list<string|self::EOF>|string|self::EOF $stopCharacters
360
     * @param list<Comment> $comments
361
     *
362
     * @throws UnexpectedEOFException
363
     * @throws UnexpectedTokenException
364
     */
365
    public function consumeUntil(
6✔
366
        $stopCharacters,
367
        bool $includeEnd = false,
368
        bool $consumeEnd = false,
369
        array &$comments = []
370
    ): string {
371
        $stopCharacters = \is_array($stopCharacters) ? $stopCharacters : [$stopCharacters];
6✔
372
        $consumedCharacters = '';
6✔
373
        $start = $this->currentPosition;
6✔
374

375
        $comments = \array_merge($comments, $this->consumeComments());
6✔
376
        while (!$this->isEnd()) {
6✔
377
            $character = $this->consume(1);
6✔
378
            if (\in_array($character, $stopCharacters, true)) {
6✔
379
                if ($includeEnd) {
6✔
380
                    $consumedCharacters .= $character;
×
381
                } elseif (!$consumeEnd) {
6✔
382
                    $this->currentPosition -= $this->strlen($character);
6✔
383
                }
384
                return $consumedCharacters;
6✔
385
            }
386
            $consumedCharacters .= $character;
6✔
387
            $comments = \array_merge($comments, $this->consumeComments());
6✔
388
        }
389

390
        if (\in_array(self::EOF, $stopCharacters, true)) {
×
391
            return $consumedCharacters;
×
392
        }
393

394
        $this->currentPosition = $start;
×
395
        throw new UnexpectedEOFException(
×
396
            'One of ("' . \implode('","', $stopCharacters) . '")',
×
397
            $this->peek(5),
×
398
            'search',
×
399
            $this->lineNumber
×
400
        );
401
    }
402

403
    private function inputLeft(): string
×
404
    {
405
        return $this->substr($this->currentPosition, -1);
×
406
    }
407

408
    public function streql(string $string1, string $string2, bool $caseInsensitive = true): bool
102✔
409
    {
410
        return $caseInsensitive
102✔
411
            ? ($this->strtolower($string1) === $this->strtolower($string2))
102✔
412
            : ($string1 === $string2);
102✔
413
    }
414

415
    /**
416
     * @param int<1, max> $numberOfCharacters
417
     */
418
    public function backtrack(int $numberOfCharacters): void
×
419
    {
420
        $this->currentPosition -= $numberOfCharacters;
×
421
    }
×
422

423
    /**
424
     * @return int<0, max>
425
     */
426
    public function strlen(string $string): int
11✔
427
    {
428
        return $this->parserSettings->hasMultibyteSupport()
11✔
429
            ? \mb_strlen($string, $this->charset)
11✔
430
            : \strlen($string);
11✔
431
    }
432

433
    /**
434
     * @param int<0, max> $offset
435
     */
436
    private function substr(int $offset, int $length): string
101✔
437
    {
438
        if ($length < 0) {
101✔
439
            $length = \count($this->characters) - $offset + $length;
×
440
        }
441
        if ($offset + $length > \count($this->characters)) {
101✔
442
            $length = \count($this->characters) - $offset;
54✔
443
        }
444
        $result = '';
101✔
445
        while ($length > 0) {
101✔
446
            $result .= $this->characters[$offset];
101✔
447
            $offset++;
101✔
448
            $length--;
101✔
449
        }
450

451
        return $result;
101✔
452
    }
453

454
    /**
455
     * @return ($string is non-empty-string ? non-empty-string : string)
456
     */
457
    private function strtolower(string $string): string
102✔
458
    {
459
        return $this->parserSettings->hasMultibyteSupport()
102✔
460
            ? \mb_strtolower($string, $this->charset)
102✔
461
            : \strtolower($string);
102✔
462
    }
463

464
    /**
465
     * @return list<string>
466
     */
467
    private function strsplit(string $string): array
102✔
468
    {
469
        if ($this->parserSettings->hasMultibyteSupport()) {
102✔
470
            if ($this->streql($this->charset, 'utf-8')) {
102✔
471
                $result = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
102✔
472
            } else {
473
                $length = \mb_strlen($string, $this->charset);
×
474
                $result = [];
×
475
                for ($i = 0; $i < $length; ++$i) {
102✔
476
                    $result[] = \mb_substr($string, $i, 1, $this->charset);
×
477
                }
478
            }
479
        } else {
480
            $result = ($string !== '') ? \str_split($string) : [];
×
481
        }
482

483
        return $result;
102✔
484
    }
485

486
    /**
487
     * @return list<Comment>
488
     */
489
    private function consumeComments(): array
6✔
490
    {
491
        $comments = [];
6✔
492

493
        while (true) {
6✔
494
            $comment = $this->consumeComment();
6✔
495
            if ($comment instanceof Comment) {
6✔
496
                $comments[] = $comment;
6✔
497
            } else {
498
                return $comments;
6✔
499
            }
500
        }
501
    }
×
502
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc