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

MyIntervals / PHP-CSS-Parser / 13344956838

15 Feb 2025 12:20PM UTC coverage: 50.42% (-0.08%) from 50.498%
13344956838

Pull #925

github

web-flow
Merge 2ed8ad3a9 into e1fa3b678
Pull Request #925: [TASK] Drop redundant `OutputException` constructor

960 of 1904 relevant lines covered (50.42%)

11.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 $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
        $result = $this->parseCharacter(true);
×
148
        if ($result === null) {
×
149
            throw new UnexpectedTokenException('', $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
                $result .= $sCharacter;
×
155
            } else {
156
                $result .= '\\' . $sCharacter;
×
157
            }
158
        }
159
        if ($bIgnoreCase) {
×
160
            $result = $this->strtolower($result);
×
161
        }
162
        return $result;
×
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(
×
298
                    $mValue,
×
299
                    $this->peek(\max($iLength, 5)),
×
300
                    'literal',
×
301
                    $this->lineNumber
×
302
                );
303
            }
304
            $this->lineNumber += $iLineCount;
×
305
            $this->iCurrentPosition += $this->strlen($mValue);
×
306
            return $mValue;
×
307
        } else {
308
            if ($this->iCurrentPosition + $mValue > $this->iLength) {
×
309
                throw new UnexpectedEOFException((string) $mValue, $this->peek(5), 'count', $this->lineNumber);
×
310
            }
311
            $result = $this->substr($this->iCurrentPosition, $mValue);
×
312
            $iLineCount = \substr_count($result, "\n");
×
313
            $this->lineNumber += $iLineCount;
×
314
            $this->iCurrentPosition += $mValue;
×
315
            return $result;
×
316
        }
317
    }
318

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

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

355
        if ($mComment !== false) {
×
356
            // We skip the * which was included in the comment.
357
            return new Comment(\substr($mComment, 1), $lineNumber);
×
358
        }
359

360
        return $mComment;
×
361
    }
362

363
    public function isEnd(): bool
×
364
    {
365
        return $this->iCurrentPosition >= $this->iLength;
×
366
    }
367

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

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

399
        if (\in_array(self::EOF, $aEnd, true)) {
×
400
            return $out;
×
401
        }
402

403
        $this->iCurrentPosition = $start;
×
404
        throw new UnexpectedEOFException(
×
405
            'One of ("' . \implode('","', $aEnd) . '")',
×
406
            $this->peek(5),
×
407
            'search',
×
408
            $this->lineNumber
×
409
        );
410
    }
411

412
    private function inputLeft(): string
×
413
    {
414
        return $this->substr($this->iCurrentPosition, -1);
×
415
    }
416

417
    /**
418
     * @param string $sString1
419
     * @param string $sString2
420
     * @param bool $bCaseInsensitive
421
     */
422
    public function streql($sString1, $sString2, $bCaseInsensitive = true): bool
×
423
    {
424
        if ($bCaseInsensitive) {
×
425
            return $this->strtolower($sString1) === $this->strtolower($sString2);
×
426
        } else {
427
            return $sString1 === $sString2;
×
428
        }
429
    }
430

431
    /**
432
     * @param int $iAmount
433
     */
434
    public function backtrack($iAmount): void
×
435
    {
436
        $this->iCurrentPosition -= $iAmount;
×
437
    }
×
438

439
    /**
440
     * @param string $sString
441
     */
442
    public function strlen($sString): int
×
443
    {
444
        if ($this->oParserSettings->bMultibyteSupport) {
×
445
            return \mb_strlen($sString, $this->sCharset);
×
446
        } else {
447
            return \strlen($sString);
×
448
        }
449
    }
450

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

472
    /**
473
     * @param string $sString
474
     */
475
    private function strtolower($sString): string
×
476
    {
477
        if ($this->oParserSettings->bMultibyteSupport) {
×
478
            return \mb_strtolower($sString, $this->sCharset);
×
479
        } else {
480
            return \strtolower($sString);
×
481
        }
482
    }
483

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

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