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

MyIntervals / PHP-CSS-Parser / 13883317705

16 Mar 2025 12:26PM UTC coverage: 56.019% (+0.2%) from 55.84%
13883317705

Pull #1173

github

web-flow
Merge 919581524 into b561b72a0
Pull Request #1173: [CLEANUP] Clean up `ParserState` a bit

0 of 24 new or added lines in 1 file covered. (0.0%)

9 existing lines in 1 file now uncovered.

1047 of 1869 relevant lines covered (56.02%)

12.5 hits per line

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

0.0
/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
    /**
16
     * @var null
17
     */
18
    public const EOF = null;
19

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

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

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

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

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

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

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

64
    /**
65
     * Sets the charset to be used if the CSS does not contain an `@charset` declaration.
66
     *
67
     * @throws SourceException if the charset is UTF-8 and the content has invalid byte sequences
68
     */
69
    public function setCharset(string $charset): void
×
70
    {
71
        $this->charset = $charset;
×
72
        $this->characters = $this->strsplit($this->text);
×
73
    }
×
74

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

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

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

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

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

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

UNCOV
133
        return $result;
×
134
    }
135

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

UNCOV
186
        return null;
×
187
    }
188

189
    /**
190
     * @return list<Comment>
191
     *
192
     * @throws UnexpectedEOFException
193
     * @throws UnexpectedTokenException
194
     */
195
    public function consumeWhiteSpace(): array
×
196
    {
197
        $comments = [];
×
198
        do {
199
            while (\preg_match('/\\s/isSu', $this->peek()) === 1) {
×
200
                $this->consume(1);
×
201
            }
202
            if ($this->parserSettings->usesLenientParsing()) {
×
203
                try {
204
                    $comment = $this->consumeComment();
×
205
                } catch (UnexpectedEOFException $e) {
×
206
                    $this->currentPosition = \count($this->characters);
×
207
                    return $comments;
×
208
                }
209
            } else {
210
                $comment = $this->consumeComment();
×
211
            }
212
            if ($comment !== false) {
×
213
                $comments[] = $comment;
×
214
            }
215
        } while ($comment !== false);
×
216

UNCOV
217
        return $comments;
×
218
    }
219

220
    /**
221
     * @param non-empty-string $string
222
     */
UNCOV
223
    public function comes(string $string, bool $caseInsensitive = false): bool
×
224
    {
225
        $peek = $this->peek(\strlen($string));
×
226

NEW
227
        return ($peek !== '') && $this->streql($peek, $string, $caseInsensitive);
×
228
    }
229

230
    /**
231
     * @param int<1, max> $length
232
     * @param int<0, max> $offset
233
     */
234
    public function peek(int $length = 1, int $offset = 0): string
×
235
    {
236
        $offset += $this->currentPosition;
×
237
        if ($offset >= \count($this->characters)) {
×
238
            return '';
×
239
        }
240

UNCOV
241
        return $this->substr($offset, $length);
×
242
    }
243

244
    /**
245
     * @param string|int<1, max> $value
246
     *
247
     * @throws UnexpectedEOFException
248
     * @throws UnexpectedTokenException
249
     */
250
    public function consume($value = 1): string
×
251
    {
252
        if (\is_string($value)) {
×
253
            $numberOfLines = \substr_count($value, "\n");
×
254
            $length = $this->strlen($value);
×
255
            if (!$this->streql($this->substr($this->currentPosition, $length), $value)) {
×
256
                throw new UnexpectedTokenException(
×
257
                    $value,
×
258
                    $this->peek(\max($length, 5)),
×
259
                    'literal',
×
260
                    $this->lineNumber
×
261
                );
262
            }
263

264
            $this->lineNumber += $numberOfLines;
×
265
            $this->currentPosition += $this->strlen($value);
×
NEW
266
            $result = $value;
×
267
        } else {
268
            if ($this->currentPosition + $value > \count($this->characters)) {
×
269
                throw new UnexpectedEOFException((string) $value, $this->peek(5), 'count', $this->lineNumber);
×
270
            }
271

272
            $result = $this->substr($this->currentPosition, $value);
×
273
            $numberOfLines = \substr_count($result, "\n");
×
274
            $this->lineNumber += $numberOfLines;
×
275
            $this->currentPosition += $value;
×
276
        }
277

NEW
278
        return $result;
×
279
    }
280

281
    /**
282
     * @param string $expression
283
     * @param int<1, max>|null $maximumLength
284
     *
285
     * @throws UnexpectedEOFException
286
     * @throws UnexpectedTokenException
287
     */
288
    public function consumeExpression(string $expression, ?int $maximumLength = null): string
×
289
    {
290
        $matches = null;
×
NEW
291
        $input = ($maximumLength !== null) ? $this->peek($maximumLength) : $this->inputLeft();
×
NEW
292
        if (\preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) {
×
NEW
293
            throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber);
×
294
        }
295

NEW
296
        return $this->consume($matches[0][0]);
×
297
    }
298

299
    /**
300
     * @return Comment|false
301
     */
302
    public function consumeComment()
×
303
    {
304
        $comment = false;
×
305
        if ($this->comes('/*')) {
×
306
            $lineNumber = $this->lineNumber;
×
307
            $this->consume(1);
×
308
            $comment = '';
×
309
            while (($char = $this->consume(1)) !== '') {
×
310
                $comment .= $char;
×
311
                if ($this->comes('*/')) {
×
312
                    $this->consume(2);
×
313
                    break;
×
314
                }
315
            }
316
        }
317

318
        // We skip the * which was included in the comment.
NEW
319
        return ($comment !== false) ? new Comment(\substr($comment, 1), $lineNumber) : false;
×
320
    }
321

322
    public function isEnd(): bool
×
323
    {
324
        return $this->currentPosition >= \count($this->characters);
×
325
    }
326

327
    /**
328
     * @param list<string>|string $stopCharacters
329
     * @param array<int, Comment> $comments
330
     *
331
     * @throws UnexpectedEOFException
332
     * @throws UnexpectedTokenException
333
     */
334
    public function consumeUntil(
×
335
        $stopCharacters,
336
        bool $includeEnd = false,
337
        bool $consumeEnd = false,
338
        array &$comments = []
339
    ): string {
340
        $stopCharacters = \is_array($stopCharacters) ? $stopCharacters : [$stopCharacters];
×
341
        $consumedCharacters = '';
×
342
        $start = $this->currentPosition;
×
343

344
        while (!$this->isEnd()) {
×
345
            $character = $this->consume(1);
×
346
            if (\in_array($character, $stopCharacters, true)) {
×
347
                if ($includeEnd) {
×
348
                    $consumedCharacters .= $character;
×
349
                } elseif (!$consumeEnd) {
×
350
                    $this->currentPosition -= $this->strlen($character);
×
351
                }
352
                return $consumedCharacters;
×
353
            }
354
            $consumedCharacters .= $character;
×
NEW
355
            $comment = $this->consumeComment();
×
NEW
356
            if ($comment instanceof Comment) {
×
UNCOV
357
                $comments[] = $comment;
×
358
            }
359
        }
360

361
        if (\in_array(self::EOF, $stopCharacters, true)) {
×
362
            return $consumedCharacters;
×
363
        }
364

365
        $this->currentPosition = $start;
×
366
        throw new UnexpectedEOFException(
×
367
            'One of ("' . \implode('","', $stopCharacters) . '")',
×
368
            $this->peek(5),
×
369
            'search',
×
370
            $this->lineNumber
×
371
        );
372
    }
373

374
    private function inputLeft(): string
×
375
    {
376
        return $this->substr($this->currentPosition, -1);
×
377
    }
378

379
    public function streql(string $string1, string $string2, bool $caseInsensitive = true): bool
×
380
    {
NEW
381
        return $caseInsensitive
×
NEW
382
            ? ($this->strtolower($string1) === $this->strtolower($string2))
×
NEW
383
            : ($string1 === $string2);
×
384
    }
385

386
    /**
387
     * @param int<1, max> $numberOfCharacters
388
     */
389
    public function backtrack(int $numberOfCharacters): void
×
390
    {
391
        $this->currentPosition -= $numberOfCharacters;
×
392
    }
×
393

394
    /**
395
     * @return int<0, max>
396
     */
397
    public function strlen(string $string): int
×
398
    {
NEW
399
        return $this->parserSettings->hasMultibyteSupport()
×
NEW
400
            ? \mb_strlen($string, $this->charset)
×
NEW
401
            : \strlen($string);
×
402
    }
403

404
    /**
405
     * @param int<0, max> $offset
406
     */
407
    private function substr(int $offset, int $length): string
×
408
    {
409
        if ($length < 0) {
×
410
            $length = \count($this->characters) - $offset + $length;
×
411
        }
412
        if ($offset + $length > \count($this->characters)) {
×
413
            $length = \count($this->characters) - $offset;
×
414
        }
415
        $result = '';
×
416
        while ($length > 0) {
×
417
            $result .= $this->characters[$offset];
×
418
            $offset++;
×
419
            $length--;
×
420
        }
421

UNCOV
422
        return $result;
×
423
    }
424

425
    /**
426
     * @return ($string is non-empty-string ? non-empty-string : string)
427
     */
UNCOV
428
    private function strtolower(string $string): string
×
429
    {
NEW
430
        return $this->parserSettings->hasMultibyteSupport()
×
NEW
431
            ? \mb_strtolower($string, $this->charset)
×
NEW
432
            : \strtolower($string);
×
433
    }
434

435
    /**
436
     * @return list<string>
437
     *
438
     * @throws SourceException if the charset is UTF-8 and the string contains invalid byte sequences
439
     */
440
    private function strsplit(string $string): array
×
441
    {
442
        if ($this->parserSettings->hasMultibyteSupport()) {
×
443
            if ($this->streql($this->charset, 'utf-8')) {
×
444
                $result = \preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
×
445
                if (!\is_array($result)) {
×
446
                    throw new SourceException('`preg_split` failed with error ' . \preg_last_error());
×
447
                }
448
            } else {
UNCOV
449
                $length = \mb_strlen($string, $this->charset);
×
450
                $result = [];
×
451
                for ($i = 0; $i < $length; ++$i) {
×
452
                    $result[] = \mb_substr($string, $i, 1, $this->charset);
×
453
                }
454
            }
455
        } else {
NEW
456
            return ($string !== '') ? \str_split($string) : [];
×
457
        }
458

NEW
459
        return $result;
×
460
    }
461

462
    /**
463
     * @return int<0, max>|false
464
     */
465
    private function strpos(string $haystack, string $needle, int $offset)
×
466
    {
NEW
467
        return $this->parserSettings->hasMultibyteSupport()
×
NEW
468
            ? \mb_strpos($haystack, $needle, $offset, $this->charset)
×
NEW
469
            : \strpos($haystack, $needle, $offset);
×
470
    }
471
}
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