Skip to content

Commit 567ffc6

Browse files
committed
Fix comments being omitted in the middle of a rule
Also fixes comments between CSSLists.
1 parent a80a97d commit 567ffc6

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static function parseList(ParserState $oParserState, CSSList $oList) {
6262
$oListItem->setComments($comments);
6363
$oList->append($oListItem);
6464
}
65-
$oParserState->consumeWhiteSpace();
65+
$oParserState->consumeWhiteSpace(false);
6666
}
6767
if(!$bIsRoot && !$bLenientParsing) {
6868
throw new SourceException("Unexpected end of document", $oParserState->currentLine());

lib/Sabberworm/CSS/Parsing/ParserState.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ public function parseCharacter($bIsForIdentifier) {
112112
return null;
113113
}
114114

115-
public function consumeWhiteSpace() {
115+
public function consumeWhiteSpace($consumeComments = true) {
116116
$comments = array();
117117
do {
118118
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
119119
$this->consume(1);
120120
}
121+
if (!$consumeComments) {
122+
return;
123+
}
121124
if($this->oParserSettings->bLenientParsing) {
122125
try {
123126
$oComment = $this->consumeComment();

lib/Sabberworm/CSS/Rule/Rule.php

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

6161
return $oRule;
6262
}

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,16 @@ function testFlatCommentExtracting() {
733733
$this->assertEquals("Find Me!", $comments[0]->getComment());
734734
}
735735

736+
function testInnerCommentExtracting() {
737+
$parser = new Parser('div {left:10px;/*Find Me!*/text-align:left;}');
738+
$doc = $parser->parse();
739+
$contents = $doc->getContents();
740+
$divRules = $contents[0]->getRules();
741+
$comments = $divRules[1]->getComments();
742+
$this->assertCount(1, $comments);
743+
$this->assertEquals("Find Me!", $comments[0]->getComment());
744+
}
745+
736746
function testTopLevelCommentExtracting() {
737747
$parser = new Parser('/*Find Me!*/div {left:10px; text-align:left;}');
738748
$doc = $parser->parse();

0 commit comments

Comments
 (0)