Skip to content

[BUGFIX] Allow at-rules to be parsed in strict mode #456

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

Merged
merged 9 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed

Fix (regression) failure to parse at-rules with strict parsing (#456)

## 8.5.0

### Added
Expand Down
17 changes: 7 additions & 10 deletions src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,15 @@ private static function parseListItem(ParserState $oParserState, CSSList $oList)
}
return $oAtRule;
} elseif ($oParserState->comes('}')) {
if (!$oParserState->getSettings()->bLenientParsing) {
throw new UnexpectedTokenException('CSS selector', '}', 'identifier', $oParserState->currentLine());
} else {
if ($bIsRoot) {
if ($oParserState->getSettings()->bLenientParsing) {
return DeclarationBlock::parse($oParserState);
} else {
throw new SourceException("Unopened {", $oParserState->currentLine());
}
if ($bIsRoot) {
if ($oParserState->getSettings()->bLenientParsing) {
return DeclarationBlock::parse($oParserState);
} else {
return null;
throw new SourceException("Unopened {", $oParserState->currentLine());
}
} else {
// End of list
return null;
}
} else {
return DeclarationBlock::parse($oParserState, $oList);
Expand Down
64 changes: 51 additions & 13 deletions tests/CSSList/AtRuleBlockListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,46 @@
use Sabberworm\CSS\CSSList\AtRuleBlockList;
use Sabberworm\CSS\Parser;
use Sabberworm\CSS\Renderable;
use Sabberworm\CSS\Settings;

/**
* @covers \Sabberworm\CSS\CSSList\AtRuleBlockList
*/
final class AtRuleBlockListTest extends TestCase
{
/**
* @return array<string, array<int, string>>
*/
public static function provideMinWidthMediaRule()
{
return [
'without spaces around arguments' => ['@media(min-width: 768px){.class{color:red}}'],
'with spaces around arguments' => ['@media (min-width: 768px) {.class{color:red}}'],
];
}

/**
* @return array<string, array<int, string>>
*/
public static function provideSyntacticlyCorrectAtRule()
{
return [
'media print' => ['@media print { html { background: white; color: black; } }'],
'keyframes' => ['@keyframes mymove { from { top: 0px; } }'],
'supports' => ['
@supports (display: flex) {
.flex-container > * {
text-shadow: 0 0 2px blue;
float: none;
}
.flex-container {
display: flex;
}
}
'],
];
}

/**
* @test
*/
Expand Down Expand Up @@ -43,23 +77,12 @@ public function implementsCommentable()
self::assertInstanceOf(Commentable::class, $subject);
}

/**
* @return array<string, array<int, string>>
*/
public static function mediaRuleDataProvider()
{
return [
'without spaces around arguments' => ['@media(min-width: 768px){.class{color:red}}'],
'with spaces around arguments' => ['@media (min-width: 768px) {.class{color:red}}'],
];
}

/**
* @test
*
* @param string $css
*
* @dataProvider mediaRuleDataProvider
* @dataProvider provideMinWidthMediaRule
*/
public function parsesRuleNameOfMediaQueries($css)
{
Expand All @@ -74,7 +97,7 @@ public function parsesRuleNameOfMediaQueries($css)
*
* @param string $css
*
* @dataProvider mediaRuleDataProvider
* @dataProvider provideMinWidthMediaRule
*/
public function parsesArgumentsOfMediaQueries($css)
{
Expand All @@ -83,4 +106,19 @@ public function parsesArgumentsOfMediaQueries($css)

self::assertSame('(min-width: 768px)', $atRuleBlockList->atRuleArgs());
}

/**
* @test
*
* @param string $css
*
* @dataProvider provideMinWidthMediaRule
* @dataProvider provideSyntacticlyCorrectAtRule
*/
public function parsesSyntacticlyCorrectAtRuleInStrictMode($css)
{
$contents = (new Parser($css, Settings::create()->beStrict()))->parse()->getContents();

self::assertNotEmpty($contents, 'Failing CSS: `' . $css . '`');
}
}