Skip to content

Commit edaa39a

Browse files
committed
[BUGFIX] Fix comment parsing to support multiple comments
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 <[email protected]>
1 parent 5a25712 commit edaa39a

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/Parsing/ParserState.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,26 @@ public function parseCharacter($bIsForIdentifier)
220220
}
221221

222222
/**
223-
* @return array<int, Comment>|void
223+
* Consumes whitespace and comments and returns the comments.
224+
* If $withoutComment is true, only whitespace is consumed.
225+
*
226+
* @param bool $withoutComment Do not consume comments, only whitespace.
227+
*
228+
* @return array<int, Comment>|void List of comments.
224229
*
225230
* @throws UnexpectedEOFException
226231
* @throws UnexpectedTokenException
227232
*/
228-
public function consumeWhiteSpace(): array
233+
public function consumeWhiteSpace(bool $withoutComment = false): array
229234
{
230235
$aComments = [];
231236
do {
232237
while (\preg_match('/\\s/isSu', $this->peek()) === 1) {
233238
$this->consume(1);
234239
}
240+
if ($withoutComment) {
241+
break;
242+
}
235243
if ($this->oParserSettings->bLenientParsing) {
236244
try {
237245
$oComment = $this->consumeComment();

src/Rule/Rule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static function parse(ParserState $oParserState): Rule
105105
while ($oParserState->comes(';')) {
106106
$oParserState->consume(';');
107107
}
108-
$oParserState->consumeWhiteSpace();
108+
$oParserState->consumeWhiteSpace(true);
109109

110110
return $oRule;
111111
}

tests/ParserTest.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -1161,13 +1161,16 @@ public function commentExtracting(): void
11611161
*/
11621162
public function flatCommentExtracting(): void
11631163
{
1164-
$parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}');
1164+
$parser = new Parser('div {/*Find Me!*/left:10px; /*Find Me Too!*/text-align:left;}');
11651165
$doc = $parser->parse();
11661166
$contents = $doc->getContents();
11671167
$divRules = $contents[0]->getRules();
1168-
$comments = $divRules[0]->getComments();
1169-
self::assertCount(1, $comments);
1170-
self::assertSame('Find Me!', $comments[0]->getComment());
1168+
$rule1Comments = $divRules[0]->getComments();
1169+
$rule2Comments = $divRules[1]->getComments();
1170+
self::assertCount(1, $rule1Comments);
1171+
self::assertCount(1, $rule2Comments);
1172+
self::assertEquals('Find Me!', $rule1Comments[0]->getComment());
1173+
self::assertEquals('Find Me Too!', $rule2Comments[0]->getComment());
11711174
}
11721175

11731176
/**

0 commit comments

Comments
 (0)