Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Add some more tests for parsing comments #738

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tests/RuleSet/DeclarationBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,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;

/**
Expand Down Expand Up @@ -428,4 +430,51 @@ public function orderOfElementsMatchingOriginalOrderAfterExpandingShorthands():
\array_map('strval', $lastDeclarationBlock->getRulesAssoc())
);
}

/**
* @return array<string, array{0: non-empty-string, 1: non-empty-string}>
*/
public static function declarationBlocksWithCommentsProvider(): array
{
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
* @dataProvider declarationBlocksWithCommentsProvider
*/
public function canRemoveCommentsFromRulesUsingLenientParsing(
string $cssWithComments,
string $cssWithoutComments
): void {
$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
* @dataProvider declarationBlocksWithCommentsProvider
*/
public function canRemoveCommentsFromRulesUsingStrictParsing(
string $cssWithComments,
string $cssWithoutComments
): void {
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);
}
}