Skip to content

Commit a24115f

Browse files
committed
[CLEANUP] Tidy up DeclarationBlock::parse()
- Assign the result of `ParserState::peek()` to a local variable, for efficiency; - Use a switch statement to branch on its value, for extensibility (e.g. #1292); - Don't unnecessarily test that a quote character is not escaped when not within a string.
1 parent ec1f6dd commit a24115f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/RuleSet/DeclarationBlock.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,20 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
4444
do {
4545
$selectorParts[] = $parserState->consume(1)
4646
. $parserState->consumeUntil(['{', '}', '\'', '"'], false, false, $comments);
47-
if (\in_array($parserState->peek(), ['\'', '"'], true) && \substr(\end($selectorParts), -1) != '\\') {
48-
if (!isset($stringWrapperCharacter)) {
49-
$stringWrapperCharacter = $parserState->peek();
50-
} elseif ($stringWrapperCharacter === $parserState->peek()) {
51-
unset($stringWrapperCharacter);
52-
}
47+
$nextCharacter = $parserState->peek();
48+
switch ($nextCharacter) {
49+
case '\'':
50+
case '"':
51+
if (!isset($stringWrapperCharacter)) {
52+
$stringWrapperCharacter = $nextCharacter;
53+
} elseif ($stringWrapperCharacter === $nextCharacter) {
54+
if (\substr(\end($selectorParts), -1) !== '\\') {
55+
unset($stringWrapperCharacter);
56+
}
57+
}
58+
break;
5359
}
54-
} while (!\in_array($parserState->peek(), ['{', '}'], true) || isset($stringWrapperCharacter));
60+
} while (!\in_array($nextCharacter, ['{', '}'], true) || isset($stringWrapperCharacter));
5561
$result->setSelectors(\implode('', $selectorParts), $list);
5662
if ($parserState->comes('{')) {
5763
$parserState->consume(1);

0 commit comments

Comments
 (0)