ParserState.php
14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
namespace Sabberworm\CSS\Parsing;
use Sabberworm\CSS\Comment\Comment;
use Sabberworm\CSS\Settings;
class ParserState
{
/**
* @var null
*/
const EOF = null;
/**
* @var Settings
*/
private $oParserSettings;
/**
* @var string
*/
private $sText;
/**
* @var array<int, string>
*/
private $aText;
/**
* @var int
*/
private $iCurrentPosition;
/**
* will only be used if the CSS does not contain an `@charset` declaration
*
* @var string
*/
private $sCharset;
/**
* @var int
*/
private $iLength;
/**
* @var int
*/
private $iLineNo;
/**
* @param string $sText the complete CSS as text (i.e., usually the contents of a CSS file)
* @param int $iLineNo
*/
public function __construct($sText, Settings $oParserSettings, $iLineNo = 1)
{
$this->oParserSettings = $oParserSettings;
$this->sText = $sText;
$this->iCurrentPosition = 0;
$this->iLineNo = $iLineNo;
$this->setCharset($this->oParserSettings->sDefaultCharset);
}
/**
* Sets the charset to be used if the CSS does not contain an `@charset` declaration.
*
* @param string $sCharset
*
* @return void
*/
public function setCharset($sCharset)
{
$this->sCharset = $sCharset;
$this->aText = $this->strsplit($this->sText);
if (is_array($this->aText)) {
$this->iLength = count($this->aText);
}
}
/**
* Returns the charset that is used if the CSS does not contain an `@charset` declaration.
*
* @return string
*/
public function getCharset()
{
return $this->sCharset;
}
/**
* @return int
*/
public function currentLine()
{
return $this->iLineNo;
}
/**
* @return int
*/
public function currentColumn()
{
return $this->iCurrentPosition;
}
/**
* @return Settings
*/
public function getSettings()
{
return $this->oParserSettings;
}
/**
* @return \Sabberworm\CSS\Parsing\Anchor
*/
public function anchor()
{
return new Anchor($this->iCurrentPosition, $this);
}
/**
* @param int $iPosition
*
* @return void
*/
public function setPosition($iPosition)
{
$this->iCurrentPosition = $iPosition;
}
/**
* @param bool $bIgnoreCase
*
* @return string
*
* @throws UnexpectedTokenException
*/
public function parseIdentifier($bIgnoreCase = true)
{
if ($this->isEnd()) {
throw new UnexpectedEOFException('', '', 'identifier', $this->iLineNo);
}
$sResult = $this->parseCharacter(true);
if ($sResult === null) {
throw new UnexpectedTokenException($sResult, $this->peek(5), 'identifier', $this->iLineNo);
}
$sCharacter = null;
while (!$this->isEnd() && ($sCharacter = $this->parseCharacter(true)) !== null) {
if (preg_match('/[a-zA-Z0-9\x{00A0}-\x{FFFF}_-]/Sux', $sCharacter)) {
$sResult .= $sCharacter;
} else {
$sResult .= '\\' . $sCharacter;
}
}
if ($bIgnoreCase) {
$sResult = $this->strtolower($sResult);
}
return $sResult;
}
/**
* @param bool $bIsForIdentifier
*
* @return string|null
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function parseCharacter($bIsForIdentifier)
{
if ($this->peek() === '\\') {
if (
$bIsForIdentifier && $this->oParserSettings->bLenientParsing
&& ($this->comes('\0') || $this->comes('\9'))
) {
// Non-strings can contain \0 or \9 which is an IE hack supported in lenient parsing.
return null;
}
$this->consume('\\');
if ($this->comes('\n') || $this->comes('\r')) {
return '';
}
if (preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
return $this->consume(1);
}
$sUnicode = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6);
if ($this->strlen($sUnicode) < 6) {
// Consume whitespace after incomplete unicode escape
if (preg_match('/\\s/isSu', $this->peek())) {
if ($this->comes('\r\n')) {
$this->consume(2);
} else {
$this->consume(1);
}
}
}
$iUnicode = intval($sUnicode, 16);
$sUtf32 = "";
for ($i = 0; $i < 4; ++$i) {
$sUtf32 .= chr($iUnicode & 0xff);
$iUnicode = $iUnicode >> 8;
}
return iconv('utf-32le', $this->sCharset, $sUtf32);
}
if ($bIsForIdentifier) {
$peek = ord($this->peek());
// Ranges: a-z A-Z 0-9 - _
if (
($peek >= 97 && $peek <= 122)
|| ($peek >= 65 && $peek <= 90)
|| ($peek >= 48 && $peek <= 57)
|| ($peek === 45)
|| ($peek === 95)
|| ($peek > 0xa1)
) {
return $this->consume(1);
}
} else {
return $this->consume(1);
}
return null;
}
/**
* @return array<int, Comment>|void
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function consumeWhiteSpace()
{
$aComments = [];
do {
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
$this->consume(1);
}
if ($this->oParserSettings->bLenientParsing) {
try {
$oComment = $this->consumeComment();
} catch (UnexpectedEOFException $e) {
$this->iCurrentPosition = $this->iLength;
return $aComments;
}
} else {
$oComment = $this->consumeComment();
}
if ($oComment !== false) {
$aComments[] = $oComment;
}
} while ($oComment !== false);
return $aComments;
}
/**
* @param string $sString
* @param bool $bCaseInsensitive
*
* @return bool
*/
public function comes($sString, $bCaseInsensitive = false)
{
$sPeek = $this->peek(strlen($sString));
return ($sPeek == '')
? false
: $this->streql($sPeek, $sString, $bCaseInsensitive);
}
/**
* @param int $iLength
* @param int $iOffset
*
* @return string
*/
public function peek($iLength = 1, $iOffset = 0)
{
$iOffset += $this->iCurrentPosition;
if ($iOffset >= $this->iLength) {
return '';
}
return $this->substr($iOffset, $iLength);
}
/**
* @param int $mValue
*
* @return string
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function consume($mValue = 1)
{
if (is_string($mValue)) {
$iLineCount = substr_count($mValue, "\n");
$iLength = $this->strlen($mValue);
if (!$this->streql($this->substr($this->iCurrentPosition, $iLength), $mValue)) {
throw new UnexpectedTokenException($mValue, $this->peek(max($iLength, 5)), $this->iLineNo);
}
$this->iLineNo += $iLineCount;
$this->iCurrentPosition += $this->strlen($mValue);
return $mValue;
} else {
if ($this->iCurrentPosition + $mValue > $this->iLength) {
throw new UnexpectedEOFException($mValue, $this->peek(5), 'count', $this->iLineNo);
}
$sResult = $this->substr($this->iCurrentPosition, $mValue);
$iLineCount = substr_count($sResult, "\n");
$this->iLineNo += $iLineCount;
$this->iCurrentPosition += $mValue;
return $sResult;
}
}
/**
* @param string $mExpression
* @param int|null $iMaxLength
*
* @return string
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function consumeExpression($mExpression, $iMaxLength = null)
{
$aMatches = null;
$sInput = $iMaxLength !== null ? $this->peek($iMaxLength) : $this->inputLeft();
if (preg_match($mExpression, $sInput, $aMatches, PREG_OFFSET_CAPTURE) === 1) {
return $this->consume($aMatches[0][0]);
}
throw new UnexpectedTokenException($mExpression, $this->peek(5), 'expression', $this->iLineNo);
}
/**
* @return Comment|false
*/
public function consumeComment()
{
$mComment = false;
if ($this->comes('/*')) {
$iLineNo = $this->iLineNo;
$this->consume(1);
$mComment = '';
while (($char = $this->consume(1)) !== '') {
$mComment .= $char;
if ($this->comes('*/')) {
$this->consume(2);
break;
}
}
}
if ($mComment !== false) {
// We skip the * which was included in the comment.
return new Comment(substr($mComment, 1), $iLineNo);
}
return $mComment;
}
/**
* @return bool
*/
public function isEnd()
{
return $this->iCurrentPosition >= $this->iLength;
}
/**
* @param array<array-key, string>|string $aEnd
* @param string $bIncludeEnd
* @param string $consumeEnd
* @param array<int, Comment> $comments
*
* @return string
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, array &$comments = [])
{
$aEnd = is_array($aEnd) ? $aEnd : [$aEnd];
$out = '';
$start = $this->iCurrentPosition;
while (!$this->isEnd()) {
$char = $this->consume(1);
if (in_array($char, $aEnd)) {
if ($bIncludeEnd) {
$out .= $char;
} elseif (!$consumeEnd) {
$this->iCurrentPosition -= $this->strlen($char);
}
return $out;
}
$out .= $char;
if ($comment = $this->consumeComment()) {
$comments[] = $comment;
}
}
if (in_array(self::EOF, $aEnd)) {
return $out;
}
$this->iCurrentPosition = $start;
throw new UnexpectedEOFException(
'One of ("' . implode('","', $aEnd) . '")',
$this->peek(5),
'search',
$this->iLineNo
);
}
/**
* @return string
*/
private function inputLeft()
{
return $this->substr($this->iCurrentPosition, -1);
}
/**
* @param string $sString1
* @param string $sString2
* @param bool $bCaseInsensitive
*
* @return bool
*/
public function streql($sString1, $sString2, $bCaseInsensitive = true)
{
if ($bCaseInsensitive) {
return $this->strtolower($sString1) === $this->strtolower($sString2);
} else {
return $sString1 === $sString2;
}
}
/**
* @param int $iAmount
*
* @return void
*/
public function backtrack($iAmount)
{
$this->iCurrentPosition -= $iAmount;
}
/**
* @param string $sString
*
* @return int
*/
public function strlen($sString)
{
if ($this->oParserSettings->bMultibyteSupport) {
return mb_strlen($sString, $this->sCharset);
} else {
return strlen($sString);
}
}
/**
* @param int $iStart
* @param int $iLength
*
* @return string
*/
private function substr($iStart, $iLength)
{
if ($iLength < 0) {
$iLength = $this->iLength - $iStart + $iLength;
}
if ($iStart + $iLength > $this->iLength) {
$iLength = $this->iLength - $iStart;
}
$sResult = '';
while ($iLength > 0) {
$sResult .= $this->aText[$iStart];
$iStart++;
$iLength--;
}
return $sResult;
}
/**
* @param string $sString
*
* @return string
*/
private function strtolower($sString)
{
if ($this->oParserSettings->bMultibyteSupport) {
return mb_strtolower($sString, $this->sCharset);
} else {
return strtolower($sString);
}
}
/**
* @param string $sString
*
* @return array<int, string>
*/
private function strsplit($sString)
{
if ($this->oParserSettings->bMultibyteSupport) {
if ($this->streql($this->sCharset, 'utf-8')) {
return preg_split('//u', $sString, -1, PREG_SPLIT_NO_EMPTY);
} else {
$iLength = mb_strlen($sString, $this->sCharset);
$aResult = [];
for ($i = 0; $i < $iLength; ++$i) {
$aResult[] = mb_substr($sString, $i, 1, $this->sCharset);
}
return $aResult;
}
} else {
if ($sString === '') {
return [];
} else {
return str_split($sString);
}
}
}
/**
* @param string $sString
* @param string $sNeedle
* @param int $iOffset
*
* @return int|false
*/
private function strpos($sString, $sNeedle, $iOffset)
{
if ($this->oParserSettings->bMultibyteSupport) {
return mb_strpos($sString, $sNeedle, $iOffset, $this->sCharset);
} else {
return strpos($sString, $sNeedle, $iOffset);
}
}
}