Skip to content

Commit 611f521

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 611f521

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/RuleSet/DeclarationBlockTest.php

Lines changed: 57 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,59 @@ 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()
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+
*
470+
* @param string $cssWithComments
471+
* @param string $cssWithoutComments
472+
*
473+
* @dataProvider declarationBlocksWithCommentsProvider
474+
*/
475+
public function canRemoveCommentsFromRulesUsingLenientParsing(
476+
$cssWithComments,
477+
$cssWithoutComments
478+
) {
479+
$parserSettings = ParserSettings::create()->withLenientParsing(true);
480+
$document = (new Parser($cssWithComments, $parserSettings))->parse();
481+
482+
$outputFormat = (new OutputFormat())->setRenderComments(false);
483+
$renderedDocument = $document->render($outputFormat);
484+
485+
self::assertSame($cssWithoutComments, $renderedDocument);
486+
}
487+
488+
/**
489+
* @test
490+
*
491+
* @param string $cssWithComments
492+
* @param string $cssWithoutComments
493+
*
494+
* @dataProvider declarationBlocksWithCommentsProvider
495+
*/
496+
public function canRemoveCommentsFromRulesUsingStrictParsing(
497+
$cssWithComments,
498+
$cssWithoutComments
499+
) {
500+
self::markTestSkipped('This currently crashes, and we need to fix it.');
501+
502+
$parserSettings = ParserSettings::create()->withLenientParsing(false);
503+
$document = (new Parser($cssWithComments, $parserSettings))->parse();
504+
505+
$outputFormat = (new OutputFormat())->setRenderComments(false);
506+
$renderedDocument = $document->render($outputFormat);
507+
508+
self::assertSame($cssWithoutComments, $renderedDocument);
509+
}
453510
}

0 commit comments

Comments
 (0)