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

MyIntervals / PHP-CSS-Parser / 13672444747

05 Mar 2025 09:14AM UTC coverage: 55.726%. Remained the same
13672444747

Pull #1084

github

web-flow
Merge 135725e8f into 13a118a4d
Pull Request #1084: [TASK] Extract value parsing functional tests (part 1)

1056 of 1895 relevant lines covered (55.73%)

12.24 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
        $sPeek = $this->peek(\strlen($string));
×
235
        return ($sPeek == '')
×
236
            ? false
×
237
            : $this->streql($sPeek, $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
            $iLineCount = \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 += $iLineCount;
×
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
            $iLineCount = \substr_count($result, "\n");
×
281
            $this->lineNumber += $iLineCount;
×
282
            $this->currentPosition += $value;
×
283
            return $result;
×
284
        }
285
    }
286

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

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

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

328
        return $mComment;
×
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 $aEnd
338
     * @param string $bIncludeEnd
339
     * @param string $consumeEnd
340
     * @param array<int, Comment> $comments
341
     *
342
     * @throws UnexpectedEOFException
343
     * @throws UnexpectedTokenException
344
     */
345
    public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, array &$comments = []): string
×
346
    {
347
        $aEnd = \is_array($aEnd) ? $aEnd : [$aEnd];
×
348
        $out = '';
×
349
        $start = $this->currentPosition;
×
350

351
        while (!$this->isEnd()) {
×
352
            $char = $this->consume(1);
×
353
            if (\in_array($char, $aEnd, true)) {
×
354
                if ($bIncludeEnd) {
×
355
                    $out .= $char;
×
356
                } elseif (!$consumeEnd) {
×
357
                    $this->currentPosition -= $this->strlen($char);
×
358
                }
359
                return $out;
×
360
            }
361
            $out .= $char;
×
362
            if ($comment = $this->consumeComment()) {
×
363
                $comments[] = $comment;
×
364
            }
365
        }
366

367
        if (\in_array(self::EOF, $aEnd, true)) {
×
368
            return $out;
×
369
        }
370

371
        $this->currentPosition = $start;
×
372
        throw new UnexpectedEOFException(
×
373
            'One of ("' . \implode('","', $aEnd) . '")',
×
374
            $this->peek(5),
×
375
            'search',
×
376
            $this->lineNumber
×
377
        );
378
    }
379

380
    private function inputLeft(): string
×
381
    {
382
        return $this->substr($this->currentPosition, -1);
×
383
    }
384

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

399
    /**
400
     * @param int $numberOfCharacters
401
     */
402
    public function backtrack($numberOfCharacters): void
×
403
    {
404
        $this->currentPosition -= $numberOfCharacters;
×
405
    }
×
406

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

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

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

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

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