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

MyIntervals / PHP-CSS-Parser / 13767045574

10 Mar 2025 02:24PM UTC coverage: 55.567%. Remained the same
13767045574

Pull #1128

github

web-flow
Merge 0b68ff170 into 9d2fd8e40
Pull Request #1128: [TASK] Make `OutputFormat` `final`

1038 of 1868 relevant lines covered (55.57%)

12.37 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
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
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<0, max> $lineNumber
55
     */
56
    public function __construct($text, Settings $parserSettings, $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
77
     */
78
    public function currentLine()
×
79
    {
80
        return $this->lineNumber;
×
81
    }
82

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

91
    /**
92
     * @return Settings
93
     */
94
    public function getSettings()
×
95
    {
96
        return $this->parserSettings;
×
97
    }
98

99
    public function anchor(): Anchor
×
100
    {
101
        return new Anchor($this->currentPosition, $this);
×
102
    }
103

104
    /**
105
     * @param int $position
106
     */
107
    public function setPosition($position): void
×
108
    {
109
        $this->currentPosition = $position;
×
110
    }
×
111

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

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

198
    /**
199
     * @return array<int, Comment>|void
200
     *
201
     * @throws UnexpectedEOFException
202
     * @throws UnexpectedTokenException
203
     */
204
    public function consumeWhiteSpace(): array
×
205
    {
206
        $comments = [];
×
207
        do {
208
            while (\preg_match('/\\s/isSu', $this->peek()) === 1) {
×
209
                $this->consume(1);
×
210
            }
211
            if ($this->parserSettings->usesLenientParsing()) {
×
212
                try {
213
                    $comment = $this->consumeComment();
×
214
                } catch (UnexpectedEOFException $e) {
×
215
                    $this->currentPosition = \count($this->characters);
×
216
                    return $comments;
×
217
                }
218
            } else {
219
                $comment = $this->consumeComment();
×
220
            }
221
            if ($comment !== false) {
×
222
                $comments[] = $comment;
×
223
            }
224
        } while ($comment !== false);
×
225
        return $comments;
×
226
    }
227

228
    /**
229
     * @param string $string
230
     * @param bool $caseInsensitive
231
     */
232
    public function comes($string, $caseInsensitive = false): bool
×
233
    {
234
        $peek = $this->peek(\strlen($string));
×
235
        return ($peek == '')
×
236
            ? false
×
237
            : $this->streql($peek, $string, $caseInsensitive);
×
238
    }
239

240
    /**
241
     * @param int $length
242
     * @param int $offset
243
     */
244
    public function peek($length = 1, $offset = 0): string
×
245
    {
246
        $offset += $this->currentPosition;
×
247
        if ($offset >= \count($this->characters)) {
×
248
            return '';
×
249
        }
250
        return $this->substr($offset, $length);
×
251
    }
252

253
    /**
254
     * @param int $value
255
     *
256
     * @throws UnexpectedEOFException
257
     * @throws UnexpectedTokenException
258
     */
259
    public function consume($value = 1): string
×
260
    {
261
        if (\is_string($value)) {
×
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
            $this->lineNumber += $numberOfLines;
×
273
            $this->currentPosition += $this->strlen($value);
×
274
            return $value;
×
275
        } else {
276
            if ($this->currentPosition + $value > \count($this->characters)) {
×
277
                throw new UnexpectedEOFException((string) $value, $this->peek(5), 'count', $this->lineNumber);
×
278
            }
279
            $result = $this->substr($this->currentPosition, $value);
×
280
            $numberOfLines = \substr_count($result, "\n");
×
281
            $this->lineNumber += $numberOfLines;
×
282
            $this->currentPosition += $value;
×
283
            return $result;
×
284
        }
285
    }
286

287
    /**
288
     * @param string $expression
289
     * @param int|null $maximumLength
290
     *
291
     * @throws UnexpectedEOFException
292
     * @throws UnexpectedTokenException
293
     */
294
    public function consumeExpression($expression, $maximumLength = null): string
×
295
    {
296
        $matches = null;
×
297
        $input = $maximumLength !== null ? $this->peek($maximumLength) : $this->inputLeft();
×
298
        if (\preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) === 1) {
×
299
            return $this->consume($matches[0][0]);
×
300
        }
301
        throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber);
×
302
    }
303

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

323
        if ($comment !== false) {
×
324
            // We skip the * which was included in the comment.
325
            return new Comment(\substr($comment, 1), $lineNumber);
×
326
        }
327

328
        return $comment;
×
329
    }
330

331
    public function isEnd(): bool
×
332
    {
333
        return $this->currentPosition >= \count($this->characters);
×
334
    }
335

336
    /**
337
     * @param array<array-key, string>|string $stopCharacters
338
     * @param bool $includeEnd
339
     * @param bool $consumeEnd
340
     * @param array<int, Comment> $comments
341
     *
342
     * @throws UnexpectedEOFException
343
     * @throws UnexpectedTokenException
344
     */
345
    public function consumeUntil(
×
346
        $stopCharacters,
347
        $includeEnd = false,
348
        $consumeEnd = false,
349
        array &$comments = []
350
    ): string {
351
        $stopCharacters = \is_array($stopCharacters) ? $stopCharacters : [$stopCharacters];
×
352
        $consumedCharacters = '';
×
353
        $start = $this->currentPosition;
×
354

355
        while (!$this->isEnd()) {
×
356
            $character = $this->consume(1);
×
357
            if (\in_array($character, $stopCharacters, true)) {
×
358
                if ($includeEnd) {
×
359
                    $consumedCharacters .= $character;
×
360
                } elseif (!$consumeEnd) {
×
361
                    $this->currentPosition -= $this->strlen($character);
×
362
                }
363
                return $consumedCharacters;
×
364
            }
365
            $consumedCharacters .= $character;
×
366
            if ($comment = $this->consumeComment()) {
×
367
                $comments[] = $comment;
×
368
            }
369
        }
370

371
        if (\in_array(self::EOF, $stopCharacters, true)) {
×
372
            return $consumedCharacters;
×
373
        }
374

375
        $this->currentPosition = $start;
×
376
        throw new UnexpectedEOFException(
×
377
            'One of ("' . \implode('","', $stopCharacters) . '")',
×
378
            $this->peek(5),
×
379
            'search',
×
380
            $this->lineNumber
×
381
        );
382
    }
383

384
    private function inputLeft(): string
×
385
    {
386
        return $this->substr($this->currentPosition, -1);
×
387
    }
388

389
    /**
390
     * @param string $string1
391
     * @param string $string2
392
     * @param bool $caseInsensitive
393
     */
394
    public function streql($string1, $string2, $caseInsensitive = true): bool
×
395
    {
396
        if ($caseInsensitive) {
×
397
            return $this->strtolower($string1) === $this->strtolower($string2);
×
398
        } else {
399
            return $string1 === $string2;
×
400
        }
401
    }
402

403
    /**
404
     * @param int $numberOfCharacters
405
     */
406
    public function backtrack($numberOfCharacters): void
×
407
    {
408
        $this->currentPosition -= $numberOfCharacters;
×
409
    }
×
410

411
    /**
412
     * @param string $string
413
     */
414
    public function strlen($string): int
×
415
    {
416
        if ($this->parserSettings->hasMultibyteSupport()) {
×
417
            return \mb_strlen($string, $this->charset);
×
418
        } else {
419
            return \strlen($string);
×
420
        }
421
    }
422

423
    /**
424
     * @param int $offset
425
     * @param int $length
426
     */
427
    private function substr($offset, $length): string
×
428
    {
429
        if ($length < 0) {
×
430
            $length = \count($this->characters) - $offset + $length;
×
431
        }
432
        if ($offset + $length > \count($this->characters)) {
×
433
            $length = \count($this->characters) - $offset;
×
434
        }
435
        $result = '';
×
436
        while ($length > 0) {
×
437
            $result .= $this->characters[$offset];
×
438
            $offset++;
×
439
            $length--;
×
440
        }
441
        return $result;
×
442
    }
443

444
    /**
445
     * @param string $string
446
     */
447
    private function strtolower($string): string
×
448
    {
449
        if ($this->parserSettings->hasMultibyteSupport()) {
×
450
            return \mb_strtolower($string, $this->charset);
×
451
        } else {
452
            return \strtolower($string);
×
453
        }
454
    }
455

456
    /**
457
     * @param string $string
458
     *
459
     * @return array<int, string>
460
     *
461
     * @throws SourceException if the charset is UTF-8 and the string contains invalid byte sequences
462
     */
463
    private function strsplit($string)
×
464
    {
465
        if ($this->parserSettings->hasMultibyteSupport()) {
×
466
            if ($this->streql($this->charset, 'utf-8')) {
×
467
                $result = \preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
×
468
                if (!\is_array($result)) {
×
469
                    throw new SourceException('`preg_split` failed with error ' . \preg_last_error());
×
470
                }
471
                return $result;
×
472
            } else {
473
                $length = \mb_strlen($string, $this->charset);
×
474
                $result = [];
×
475
                for ($i = 0; $i < $length; ++$i) {
×
476
                    $result[] = \mb_substr($string, $i, 1, $this->charset);
×
477
                }
478
                return $result;
×
479
            }
480
        } else {
481
            if ($string === '') {
×
482
                return [];
×
483
            } else {
484
                return \str_split($string);
×
485
            }
486
        }
487
    }
488

489
    /**
490
     * @param string $haystack
491
     * @param string $needle
492
     * @param int $offset
493
     *
494
     * @return int|false
495
     */
496
    private function strpos($haystack, $needle, $offset)
×
497
    {
498
        if ($this->parserSettings->hasMultibyteSupport()) {
×
499
            return \mb_strpos($haystack, $needle, $offset, $this->charset);
×
500
        } else {
501
            return \strpos($haystack, $needle, $offset);
×
502
        }
503
    }
504
}
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