From 611f5218ca5ee6f014940e12ec2f671ad61cd58b Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sun, 20 Oct 2024 19:43:48 +0200 Subject: [PATCH] [TASK] Add some more tests for parsing comments Also add a skipped test for the broken behavior that currently is blocking Emogrifier. This is the 8.x backport of #738. --- tests/RuleSet/DeclarationBlockTest.php | 57 ++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/RuleSet/DeclarationBlockTest.php b/tests/RuleSet/DeclarationBlockTest.php index 27c6615f..ea48f283 100644 --- a/tests/RuleSet/DeclarationBlockTest.php +++ b/tests/RuleSet/DeclarationBlockTest.php @@ -3,8 +3,10 @@ namespace Sabberworm\CSS\Tests\RuleSet; use PHPUnit\Framework\TestCase; +use Sabberworm\CSS\OutputFormat; use Sabberworm\CSS\Parser; use Sabberworm\CSS\Rule\Rule; +use Sabberworm\CSS\Settings as ParserSettings; use Sabberworm\CSS\Value\Size; /** @@ -450,4 +452,59 @@ public function orderOfElementsMatchingOriginalOrderAfterExpandingShorthands() array_map('strval', $oDeclaration->getRulesAssoc()) ); } + + /** + * @return array + */ + public static function declarationBlocksWithCommentsProvider() + { + return [ + 'CSS comments with one asterisk' => ['p {color: #000;/* black */}', 'p {color: #000;}'], + 'CSS comments with two asterisks' => ['p {color: #000;/** black */}', 'p {color: #000;}'], + ]; + } + + /** + * @test + * + * @param string $cssWithComments + * @param string $cssWithoutComments + * + * @dataProvider declarationBlocksWithCommentsProvider + */ + public function canRemoveCommentsFromRulesUsingLenientParsing( + $cssWithComments, + $cssWithoutComments + ) { + $parserSettings = ParserSettings::create()->withLenientParsing(true); + $document = (new Parser($cssWithComments, $parserSettings))->parse(); + + $outputFormat = (new OutputFormat())->setRenderComments(false); + $renderedDocument = $document->render($outputFormat); + + self::assertSame($cssWithoutComments, $renderedDocument); + } + + /** + * @test + * + * @param string $cssWithComments + * @param string $cssWithoutComments + * + * @dataProvider declarationBlocksWithCommentsProvider + */ + public function canRemoveCommentsFromRulesUsingStrictParsing( + $cssWithComments, + $cssWithoutComments + ) { + self::markTestSkipped('This currently crashes, and we need to fix it.'); + + $parserSettings = ParserSettings::create()->withLenientParsing(false); + $document = (new Parser($cssWithComments, $parserSettings))->parse(); + + $outputFormat = (new OutputFormat())->setRenderComments(false); + $renderedDocument = $document->render($outputFormat); + + self::assertSame($cssWithoutComments, $renderedDocument); + } }