From 049f1a0be7cef71d48bdba8eca4159691595cf2c Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Mon, 15 Dec 2025 00:59:30 +0000 Subject: [PATCH] [TASK] Consume to EOF in `DeclarationBlock::parse()` upon failure This is not the right long-term fix - see #1430. For now it is a sticking-plaster to help complete #1424. --- src/RuleSet/DeclarationBlock.php | 2 +- tests/Unit/RuleSet/DeclarationBlockTest.php | 40 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/RuleSet/DeclarationBlock.php b/src/RuleSet/DeclarationBlock.php index 7aa81f27..6e2e1a60 100644 --- a/src/RuleSet/DeclarationBlock.php +++ b/src/RuleSet/DeclarationBlock.php @@ -75,7 +75,7 @@ public static function parse(ParserState $parserState, ?CSSList $list = null): ? } catch (UnexpectedTokenException $e) { if ($parserState->getSettings()->usesLenientParsing()) { if (!$parserState->consumeIfComes('}')) { - $parserState->consumeUntil('}', false, true); + $parserState->consumeUntil(['}', ParserState::EOF], false, true); } return null; } else { diff --git a/tests/Unit/RuleSet/DeclarationBlockTest.php b/tests/Unit/RuleSet/DeclarationBlockTest.php index caddbd13..6b1b2865 100644 --- a/tests/Unit/RuleSet/DeclarationBlockTest.php +++ b/tests/Unit/RuleSet/DeclarationBlockTest.php @@ -231,6 +231,46 @@ public function parseConsumesClosingBraceAfterInvalidSelector(string $selector, self::assertTrue($parserState->isEnd()); } + /** + * @return array + */ + public static function provideOptionalWhitespace(): array + { + return [ + 'none' => [''], + 'space' => [' '], + 'newline' => ["\n"], + ]; + } + + /** + * @return DataProvider + */ + public static function provideInvalidSelectorAndOptionalWhitespace(): DataProvider + { + return DataProvider::cross(self::provideInvalidSelector(), self::provideOptionalWhitespace()); + } + + /** + * TODO: It's probably not the responsibility of `DeclarationBlock` to deal with this. + * + * @test + * + * @param non-empty-string $selector + * + * @dataProvider provideInvalidSelectorAndOptionalWhitespace + */ + public function parseConsumesToEofIfNoClosingBraceAfterInvalidSelector( + string $selector, + string $optionalWhitespace + ): void { + $parserState = new ParserState($selector . $optionalWhitespace, Settings::create()); + + DeclarationBlock::parse($parserState); + + self::assertTrue($parserState->isEnd()); + } + /** * @return array */