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