Skip to content

Commit ddd9ea7

Browse files
authored
[BUGFIX] Fix comment parsing to support multiple comments (#671) (#671)
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>
1 parent d2fb94a commit ddd9ea7

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1515

1616
### Fixed
1717

18+
- Fix comment parsing to support multiple comments (#671)
19+
1820
## 8.6.0
1921

2022
### Added

src/Rule/Rule.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ public static function parse(ParserState $oParserState)
107107
while ($oParserState->comes(';')) {
108108
$oParserState->consume(';');
109109
}
110-
$oParserState->consumeWhiteSpace();
110+
111+
// NOTE: This is a backport to fix comment parsing to support multiple
112+
// comments. This will be rectified in version 9.0.0.
113+
while (preg_match('/\\s/isSu', $oParserState->peek()) === 1) {
114+
$oParserState->consume(1);
115+
}
111116

112117
return $oRule;
113118
}

tests/ParserTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ public function commentExtracting()
11621162
/**
11631163
* @test
11641164
*/
1165-
public function flatCommentExtracting()
1165+
public function flatCommentExtractingOneComment()
11661166
{
11671167
$parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}');
11681168
$doc = $parser->parse();
@@ -1172,6 +1172,22 @@ public function flatCommentExtracting()
11721172
self::assertCount(1, $comments);
11731173
self::assertSame("Find Me!", $comments[0]->getComment());
11741174
}
1175+
/**
1176+
* @test
1177+
*/
1178+
public function flatCommentExtractingTwoComments()
1179+
{
1180+
$parser = new Parser('div {/*Find Me!*/left:10px; /*Find Me Too!*/text-align:left;}');
1181+
$doc = $parser->parse();
1182+
$contents = $doc->getContents();
1183+
$divRules = $contents[0]->getRules();
1184+
$rule1Comments = $divRules[0]->getComments();
1185+
$rule2Comments = $divRules[1]->getComments();
1186+
self::assertCount(1, $rule1Comments);
1187+
self::assertCount(1, $rule2Comments);
1188+
self::assertEquals('Find Me!', $rule1Comments[0]->getComment());
1189+
self::assertEquals('Find Me Too!', $rule2Comments[0]->getComment());
1190+
}
11751191

11761192
/**
11771193
* @test

0 commit comments

Comments
 (0)