Skip to content

Commit

Permalink
Fix comments being omitted in the middle of a rule
Browse files Browse the repository at this point in the history
Also fixes comments between CSSLists.
  • Loading branch information
scq committed Feb 24, 2020
1 parent a80a97d commit 567ffc6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Sabberworm/CSS/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function parseList(ParserState $oParserState, CSSList $oList) {
$oListItem->setComments($comments);
$oList->append($oListItem);
}
$oParserState->consumeWhiteSpace();
$oParserState->consumeWhiteSpace(false);
}
if(!$bIsRoot && !$bLenientParsing) {
throw new SourceException("Unexpected end of document", $oParserState->currentLine());
Expand Down
5 changes: 4 additions & 1 deletion lib/Sabberworm/CSS/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,15 @@ public function parseCharacter($bIsForIdentifier) {
return null;
}

public function consumeWhiteSpace() {
public function consumeWhiteSpace($consumeComments = true) {
$comments = array();
do {
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
$this->consume(1);
}
if (!$consumeComments) {
return;
}
if($this->oParserSettings->bLenientParsing) {
try {
$oComment = $this->consumeComment();
Expand Down
2 changes: 1 addition & 1 deletion lib/Sabberworm/CSS/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function parse(ParserState $oParserState) {
while ($oParserState->comes(';')) {
$oParserState->consume(';');
}
$oParserState->consumeWhiteSpace();
$oParserState->consumeWhiteSpace(false);

return $oRule;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Sabberworm/CSS/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,16 @@ function testFlatCommentExtracting() {
$this->assertEquals("Find Me!", $comments[0]->getComment());
}

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

function testTopLevelCommentExtracting() {
$parser = new Parser('/*Find Me!*/div {left:10px; text-align:left;}');
$doc = $parser->parse();
Expand Down

0 comments on commit 567ffc6

Please sign in to comment.