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

MyIntervals / PHP-CSS-Parser / 13354579848

16 Feb 2025 11:29AM UTC coverage: 50.801% (+0.05%) from 50.747%
13354579848

Pull #941

github

web-flow
Merge 8e013bf0c into 0094c4fcb
Pull Request #941: [TASK] Drop the unused `ParserState::getCharset()`

951 of 1872 relevant lines covered (50.8%)

11.41 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 $parserSettings;
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 $parserSettings, $lineNumber = 1)
×
64
    {
65
        $this->parserSettings = $parserSettings;
×
66
        $this->sText = $sText;
×
67
        $this->lineNumber = $lineNumber;
×
68
        $this->setCharset($this->parserSettings->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
     * @return int
87
     */
88
    public function currentLine()
×
89
    {
90
        return $this->lineNumber;
×
91
    }
92

93
    /**
94
     * @return int
95
     */
96
    public function currentColumn()
×
97
    {
98
        return $this->iCurrentPosition;
×
99
    }
100

101
    /**
102
     * @return Settings
103
     */
104
    public function getSettings()
×
105
    {
106
        return $this->parserSettings;
×
107
    }
108

109
    public function anchor(): Anchor
×
110
    {
111
        return new Anchor($this->iCurrentPosition, $this);
×
112
    }
113

114
    /**
115
     * @param int $position
116
     */
117
    public function setPosition($position): void
×
118
    {
119
        $this->iCurrentPosition = $position;
×
120
    }
×
121

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

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

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

249
    /**
250
     * @param string $sString
251
     * @param bool $bCaseInsensitive
252
     */
253
    public function comes($sString, $bCaseInsensitive = false): bool
×
254
    {
255
        $sPeek = $this->peek(\strlen($sString));
×
256
        return ($sPeek == '')
×
257
            ? false
×
258
            : $this->streql($sPeek, $sString, $bCaseInsensitive);
×
259
    }
260

261
    /**
262
     * @param int $iLength
263
     * @param int $iOffset
264
     */
265
    public function peek($iLength = 1, $iOffset = 0): string
×
266
    {
267
        $iOffset += $this->iCurrentPosition;
×
268
        if ($iOffset >= $this->iLength) {
×
269
            return '';
×
270
        }
271
        return $this->substr($iOffset, $iLength);
×
272
    }
273

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

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

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

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

349
        return $mComment;
×
350
    }
351

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

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

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

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

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

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

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

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

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

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

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

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

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