Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Test rejection of selectors with unmatched parentheses
Also reject selectors with unclosed parentheses.
  • Loading branch information
JakeQZ committed Jul 10, 2025
commit 0612c6535504fb1afad96471c24e4ee475649389
3 changes: 3 additions & 0 deletions src/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,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
34 changes: 34 additions & 0 deletions tests/Unit/RuleSet/DeclarationBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,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