Skip to content

Commit

Permalink
Fix comment parsing to support multiple comments
Browse files Browse the repository at this point in the history
This fixes MyIntervals#173.

Because of an eager consumption of whitespace, the rule and csslist
parsing would swallow a trailing comment, meaning the comment for the
next rule/list would be affected. This patch addresses this by not
consuming whitespace after a rule/list.
  • Loading branch information
snake committed Nov 4, 2019
1 parent c4509f5 commit ca5d353
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 0 additions & 1 deletion lib/Sabberworm/CSS/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public static function parseList(ParserState $oParserState, CSSList $oList) {
$oListItem->setComments($comments);
$oList->append($oListItem);
}
$oParserState->consumeWhiteSpace();
}
if(!$bIsRoot && !$bLenientParsing) {
throw new SourceException("Unexpected end of document", $oParserState->currentLine());
Expand Down
1 change: 0 additions & 1 deletion lib/Sabberworm/CSS/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public static function parse(ParserState $oParserState) {
while ($oParserState->comes(';')) {
$oParserState->consume(';');
}
$oParserState->consumeWhiteSpace();

return $oRule;
}
Expand Down
22 changes: 14 additions & 8 deletions tests/Sabberworm/CSS/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,22 +716,28 @@ function testCommentExtracting() {
}

function testFlatCommentExtracting() {
$parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}');
$parser = new Parser('div {/*Find Me!*/left:10px; /*Find Me Too!*/text-align:left;}');
$doc = $parser->parse();
$contents = $doc->getContents();
$divRules = $contents[0]->getRules();
$comments = $divRules[0]->getComments();
$this->assertCount(1, $comments);
$this->assertEquals("Find Me!", $comments[0]->getComment());
$rule1Comments = $divRules[0]->getComments();
$rule2Comments = $divRules[1]->getComments();
$this->assertCount(1, $rule1Comments);
$this->assertCount(1, $rule2Comments);
$this->assertEquals("Find Me!", $rule1Comments[0]->getComment());
$this->assertEquals("Find Me Too!", $rule2Comments[0]->getComment());
}

function testTopLevelCommentExtracting() {
$parser = new Parser('/*Find Me!*/div {left:10px; text-align:left;}');
$parser = new Parser('/*Find Me!*/div {left:10px; text-align:left;} /*Find Me Too!*/a {left:10px;}');
$doc = $parser->parse();
$contents = $doc->getContents();
$comments = $contents[0]->getComments();
$this->assertCount(1, $comments);
$this->assertEquals("Find Me!", $comments[0]->getComment());
$list1Comments = $contents[0]->getComments();
$list2Comments = $contents[1]->getComments();
$this->assertCount(1, $list1Comments);
$this->assertCount(1, $list2Comments);
$this->assertEquals("Find Me!", $list1Comments[0]->getComment());
$this->assertEquals("Find Me Too!", $list2Comments[0]->getComment());
}

/**
Expand Down

0 comments on commit ca5d353

Please sign in to comment.