Skip to content

[BUGFIX] Allow comma-separated arguments in selectors #1292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 10, 2025
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
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 selector functions (like `:not`) with comma-separated arguments (#1292)
- 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
Expand Down
21 changes: 19 additions & 2 deletions src/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
$selectors = [];
$selectorParts = [];
$stringWrapperCharacter = null;
$functionNestingLevel = 0;
$consumedNextCharacter = false;
static $stopCharacters = ['{', '}', '\'', '"', ','];
static $stopCharacters = ['{', '}', '\'', '"', '(', ')', ','];
do {
if (!$consumedNextCharacter) {
$selectorParts[] = $parserState->consume(1);
Expand All @@ -64,8 +65,21 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
}
}
break;
case ',':
case '(':
if (!\is_string($stringWrapperCharacter)) {
++$functionNestingLevel;
}
break;
case ')':
if (!\is_string($stringWrapperCharacter)) {
if ($functionNestingLevel <= 0) {
throw new UnexpectedTokenException('anything but', ')');
}
--$functionNestingLevel;
}
break;
case ',':
if (!\is_string($stringWrapperCharacter) && $functionNestingLevel === 0) {
$selectors[] = \implode('', $selectorParts);
$selectorParts = [];
$parserState->consume(1);
Expand All @@ -74,6 +88,9 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ?
break;
}
} while (!\in_array($nextCharacter, ['{', '}'], true) || \is_string($stringWrapperCharacter));
if ($functionNestingLevel !== 0) {
throw new UnexpectedTokenException(')', $nextCharacter);
}
$selectors[] = \implode('', $selectorParts); // add final or only selector
$result->setSelectors($selectors, $list);
if ($parserState->comes('{')) {
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/RuleSet/DeclarationBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static function provideSelector(): array
'pseudo-class' => [':hover'],
'type & pseudo-class' => ['a:hover'],
'`not`' => [':not(#your-mug)'],
'`not` with multiple arguments' => [':not(#your-mug, .their-mug)'],
'pseudo-element' => ['::before'],
'attribute with `"`' => ['[alt="{}()[]\\"\',"]'],
'attribute with `\'`' => ['[alt=\'{}()[]"\\\',\']'],
Expand Down Expand Up @@ -114,6 +115,40 @@ public function parsesTwoCommaSeparatedSelectors(string $firstSelector, string $
self::assertSame([$firstSelector, $secondSelector], self::getSelectorsAsStrings($subject));
}

/**
* @return array<non-empty-string, array{0: non-empty-string}>
*/
public static function provideInvalidSelector(): array
{
// TODO: the `parse` method consumes the first character without inspection,
// so the 'lone' test strings are prefixed with a space.
return [
'lone `(`' => [' ('],
'lone `)`' => [' )'],
'unclosed `(`' => [':not(#your-mug'],
'extra `)`' => [':not(#your-mug))'],
];
}

/**
* @test
*
* @param non-empty-string $selector
*
* @dataProvider provideInvalidSelector
*/
public function parseSkipsBlockWithInvalidSelector(string $selector): void
{
static $nextCss = ' .next {}';
$css = $selector . ' {}' . $nextCss;
$parserState = new ParserState($css, Settings::create());

$subject = DeclarationBlock::parse($parserState);

self::assertNull($subject);
self::assertTrue($parserState->comes($nextCss));
}

/**
* @return array<string>
*/
Expand Down