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

MyIntervals / PHP-CSS-Parser / 21410081995

27 Jan 2026 06:56PM UTC coverage: 70.488% (-0.8%) from 71.315%
21410081995

Pull #1484

github

web-flow
Merge 2332f66fa into 96410045c
Pull Request #1484: Remove `thecodingmachine/safe` dependency (2)

21 of 60 new or added lines in 8 files covered. (35.0%)

5 existing lines in 3 files now uncovered.

1445 of 2050 relevant lines covered (70.49%)

30.36 hits per line

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

52.73
/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
/**
11
 * @internal since 8.7.0
12
 */
13
class ParserState
14
{
15
    public const EOF = null;
16

17
    /**
18
     * @var Settings
19
     */
20
    private $parserSettings;
21

22
    /**
23
     * @var string
24
     */
25
    private $text;
26

27
    /**
28
     * @var array<int, string>
29
     */
30
    private $characters;
31

32
    /**
33
     * @var int<0, max>
34
     */
35
    private $currentPosition = 0;
36

37
    /**
38
     * will only be used if the CSS does not contain an `@charset` declaration
39
     *
40
     * @var string
41
     */
42
    private $charset;
43

44
    /**
45
     * @var int<1, max> $lineNumber
46
     */
47
    private $lineNumber;
48

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

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

70
    /**
71
     * @return int<1, max>
72
     */
73
    public function currentLine(): int
1✔
74
    {
75
        return $this->lineNumber;
1✔
76
    }
77

78
    /**
79
     * @return int<0, max>
80
     */
81
    public function currentColumn(): int
×
82
    {
83
        return $this->currentPosition;
×
84
    }
85

86
    public function getSettings(): Settings
×
87
    {
88
        return $this->parserSettings;
×
89
    }
90

91
    public function anchor(): Anchor
×
92
    {
93
        return new Anchor($this->currentPosition, $this);
×
94
    }
95

96
    /**
97
     * @param int<0, max> $position
98
     */
99
    public function setPosition(int $position): void
×
100
    {
101
        $this->currentPosition = $position;
×
102
    }
×
103

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

135
        return $result;
×
136
    }
137

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

199
        return null;
×
200
    }
201

202
    /**
203
     * Consumes whitespace and/or comments until the next non-whitespace character that isn't a slash opening a comment.
204
     *
205
     * @param list<Comment> $comments Any comments consumed will be appended to this array.
206
     *
207
     * @return string the whitespace consumed, without the comments
208
     *
209
     * @throws UnexpectedEOFException
210
     * @throws UnexpectedTokenException
211
     *
212
     * @phpstan-impure
213
     * This method may change the state of the object by advancing the internal position;
214
     * it does not simply 'get' a value.
215
     */
216
    public function consumeWhiteSpace(array &$comments = []): string
91✔
217
    {
218
        $consumed = '';
91✔
219
        do {
220
            while (true) {
91✔
221
                /** @phpstan-ignore theCodingMachineSafe.function */
222
                $whitespaceCheck = \preg_match('/\\s/isSu', $this->peek());
91✔
223
                if ($whitespaceCheck === false) {
91✔
NEW
224
                    throw new \RuntimeException('Unexpected error');
×
225
                }
226
                if ($whitespaceCheck !== 1) {
91✔
227
                    break;
91✔
228
                }
229
                $consumed .= $this->consume(1);
68✔
230
            }
231
            if ($this->parserSettings->usesLenientParsing()) {
91✔
232
                try {
233
                    $comment = $this->consumeComment();
91✔
234
                } catch (UnexpectedEOFException $e) {
×
235
                    $this->currentPosition = \count($this->characters);
×
236
                    break;
91✔
237
                }
238
            } else {
239
                $comment = $this->consumeComment();
×
240
            }
241
            if ($comment instanceof Comment) {
91✔
242
                $comments[] = $comment;
49✔
243
            }
244
        } while ($comment instanceof Comment);
91✔
245

246
        return $consumed;
91✔
247
    }
248

249
    /**
250
     * @param non-empty-string $string
251
     */
252
    public function comes(string $string, bool $caseInsensitive = false): bool
97✔
253
    {
254
        $peek = $this->peek(\strlen($string));
97✔
255

256
        return ($peek !== '') && $this->streql($peek, $string, $caseInsensitive);
97✔
257
    }
258

259
    /**
260
     * @param int<1, max> $length
261
     * @param int<0, max> $offset
262
     */
263
    public function peek(int $length = 1, int $offset = 0): string
99✔
264
    {
265
        $offset += $this->currentPosition;
99✔
266
        if ($offset >= \count($this->characters)) {
99✔
267
            return '';
19✔
268
        }
269

270
        return $this->substr($offset, $length);
98✔
271
    }
272

273
    /**
274
     * @param string|int<1, max> $value
275
     *
276
     * @throws UnexpectedEOFException
277
     * @throws UnexpectedTokenException
278
     */
279
    public function consume($value = 1): string
90✔
280
    {
281
        if (\is_string($value)) {
90✔
282
            $numberOfLines = \substr_count($value, "\n");
×
283
            $length = $this->strlen($value);
×
284
            if (!$this->streql($this->substr($this->currentPosition, $length), $value)) {
×
285
                throw new UnexpectedTokenException(
×
286
                    $value,
×
287
                    $this->peek(\max($length, 5)),
×
288
                    'literal',
×
289
                    $this->lineNumber
×
290
                );
291
            }
292

293
            $this->lineNumber += $numberOfLines;
×
294
            $this->currentPosition += $this->strlen($value);
×
295
            $result = $value;
×
296
        } else {
297
            if ($this->currentPosition + $value > \count($this->characters)) {
90✔
298
                throw new UnexpectedEOFException((string) $value, $this->peek(5), 'count', $this->lineNumber);
×
299
            }
300

301
            $result = $this->substr($this->currentPosition, $value);
90✔
302
            $numberOfLines = \substr_count($result, "\n");
90✔
303
            $this->lineNumber += $numberOfLines;
90✔
304
            $this->currentPosition += $value;
90✔
305
        }
306

307
        return $result;
90✔
308
    }
309

310
    /**
311
     * If the possibly-expected next content is next, consume it.
312
     *
313
     * @param non-empty-string $nextContent
314
     *
315
     * @return bool whether the possibly-expected content was found and consumed
316
     */
317
    public function consumeIfComes(string $nextContent): bool
5✔
318
    {
319
        $length = $this->strlen($nextContent);
5✔
320
        if (!$this->streql($this->substr($this->currentPosition, $length), $nextContent)) {
5✔
321
            return false;
2✔
322
        }
323

324
        $numberOfLines = \substr_count($nextContent, "\n");
3✔
325
        $this->lineNumber += $numberOfLines;
3✔
326
        $this->currentPosition += $this->strlen($nextContent);
3✔
327

328
        return true;
3✔
329
    }
330

331
    /**
332
     * @param string $expression
333
     * @param int<1, max>|null $maximumLength
334
     *
335
     * @throws UnexpectedEOFException
336
     * @throws UnexpectedTokenException
337
     */
338
    public function consumeExpression(string $expression, ?int $maximumLength = null): string
×
339
    {
340
        $matches = null;
×
341
        $input = ($maximumLength !== null) ? $this->peek($maximumLength) : $this->inputLeft();
×
342
        /** @phpstan-ignore theCodingMachineSafe.function */
NEW
343
        if (\preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) {
×
UNCOV
344
            throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber);
×
345
        }
346

347
        return $this->consume($matches[0][0]);
×
348
    }
349

350
    /**
351
     * @return Comment|false
352
     */
353
    public function consumeComment()
97✔
354
    {
355
        $lineNumber = $this->lineNumber;
97✔
356
        $comment = null;
97✔
357

358
        if ($this->comes('/*')) {
97✔
359
            $this->consume(1);
55✔
360
            $comment = '';
55✔
361
            while (($char = $this->consume(1)) !== '') {
55✔
362
                $comment .= $char;
55✔
363
                if ($this->comes('*/')) {
55✔
364
                    $this->consume(2);
55✔
365
                    break;
55✔
366
                }
367
            }
368
        }
369

370
        // We skip the * which was included in the comment.
371
        return \is_string($comment) ? new Comment(\substr($comment, 1), $lineNumber) : false;
97✔
372
    }
373

374
    public function isEnd(): bool
6✔
375
    {
376
        return $this->currentPosition >= \count($this->characters);
6✔
377
    }
378

379
    /**
380
     * @param list<string|self::EOF>|string|self::EOF $stopCharacters
381
     * @param list<Comment> $comments
382
     *
383
     * @throws UnexpectedEOFException
384
     * @throws UnexpectedTokenException
385
     */
386
    public function consumeUntil(
6✔
387
        $stopCharacters,
388
        bool $includeEnd = false,
389
        bool $consumeEnd = false,
390
        array &$comments = []
391
    ): string {
392
        $stopCharacters = \is_array($stopCharacters) ? $stopCharacters : [$stopCharacters];
6✔
393
        $consumedCharacters = '';
6✔
394
        $start = $this->currentPosition;
6✔
395

396
        $comments = \array_merge($comments, $this->consumeComments());
6✔
397
        while (!$this->isEnd()) {
6✔
398
            $character = $this->consume(1);
6✔
399
            if (\in_array($character, $stopCharacters, true)) {
6✔
400
                if ($includeEnd) {
6✔
401
                    $consumedCharacters .= $character;
×
402
                } elseif (!$consumeEnd) {
6✔
403
                    $this->currentPosition -= $this->strlen($character);
6✔
404
                }
405
                return $consumedCharacters;
6✔
406
            }
407
            $consumedCharacters .= $character;
6✔
408
            $comments = \array_merge($comments, $this->consumeComments());
6✔
409
        }
410

411
        if (\in_array(self::EOF, $stopCharacters, true)) {
×
412
            return $consumedCharacters;
×
413
        }
414

415
        $this->currentPosition = $start;
×
416
        throw new UnexpectedEOFException(
×
417
            'One of ("' . \implode('","', $stopCharacters) . '")',
×
418
            $this->peek(5),
×
419
            'search',
×
420
            $this->lineNumber
×
421
        );
422
    }
423

424
    private function inputLeft(): string
×
425
    {
426
        return $this->substr($this->currentPosition, -1);
×
427
    }
428

429
    public function streql(string $string1, string $string2, bool $caseInsensitive = true): bool
102✔
430
    {
431
        return $caseInsensitive
102✔
432
            ? ($this->strtolower($string1) === $this->strtolower($string2))
102✔
433
            : ($string1 === $string2);
102✔
434
    }
435

436
    /**
437
     * @param int<1, max> $numberOfCharacters
438
     */
439
    public function backtrack(int $numberOfCharacters): void
×
440
    {
441
        $this->currentPosition -= $numberOfCharacters;
×
442
    }
×
443

444
    /**
445
     * @return int<0, max>
446
     */
447
    public function strlen(string $string): int
11✔
448
    {
449
        return $this->parserSettings->hasMultibyteSupport()
11✔
450
            ? \mb_strlen($string, $this->charset)
11✔
451
            : \strlen($string);
11✔
452
    }
453

454
    /**
455
     * @param int<0, max> $offset
456
     */
457
    private function substr(int $offset, int $length): string
101✔
458
    {
459
        if ($length < 0) {
101✔
460
            $length = \count($this->characters) - $offset + $length;
×
461
        }
462
        if ($offset + $length > \count($this->characters)) {
101✔
463
            $length = \count($this->characters) - $offset;
54✔
464
        }
465
        $result = '';
101✔
466
        while ($length > 0) {
101✔
467
            $result .= $this->characters[$offset];
101✔
468
            $offset++;
101✔
469
            $length--;
101✔
470
        }
471

472
        return $result;
101✔
473
    }
474

475
    /**
476
     * @return ($string is non-empty-string ? non-empty-string : string)
477
     */
478
    private function strtolower(string $string): string
102✔
479
    {
480
        return $this->parserSettings->hasMultibyteSupport()
102✔
481
            ? \mb_strtolower($string, $this->charset)
102✔
482
            : \strtolower($string);
102✔
483
    }
484

485
    /**
486
     * @return list<string>
487
     */
488
    private function strsplit(string $string): array
102✔
489
    {
490
        if ($this->parserSettings->hasMultibyteSupport()) {
102✔
491
            if ($this->streql($this->charset, 'utf-8')) {
102✔
492
                /** @phpstan-ignore theCodingMachineSafe.function */
493
                $result = \preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
102✔
494
                if ($result === false) {
102✔
495
                    throw new \RuntimeException('Unexpected error');
102✔
496
                }
497
            } else {
498
                $length = \mb_strlen($string, $this->charset);
×
499
                $result = [];
×
500
                for ($i = 0; $i < $length; ++$i) {
102✔
501
                    $result[] = \mb_substr($string, $i, 1, $this->charset);
×
502
                }
503
            }
504
        } else {
505
            $result = ($string !== '') ? \str_split($string) : [];
×
506
        }
507

508
        return $result;
102✔
509
    }
510

511
    /**
512
     * @return list<Comment>
513
     */
514
    private function consumeComments(): array
6✔
515
    {
516
        $comments = [];
6✔
517

518
        while (true) {
6✔
519
            $comment = $this->consumeComment();
6✔
520
            if ($comment instanceof Comment) {
6✔
521
                $comments[] = $comment;
6✔
522
            } else {
523
                return $comments;
6✔
524
            }
525
        }
526
    }
×
527
}
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