Skip to content

Commit ca5d353

Browse files
committed
Fix comment parsing to support multiple comments
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.
1 parent c4509f5 commit ca5d353

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public static function parseList(ParserState $oParserState, CSSList $oList) {
6161
$oListItem->setComments($comments);
6262
$oList->append($oListItem);
6363
}
64-
$oParserState->consumeWhiteSpace();
6564
}
6665
if(!$bIsRoot && !$bLenientParsing) {
6766
throw new SourceException("Unexpected end of document", $oParserState->currentLine());

lib/Sabberworm/CSS/Rule/Rule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public static function parse(ParserState $oParserState) {
5656
while ($oParserState->comes(';')) {
5757
$oParserState->consume(';');
5858
}
59-
$oParserState->consumeWhiteSpace();
6059

6160
return $oRule;
6261
}

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -716,22 +716,28 @@ function testCommentExtracting() {
716716
}
717717

718718
function testFlatCommentExtracting() {
719-
$parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}');
719+
$parser = new Parser('div {/*Find Me!*/left:10px; /*Find Me Too!*/text-align:left;}');
720720
$doc = $parser->parse();
721721
$contents = $doc->getContents();
722722
$divRules = $contents[0]->getRules();
723-
$comments = $divRules[0]->getComments();
724-
$this->assertCount(1, $comments);
725-
$this->assertEquals("Find Me!", $comments[0]->getComment());
723+
$rule1Comments = $divRules[0]->getComments();
724+
$rule2Comments = $divRules[1]->getComments();
725+
$this->assertCount(1, $rule1Comments);
726+
$this->assertCount(1, $rule2Comments);
727+
$this->assertEquals("Find Me!", $rule1Comments[0]->getComment());
728+
$this->assertEquals("Find Me Too!", $rule2Comments[0]->getComment());
726729
}
727730

728731
function testTopLevelCommentExtracting() {
729-
$parser = new Parser('/*Find Me!*/div {left:10px; text-align:left;}');
732+
$parser = new Parser('/*Find Me!*/div {left:10px; text-align:left;} /*Find Me Too!*/a {left:10px;}');
730733
$doc = $parser->parse();
731734
$contents = $doc->getContents();
732-
$comments = $contents[0]->getComments();
733-
$this->assertCount(1, $comments);
734-
$this->assertEquals("Find Me!", $comments[0]->getComment());
735+
$list1Comments = $contents[0]->getComments();
736+
$list2Comments = $contents[1]->getComments();
737+
$this->assertCount(1, $list1Comments);
738+
$this->assertCount(1, $list2Comments);
739+
$this->assertEquals("Find Me!", $list1Comments[0]->getComment());
740+
$this->assertEquals("Find Me Too!", $list2Comments[0]->getComment());
735741
}
736742

737743
/**

0 commit comments

Comments
 (0)