Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Please also have a look at our

### Fixed

- Allow CSS containing only whitespace or comments (#1593)
- Only allow strings as `LineName` components (#1590)

### Documentation
Expand Down
4 changes: 2 additions & 2 deletions src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public static function parseList(ParserState $parserState, CSSList $list): void
$isRoot = $list instanceof Document;
$usesLenientParsing = $parserState->getSettings()->usesLenientParsing();
$comments = [];
$parserState->consumeWhiteSpace($comments);
while (!$parserState->isEnd()) {
$parserState->consumeWhiteSpace($comments);
$listItem = null;
if ($usesLenientParsing) {
try {
Expand All @@ -77,7 +77,7 @@ public static function parseList(ParserState $parserState, CSSList $list): void
} catch (UnexpectedTokenException $e) {
$listItem = false;
// If the failed parsing did not consume anything that was to come ...
if ($parserState->currentColumn() === $positionBeforeParse && !$parserState->isEnd()) {
if ($parserState->currentColumn() === $positionBeforeParse) {
// ... the unexpected token needs to be skipped, otherwise there'll be an infinite loop.
$parserState->consume(1);
}
Expand Down
52 changes: 52 additions & 0 deletions tests/Functional/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\CSSList\Document;
use Sabberworm\CSS\Parser;
use Sabberworm\CSS\Settings;
use TRegx\PhpUnit\DataProviders\DataProvider;

/**
* @covers \Sabberworm\CSS\Parser
Expand Down Expand Up @@ -36,4 +38,54 @@ public function parseWithOneRuleSetReturnsDocument(): void

self::assertInstanceOf(Document::class, $result);
}

/**
* @return array<non-empty-string, array{0: string}>
*/
public static function provideEmptyCss(): array
{
return [
'empty string' => [''],
'space' => [' '],
'newline' => ["\n"],
'carriage return' => ["\r"],
'tab' => ["\t"],
'Windows line ending' => ["\r\n"],
'comment' => ['/* I get put in a separate property */'],
];
}

/**
* @return array<non-empty-string, array{0: bool}>
*/
public static function provideLenientParsingFlag(): array
{
return [
'strict parsing' => [false],
'lenient parsing' => [true],
];
}

/**
* @return DataProvider<non-empty-string, array{0: string, 1: bool}>
*/
public static function provideEmptyCssAndLenientParsingFlag(): DataProvider
{
return DataProvider::cross(static::provideEmptyCss(), static::provideLenientParsingFlag());
}

/**
* @test
*
* @dataProvider provideEmptyCssAndLenientParsingFlag
*/
public function parsesEmptyCss(string $css, bool $parseLeniently): void
{
$parser = new Parser($css, Settings::create()->withLenientParsing($parseLeniently));

$result = $parser->parse();

// Note: Comments for the document are accessed separately via `getComments()`.
self::assertSame([], $result->getContents());
}
}