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

MyIntervals / PHP-CSS-Parser / 13344937658

15 Feb 2025 12:18PM UTC coverage: 50.453% (-0.05%) from 50.498%
13344937658

Pull #932

github

web-flow
Merge b7989b0ad into e1fa3b678
Pull Request #932: [TASK] Drop redundant constructor code

947 of 1877 relevant lines covered (50.45%)

11.35 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 = 0;
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->lineNumber = $lineNumber;
×
68
        $this->setCharset($this->oParserSettings->sDefaultCharset);
×
69
    }
×
70

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

359
        return $mComment;
×
360
    }
361

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

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

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

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

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

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

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

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

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

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

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

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

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