Skip to content

Commit a9c061d

Browse files
committed
[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.
1 parent df0ac7c commit a9c061d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tests/RuleSet/DeclarationBlockTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Sabberworm\CSS\Tests\RuleSet;
44

55
use PHPUnit\Framework\TestCase;
6+
use Sabberworm\CSS\OutputFormat;
67
use Sabberworm\CSS\Parser;
78
use Sabberworm\CSS\Rule\Rule;
9+
use Sabberworm\CSS\Settings as ParserSettings;
810
use Sabberworm\CSS\Value\Size;
911

1012
/**
@@ -450,4 +452,51 @@ public function orderOfElementsMatchingOriginalOrderAfterExpandingShorthands()
450452
array_map('strval', $oDeclaration->getRulesAssoc())
451453
);
452454
}
455+
456+
/**
457+
* @return array<string, array{0: non-empty-string, 1: non-empty-string}>
458+
*/
459+
public static function declarationBlocksWithCommentsProvider(): array
460+
{
461+
return [
462+
'CSS comments with one asterisk' => ['p {color: #000;/* black */}', 'p {color: #000;}'],
463+
'CSS comments with two asterisks' => ['p {color: #000;/** black */}', 'p {color: #000;}'],
464+
];
465+
}
466+
467+
/**
468+
* @test
469+
* @dataProvider declarationBlocksWithCommentsProvider
470+
*/
471+
public function canRemoveCommentsFromRulesUsingLenientParsing(
472+
string $cssWithComments,
473+
string $cssWithoutComments
474+
): void {
475+
$parserSettings = ParserSettings::create()->withLenientParsing(true);
476+
$document = (new Parser($cssWithComments, $parserSettings))->parse();
477+
478+
$outputFormat = (new OutputFormat())->setRenderComments(false);
479+
$renderedDocument = $document->render($outputFormat);
480+
481+
self::assertSame($cssWithoutComments, $renderedDocument);
482+
}
483+
484+
/**
485+
* @test
486+
* @dataProvider declarationBlocksWithCommentsProvider
487+
*/
488+
public function canRemoveCommentsFromRulesUsingStrictParsing(
489+
string $cssWithComments,
490+
string $cssWithoutComments
491+
): void {
492+
self::markTestSkipped('This currently crashes, and we need to fix it.');
493+
494+
$parserSettings = ParserSettings::create()->withLenientParsing(false);
495+
$document = (new Parser($cssWithComments, $parserSettings))->parse();
496+
497+
$outputFormat = (new OutputFormat())->setRenderComments(false);
498+
$renderedDocument = $document->render($outputFormat);
499+
500+
self::assertSame($cssWithoutComments, $renderedDocument);
501+
}
453502
}

0 commit comments

Comments
 (0)