Skip to content

Commit e4c66f6

Browse files
[BUGFIX] Fix comment parsing to support multiple comments (#672)
Because of an eager consumption of whitespace, the rule parsing would swallow a trailing comment, meaning the comment for the next rule would be affected. This patch addresses this by only consuming real whitespace without comments after a rule. Fixes #173 Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at> Co-authored-by: Daniel Ziegenberg <daniel@ziegenberg.at>
1 parent 5a25712 commit e4c66f6

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
4646

4747
### Fixed
4848

49+
- Fix comment parsing to support multiple comments (#672)
4950
- Fix undefined local variable in `CalcFunction::parse()` (#593)
5051
- Fix PHP notice caused by parsing invalid color values having less than 6 characters (#485)
5152
- Fix (regression) failure to parse at-rules with strict parsing (#456)

src/Rule/Rule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ public static function parse(ParserState $oParserState): Rule
105105
while ($oParserState->comes(';')) {
106106
$oParserState->consume(';');
107107
}
108-
$oParserState->consumeWhiteSpace();
108+
109+
while (\preg_match('/\\s/isSu', $oParserState->peek()) === 1) {
110+
$oParserState->consume(1);
111+
}
109112

110113
return $oRule;
111114
}

tests/ParserTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ public function commentExtracting(): void
11591159
/**
11601160
* @test
11611161
*/
1162-
public function flatCommentExtracting(): void
1162+
public function flatCommentExtractingOneComment(): void
11631163
{
11641164
$parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}');
11651165
$doc = $parser->parse();
@@ -1169,6 +1169,22 @@ public function flatCommentExtracting(): void
11691169
self::assertCount(1, $comments);
11701170
self::assertSame('Find Me!', $comments[0]->getComment());
11711171
}
1172+
/**
1173+
* @test
1174+
*/
1175+
public function flatCommentExtractingTwoComments(): void
1176+
{
1177+
$parser = new Parser('div {/*Find Me!*/left:10px; /*Find Me Too!*/text-align:left;}');
1178+
$doc = $parser->parse();
1179+
$contents = $doc->getContents();
1180+
$divRules = $contents[0]->getRules();
1181+
$rule1Comments = $divRules[0]->getComments();
1182+
$rule2Comments = $divRules[1]->getComments();
1183+
self::assertCount(1, $rule1Comments);
1184+
self::assertCount(1, $rule2Comments);
1185+
self::assertEquals('Find Me!', $rule1Comments[0]->getComment());
1186+
self::assertEquals('Find Me Too!', $rule2Comments[0]->getComment());
1187+
}
11721188

11731189
/**
11741190
* @test

0 commit comments

Comments
 (0)