Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[BUGFIX] Allow comma in quoted string in selector
Split by commas during parsing, not after.
  • Loading branch information
JakeQZ committed Jul 10, 2025
commit 077a10e3b72d74244e03f56737817ee7637bb003
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Please also have a look at our

### Fixed

- Parse quoted attribute selector value containing comma (#1323)
- Allow comma in selectors (e.g. `:not(html, body)`) (#1293)
- Insert `Rule` before sibling even with different property name
(in `RuleSet::addRule()`) (#1270)
Expand Down
22 changes: 18 additions & 4 deletions src/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
$comments = [];
$result = new DeclarationBlock($parserState->currentLine());
try {
$selectors = [];
$selectorParts = [];
$stringWrapperCharacter = null;
static $stopCharacters = ['{', '}', '\'', '"'];
$consumedNextCharacter = false;
static $stopCharacters = ['{', '}', '\'', '"', ','];
do {
$selectorParts[] = $parserState->consume(1)
. $parserState->consumeUntil($stopCharacters, false, false, $comments);
if (!$consumedNextCharacter) {
$selectorParts[] = $parserState->consume(1);
}
$selectorParts[] = $parserState->consumeUntil($stopCharacters, false, false, $comments);
$nextCharacter = $parserState->peek();
$consumedNextCharacter = false;
switch ($nextCharacter) {
case '\'':
// The fallthrough is intentional.
Expand All @@ -59,9 +64,18 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
}
}
break;
case ',':
if (!\is_string($stringWrapperCharacter)) {
$selectors[] = \implode('', $selectorParts);
$selectorParts = [];
$parserState->consume(1);
$consumedNextCharacter = true;
}
break;
}
} while (!\in_array($nextCharacter, ['{', '}'], true) || \is_string($stringWrapperCharacter));
$result->setSelectors(\implode('', $selectorParts), $list);
$selectors[] = \implode('', $selectorParts); // add final or only selector
$result->setSelectors($selectors, $list);
if ($parserState->comes('{')) {
$parserState->consume(1);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/RuleSet/DeclarationBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public static function provideSelector(): array
'type & pseudo-class' => ['a:hover'],
'`not`' => [':not(#your-mug)'],
'pseudo-element' => ['::before'],
'attribute with `"`' => ['[alt="{}()[]\\"\'"]'],
'attribute with `\'`' => ['[alt=\'{}()[]"\\\'\']'],
'attribute with `"`' => ['[alt="{}()[]\\"\',"]'],
'attribute with `\'`' => ['[alt=\'{}()[]"\\\',\']'],
];
}

Expand Down