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

MyIntervals / PHP-CSS-Parser / 13270695711

11 Feb 2025 07:13PM UTC coverage: 49.156%. Remained the same
13270695711

Pull #908

github

web-flow
Merge 7d7b5da61 into a5d22be77
Pull Request #908: [TASK] Mark many parsing-related methods as `@internal`

932 of 1896 relevant lines covered (49.16%)

11.42 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
     * @internal since 8.5.2
19
     */
20
    public const EOF = null;
21

22
    /**
23
     * @var Settings
24
     */
25
    private $oParserSettings;
26

27
    /**
28
     * @var string
29
     */
30
    private $sText;
31

32
    /**
33
     * @var array<int, string>
34
     */
35
    private $aText;
36

37
    /**
38
     * @var int
39
     */
40
    private $iCurrentPosition;
41

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

49
    /**
50
     * @var int
51
     */
52
    private $iLength;
53

54
    /**
55
     * @var int
56
     */
57
    private $lineNumber;
58

59
    /**
60
     * @param string $sText the complete CSS as text (i.e., usually the contents of a CSS file)
61
     * @param int<0, max> $lineNumber
62
     */
63
    public function __construct($sText, Settings $oParserSettings, $lineNumber = 1)
×
64
    {
65
        $this->oParserSettings = $oParserSettings;
×
66
        $this->sText = $sText;
×
67
        $this->iCurrentPosition = 0;
×
68
        $this->lineNumber = $lineNumber;
×
69
        $this->setCharset($this->oParserSettings->sDefaultCharset);
×
70
    }
×
71

72
    /**
73
     * Sets the charset to be used if the CSS does not contain an `@charset` declaration.
74
     *
75
     * @param string $sCharset
76
     */
77
    public function setCharset($sCharset): void
×
78
    {
79
        $this->sCharset = $sCharset;
×
80
        $this->aText = $this->strsplit($this->sText);
×
81
        if (\is_array($this->aText)) {
×
82
            $this->iLength = \count($this->aText);
×
83
        }
84
    }
×
85

86
    /**
87
     * Returns the charset that is used if the CSS does not contain an `@charset` declaration.
88
     *
89
     * @return string
90
     */
91
    public function getCharset()
×
92
    {
93
        return $this->sCharset;
×
94
    }
95

96
    /**
97
     * @return int
98
     */
99
    public function currentLine()
×
100
    {
101
        return $this->lineNumber;
×
102
    }
103

104
    /**
105
     * @return int
106
     */
107
    public function currentColumn()
×
108
    {
109
        return $this->iCurrentPosition;
×
110
    }
111

112
    /**
113
     * @return Settings
114
     */
115
    public function getSettings()
×
116
    {
117
        return $this->oParserSettings;
×
118
    }
119

120
    public function anchor(): Anchor
×
121
    {
122
        return new Anchor($this->iCurrentPosition, $this);
×
123
    }
124

125
    /**
126
     * @param int $iPosition
127
     */
128
    public function setPosition($iPosition): void
×
129
    {
130
        $this->iCurrentPosition = $iPosition;
×
131
    }
×
132

133
    /**
134
     * @param bool $bIgnoreCase
135
     *
136
     * @return string
137
     *
138
     * @throws UnexpectedTokenException
139
     *
140
     * @internal since V8.8.0
141
     */
142
    public function parseIdentifier($bIgnoreCase = true)
×
143
    {
144
        if ($this->isEnd()) {
×
145
            throw new UnexpectedEOFException('', '', 'identifier', $this->lineNumber);
×
146
        }
147
        $sResult = $this->parseCharacter(true);
×
148
        if ($sResult === null) {
×
149
            throw new UnexpectedTokenException($sResult, $this->peek(5), 'identifier', $this->lineNumber);
×
150
        }
151
        $sCharacter = null;
×
152
        while (!$this->isEnd() && ($sCharacter = $this->parseCharacter(true)) !== null) {
×
153
            if (\preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $sCharacter)) {
×
154
                $sResult .= $sCharacter;
×
155
            } else {
156
                $sResult .= '\\' . $sCharacter;
×
157
            }
158
        }
159
        if ($bIgnoreCase) {
×
160
            $sResult = $this->strtolower($sResult);
×
161
        }
162
        return $sResult;
×
163
    }
164

165
    /**
166
     * @param bool $bIsForIdentifier
167
     *
168
     * @return string|null
169
     *
170
     * @throws UnexpectedEOFException
171
     * @throws UnexpectedTokenException
172
     *
173
     * @internal since V8.8.0
174
     */
175
    public function parseCharacter($bIsForIdentifier)
×
176
    {
177
        if ($this->peek() === '\\') {
×
178
            if (
179
                $bIsForIdentifier && $this->oParserSettings->bLenientParsing
×
180
                && ($this->comes('\\0') || $this->comes('\\9'))
×
181
            ) {
182
                // Non-strings can contain \0 or \9 which is an IE hack supported in lenient parsing.
183
                return null;
×
184
            }
185
            $this->consume('\\');
×
186
            if ($this->comes('\\n') || $this->comes('\\r')) {
×
187
                return '';
×
188
            }
189
            if (\preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
×
190
                return $this->consume(1);
×
191
            }
192
            $sUnicode = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6);
×
193
            if ($this->strlen($sUnicode) < 6) {
×
194
                // Consume whitespace after incomplete unicode escape
195
                if (\preg_match('/\\s/isSu', $this->peek())) {
×
196
                    if ($this->comes('\\r\\n')) {
×
197
                        $this->consume(2);
×
198
                    } else {
199
                        $this->consume(1);
×
200
                    }
201
                }
202
            }
203
            $iUnicode = \intval($sUnicode, 16);
×
204
            $sUtf32 = '';
×
205
            for ($i = 0; $i < 4; ++$i) {
×
206
                $sUtf32 .= \chr($iUnicode & 0xff);
×
207
                $iUnicode = $iUnicode >> 8;
×
208
            }
209
            return \iconv('utf-32le', $this->sCharset, $sUtf32);
×
210
        }
211
        if ($bIsForIdentifier) {
×
212
            $peek = \ord($this->peek());
×
213
            // Ranges: a-z A-Z 0-9 - _
214
            if (
215
                ($peek >= 97 && $peek <= 122)
×
216
                || ($peek >= 65 && $peek <= 90)
×
217
                || ($peek >= 48 && $peek <= 57)
×
218
                || ($peek === 45)
×
219
                || ($peek === 95)
×
220
                || ($peek > 0xa1)
×
221
            ) {
222
                return $this->consume(1);
×
223
            }
224
        } else {
225
            return $this->consume(1);
×
226
        }
227
        return null;
×
228
    }
229

230
    /**
231
     * @return array<int, Comment>|void
232
     *
233
     * @throws UnexpectedEOFException
234
     * @throws UnexpectedTokenException
235
     */
236
    public function consumeWhiteSpace(): array
×
237
    {
238
        $comments = [];
×
239
        do {
240
            while (\preg_match('/\\s/isSu', $this->peek()) === 1) {
×
241
                $this->consume(1);
×
242
            }
243
            if ($this->oParserSettings->bLenientParsing) {
×
244
                try {
245
                    $oComment = $this->consumeComment();
×
246
                } catch (UnexpectedEOFException $e) {
×
247
                    $this->iCurrentPosition = $this->iLength;
×
248
                    return $comments;
×
249
                }
250
            } else {
251
                $oComment = $this->consumeComment();
×
252
            }
253
            if ($oComment !== false) {
×
254
                $comments[] = $oComment;
×
255
            }
256
        } while ($oComment !== false);
×
257
        return $comments;
×
258
    }
259

260
    /**
261
     * @param string $sString
262
     * @param bool $bCaseInsensitive
263
     */
264
    public function comes($sString, $bCaseInsensitive = false): bool
×
265
    {
266
        $sPeek = $this->peek(\strlen($sString));
×
267
        return ($sPeek == '')
×
268
            ? false
×
269
            : $this->streql($sPeek, $sString, $bCaseInsensitive);
×
270
    }
271

272
    /**
273
     * @param int $iLength
274
     * @param int $iOffset
275
     */
276
    public function peek($iLength = 1, $iOffset = 0): string
×
277
    {
278
        $iOffset += $this->iCurrentPosition;
×
279
        if ($iOffset >= $this->iLength) {
×
280
            return '';
×
281
        }
282
        return $this->substr($iOffset, $iLength);
×
283
    }
284

285
    /**
286
     * @param int $mValue
287
     *
288
     * @throws UnexpectedEOFException
289
     * @throws UnexpectedTokenException
290
     */
291
    public function consume($mValue = 1): string
×
292
    {
293
        if (\is_string($mValue)) {
×
294
            $iLineCount = \substr_count($mValue, "\n");
×
295
            $iLength = $this->strlen($mValue);
×
296
            if (!$this->streql($this->substr($this->iCurrentPosition, $iLength), $mValue)) {
×
297
                throw new UnexpectedTokenException($mValue, $this->peek(\max($iLength, 5)), $this->lineNumber);
×
298
            }
299
            $this->lineNumber += $iLineCount;
×
300
            $this->iCurrentPosition += $this->strlen($mValue);
×
301
            return $mValue;
×
302
        } else {
303
            if ($this->iCurrentPosition + $mValue > $this->iLength) {
×
304
                throw new UnexpectedEOFException($mValue, $this->peek(5), 'count', $this->lineNumber);
×
305
            }
306
            $sResult = $this->substr($this->iCurrentPosition, $mValue);
×
307
            $iLineCount = \substr_count($sResult, "\n");
×
308
            $this->lineNumber += $iLineCount;
×
309
            $this->iCurrentPosition += $mValue;
×
310
            return $sResult;
×
311
        }
312
    }
313

314
    /**
315
     * @param string $mExpression
316
     * @param int|null $iMaxLength
317
     *
318
     * @throws UnexpectedEOFException
319
     * @throws UnexpectedTokenException
320
     */
321
    public function consumeExpression($mExpression, $iMaxLength = null): string
×
322
    {
323
        $aMatches = null;
×
324
        $sInput = $iMaxLength !== null ? $this->peek($iMaxLength) : $this->inputLeft();
×
325
        if (\preg_match($mExpression, $sInput, $aMatches, PREG_OFFSET_CAPTURE) === 1) {
×
326
            return $this->consume($aMatches[0][0]);
×
327
        }
328
        throw new UnexpectedTokenException($mExpression, $this->peek(5), 'expression', $this->lineNumber);
×
329
    }
330

331
    /**
332
     * @return Comment|false
333
     */
334
    public function consumeComment()
×
335
    {
336
        $mComment = false;
×
337
        if ($this->comes('/*')) {
×
338
            $lineNumber = $this->lineNumber;
×
339
            $this->consume(1);
×
340
            $mComment = '';
×
341
            while (($char = $this->consume(1)) !== '') {
×
342
                $mComment .= $char;
×
343
                if ($this->comes('*/')) {
×
344
                    $this->consume(2);
×
345
                    break;
×
346
                }
347
            }
348
        }
349

350
        if ($mComment !== false) {
×
351
            // We skip the * which was included in the comment.
352
            return new Comment(\substr($mComment, 1), $lineNumber);
×
353
        }
354

355
        return $mComment;
×
356
    }
357

358
    public function isEnd(): bool
×
359
    {
360
        return $this->iCurrentPosition >= $this->iLength;
×
361
    }
362

363
    /**
364
     * @param array<array-key, string>|string $aEnd
365
     * @param string $bIncludeEnd
366
     * @param string $consumeEnd
367
     * @param array<int, Comment> $comments
368
     *
369
     * @throws UnexpectedEOFException
370
     * @throws UnexpectedTokenException
371
     */
372
    public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, array &$comments = []): string
×
373
    {
374
        $aEnd = \is_array($aEnd) ? $aEnd : [$aEnd];
×
375
        $out = '';
×
376
        $start = $this->iCurrentPosition;
×
377

378
        while (!$this->isEnd()) {
×
379
            $char = $this->consume(1);
×
380
            if (\in_array($char, $aEnd, true)) {
×
381
                if ($bIncludeEnd) {
×
382
                    $out .= $char;
×
383
                } elseif (!$consumeEnd) {
×
384
                    $this->iCurrentPosition -= $this->strlen($char);
×
385
                }
386
                return $out;
×
387
            }
388
            $out .= $char;
×
389
            if ($comment = $this->consumeComment()) {
×
390
                $comments[] = $comment;
×
391
            }
392
        }
393

394
        if (\in_array(self::EOF, $aEnd, true)) {
×
395
            return $out;
×
396
        }
397

398
        $this->iCurrentPosition = $start;
×
399
        throw new UnexpectedEOFException(
×
400
            'One of ("' . \implode('","', $aEnd) . '")',
×
401
            $this->peek(5),
×
402
            'search',
×
403
            $this->lineNumber
×
404
        );
405
    }
406

407
    private function inputLeft(): string
×
408
    {
409
        return $this->substr($this->iCurrentPosition, -1);
×
410
    }
411

412
    /**
413
     * @param string $sString1
414
     * @param string $sString2
415
     * @param bool $bCaseInsensitive
416
     */
417
    public function streql($sString1, $sString2, $bCaseInsensitive = true): bool
×
418
    {
419
        if ($bCaseInsensitive) {
×
420
            return $this->strtolower($sString1) === $this->strtolower($sString2);
×
421
        } else {
422
            return $sString1 === $sString2;
×
423
        }
424
    }
425

426
    /**
427
     * @param int $iAmount
428
     */
429
    public function backtrack($iAmount): void
×
430
    {
431
        $this->iCurrentPosition -= $iAmount;
×
432
    }
×
433

434
    /**
435
     * @param string $sString
436
     */
437
    public function strlen($sString): int
×
438
    {
439
        if ($this->oParserSettings->bMultibyteSupport) {
×
440
            return \mb_strlen($sString, $this->sCharset);
×
441
        } else {
442
            return \strlen($sString);
×
443
        }
444
    }
445

446
    /**
447
     * @param int $iStart
448
     * @param int $iLength
449
     */
450
    private function substr($iStart, $iLength): string
×
451
    {
452
        if ($iLength < 0) {
×
453
            $iLength = $this->iLength - $iStart + $iLength;
×
454
        }
455
        if ($iStart + $iLength > $this->iLength) {
×
456
            $iLength = $this->iLength - $iStart;
×
457
        }
458
        $sResult = '';
×
459
        while ($iLength > 0) {
×
460
            $sResult .= $this->aText[$iStart];
×
461
            $iStart++;
×
462
            $iLength--;
×
463
        }
464
        return $sResult;
×
465
    }
466

467
    /**
468
     * @param string $sString
469
     */
470
    private function strtolower($sString): string
×
471
    {
472
        if ($this->oParserSettings->bMultibyteSupport) {
×
473
            return \mb_strtolower($sString, $this->sCharset);
×
474
        } else {
475
            return \strtolower($sString);
×
476
        }
477
    }
478

479
    /**
480
     * @param string $sString
481
     *
482
     * @return array<int, string>
483
     */
484
    private function strsplit($sString)
×
485
    {
486
        if ($this->oParserSettings->bMultibyteSupport) {
×
487
            if ($this->streql($this->sCharset, 'utf-8')) {
×
488
                return \preg_split('//u', $sString, -1, PREG_SPLIT_NO_EMPTY);
×
489
            } else {
490
                $iLength = \mb_strlen($sString, $this->sCharset);
×
491
                $aResult = [];
×
492
                for ($i = 0; $i < $iLength; ++$i) {
×
493
                    $aResult[] = \mb_substr($sString, $i, 1, $this->sCharset);
×
494
                }
495
                return $aResult;
×
496
            }
497
        } else {
498
            if ($sString === '') {
×
499
                return [];
×
500
            } else {
501
                return \str_split($sString);
×
502
            }
503
        }
504
    }
505

506
    /**
507
     * @param string $sString
508
     * @param string $sNeedle
509
     * @param int $iOffset
510
     *
511
     * @return int|false
512
     */
513
    private function strpos($sString, $sNeedle, $iOffset)
×
514
    {
515
        if ($this->oParserSettings->bMultibyteSupport) {
×
516
            return \mb_strpos($sString, $sNeedle, $iOffset, $this->sCharset);
×
517
        } else {
518
            return \strpos($sString, $sNeedle, $iOffset);
×
519
        }
520
    }
521
}
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