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

MyIntervals / PHP-CSS-Parser / 12973829822

26 Jan 2025 10:32AM UTC coverage: 42.018% (-0.2%) from 42.261%
12973829822

Pull #809

github

web-flow
Merge 1b26da12f into 7c6845f83
Pull Request #809: [BUGFIX] Drop `@covers` annotation referencing inexistent method

866 of 2061 relevant lines covered (42.02%)

6.56 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 $iLineNo;
58

59
    /**
60
     * @param string $sText the complete CSS as text (i.e., usually the contents of a CSS file)
61
     * @param int $iLineNo
62
     */
63
    public function __construct($sText, Settings $oParserSettings, $iLineNo = 1)
×
64
    {
65
        $this->oParserSettings = $oParserSettings;
×
66
        $this->sText = $sText;
×
67
        $this->iCurrentPosition = 0;
×
68
        $this->iLineNo = $iLineNo;
×
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->iLineNo;
×
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
    public function parseIdentifier($bIgnoreCase = true)
×
141
    {
142
        if ($this->isEnd()) {
×
143
            throw new UnexpectedEOFException('', '', 'identifier', $this->iLineNo);
×
144
        }
145
        $sResult = $this->parseCharacter(true);
×
146
        if ($sResult === null) {
×
147
            throw new UnexpectedTokenException($sResult, $this->peek(5), 'identifier', $this->iLineNo);
×
148
        }
149
        $sCharacter = null;
×
150
        while (!$this->isEnd() && ($sCharacter = $this->parseCharacter(true)) !== null) {
×
151
            if (\preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $sCharacter)) {
×
152
                $sResult .= $sCharacter;
×
153
            } else {
154
                $sResult .= '\\' . $sCharacter;
×
155
            }
156
        }
157
        if ($bIgnoreCase) {
×
158
            $sResult = $this->strtolower($sResult);
×
159
        }
160
        return $sResult;
×
161
    }
162

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

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

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

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

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

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

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

346
        if ($mComment !== false) {
×
347
            // We skip the * which was included in the comment.
348
            return new Comment(\substr($mComment, 1), $iLineNo);
×
349
        }
350

351
        return $mComment;
×
352
    }
353

354
    public function isEnd(): bool
×
355
    {
356
        return $this->iCurrentPosition >= $this->iLength;
×
357
    }
358

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

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

390
        if (\in_array(self::EOF, $aEnd, true)) {
×
391
            return $out;
×
392
        }
393

394
        $this->iCurrentPosition = $start;
×
395
        throw new UnexpectedEOFException(
×
396
            'One of ("' . \implode('","', $aEnd) . '")',
×
397
            $this->peek(5),
×
398
            'search',
×
399
            $this->iLineNo
×
400
        );
401
    }
402

403
    private function inputLeft(): string
×
404
    {
405
        return $this->substr($this->iCurrentPosition, -1);
×
406
    }
407

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

422
    /**
423
     * @param int $iAmount
424
     */
425
    public function backtrack($iAmount): void
×
426
    {
427
        $this->iCurrentPosition -= $iAmount;
×
428
    }
×
429

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

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

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

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

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