Skip to content

Commit e2bb6b7

Browse files
authored
[TASK] Add some more tests for parsing comments (#738)
Also add a skipped test for the broken behavior that currently is blocking Emogrifier.
1 parent 9974bbf commit e2bb6b7

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
@@ -5,8 +5,10 @@
55
namespace Sabberworm\CSS\Tests\RuleSet;
66

77
use PHPUnit\Framework\TestCase;
8+
use Sabberworm\CSS\OutputFormat;
89
use Sabberworm\CSS\Parser;
910
use Sabberworm\CSS\Rule\Rule;
11+
use Sabberworm\CSS\Settings as ParserSettings;
1012
use Sabberworm\CSS\Value\Size;
1113

1214
/**
@@ -428,4 +430,51 @@ public function orderOfElementsMatchingOriginalOrderAfterExpandingShorthands():
428430
\array_map('strval', $lastDeclarationBlock->getRulesAssoc())
429431
);
430432
}
433+
434+
/**
435+
* @return array<string, array{0: non-empty-string, 1: non-empty-string}>
436+
*/
437+
public static function declarationBlocksWithCommentsProvider(): array
438+
{
439+
return [
440+
'CSS comments with one asterisk' => ['p {color: #000;/* black */}', 'p {color: #000;}'],
441+
'CSS comments with two asterisks' => ['p {color: #000;/** black */}', 'p {color: #000;}'],
442+
];
443+
}
444+
445+
/**
446+
* @test
447+
* @dataProvider declarationBlocksWithCommentsProvider
448+
*/
449+
public function canRemoveCommentsFromRulesUsingLenientParsing(
450+
string $cssWithComments,
451+
string $cssWithoutComments
452+
): void {
453+
$parserSettings = ParserSettings::create()->withLenientParsing(true);
454+
$document = (new Parser($cssWithComments, $parserSettings))->parse();
455+
456+
$outputFormat = (new OutputFormat())->setRenderComments(false);
457+
$renderedDocument = $document->render($outputFormat);
458+
459+
self::assertSame($cssWithoutComments, $renderedDocument);
460+
}
461+
462+
/**
463+
* @test
464+
* @dataProvider declarationBlocksWithCommentsProvider
465+
*/
466+
public function canRemoveCommentsFromRulesUsingStrictParsing(
467+
string $cssWithComments,
468+
string $cssWithoutComments
469+
): void {
470+
self::markTestSkipped('This currently crashes, and we need to fix it.');
471+
472+
$parserSettings = ParserSettings::create()->withLenientParsing(false);
473+
$document = (new Parser($cssWithComments, $parserSettings))->parse();
474+
475+
$outputFormat = (new OutputFormat())->setRenderComments(false);
476+
$renderedDocument = $document->render($outputFormat);
477+
478+
self::assertSame($cssWithoutComments, $renderedDocument);
479+
}
431480
}

0 commit comments

Comments
 (0)