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

MyIntervals / PHP-CSS-Parser / 13886296170

16 Mar 2025 06:39PM UTC coverage: 55.906% (+0.2%) from 55.727%
13886296170

push

github

web-flow
[CLEANUP] Clean up `ParserState` a bit (#1173)

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

9 existing lines in 1 file now uncovered.

1046 of 1871 relevant lines covered (55.91%)

12.68 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);
×
NEW
207
                    break;
×
208
                }
209
            } else {
210
                $comment = $this->consumeComment();
×
211
            }
NEW
212
            if ($comment instanceof Comment) {
×
213
                $comments[] = $comment;
×
214
            }
NEW
215
        } while ($comment instanceof Comment);
×
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
    {
NEW
304
        $lineNumber = $this->lineNumber;
×
NEW
305
        $comment = null;
×
306

307
        if ($this->comes('/*')) {
×
308
            $this->consume(1);
×
309
            $comment = '';
×
310
            while (($char = $this->consume(1)) !== '') {
×
311
                $comment .= $char;
×
312
                if ($this->comes('*/')) {
×
313
                    $this->consume(2);
×
314
                    break;
×
315
                }
316
            }
317
        }
318

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

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

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

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

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

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

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

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

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

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

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

UNCOV
423
        return $result;
×
424
    }
425

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

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

NEW
460
        return $result;
×
461
    }
462

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

© 2025 Coveralls, Inc