Skip to content

Commit

Permalink
[BUGFIX] Fix comment parsing to support multiple comments (MyInterval…
Browse files Browse the repository at this point in the history
…s#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 MyIntervals#173

Signed-off-by: Daniel Ziegenberg <[email protected]>
  • Loading branch information
ziegenberg committed Aug 25, 2024
1 parent d2fb94a commit 6ccfb21
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed

- Fix comment parsing to support multiple comments (#671)

## 8.6.0

### Added
Expand Down
7 changes: 6 additions & 1 deletion src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ public static function parse(ParserState $oParserState)
while ($oParserState->comes(';')) {
$oParserState->consume(';');
}
$oParserState->consumeWhiteSpace();

// NOTE: This is a backport to fix comment parsing to support multiple
// comments. This will be rectified in version 9.0.0.
while (preg_match('/\\s/isSu', $oParserState->peek()) === 1) {
$oParserState->consume(1);
}

return $oRule;
}
Expand Down
18 changes: 17 additions & 1 deletion tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ public function commentExtracting()
/**
* @test
*/
public function flatCommentExtracting()
public function flatCommentExtractingOneComment()
{
$parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}');
$doc = $parser->parse();
Expand All @@ -1172,6 +1172,22 @@ public function flatCommentExtracting()
self::assertCount(1, $comments);
self::assertSame("Find Me!", $comments[0]->getComment());
}
/**
* @test
*/
public function flatCommentExtractingTwoComments()
{
$parser = new Parser('div {/*Find Me!*/left:10px; /*Find Me Too!*/text-align:left;}');
$doc = $parser->parse();
$contents = $doc->getContents();
$divRules = $contents[0]->getRules();
$rule1Comments = $divRules[0]->getComments();
$rule2Comments = $divRules[1]->getComments();
self::assertCount(1, $rule1Comments);
self::assertCount(1, $rule2Comments);
self::assertEquals('Find Me!', $rule1Comments[0]->getComment());
self::assertEquals('Find Me Too!', $rule2Comments[0]->getComment());
}

/**
* @test
Expand Down

0 comments on commit 6ccfb21

Please sign in to comment.