Skip to content

Commit 9ac3325

Browse files
scqfabianderschatta
authored andcommitted
Fix comments being omitted in the middle of a rule
Also fixes comments between CSSLists.
1 parent 19e8392 commit 9ac3325

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
@@ -61,7 +61,7 @@ public static function parseList(ParserState $oParserState, CSSList $oList) {
6161
$oListItem->setComments($comments);
6262
$oList->append($oListItem);
6363
}
64-
$oParserState->consumeWhiteSpace();
64+
$oParserState->consumeWhiteSpace(false);
6565
}
6666
if(!$bIsRoot && !$bLenientParsing) {
6767
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
@@ -107,12 +107,15 @@ public function parseCharacter($bIsForIdentifier) {
107107
return null;
108108
}
109109

110-
public function consumeWhiteSpace() {
110+
public function consumeWhiteSpace($consumeComments = true) {
111111
$comments = array();
112112
do {
113113
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
114114
$this->consume(1);
115115
}
116+
if (!$consumeComments) {
117+
return;
118+
}
116119
if($this->oParserSettings->bLenientParsing) {
117120
try {
118121
$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
@@ -690,6 +690,16 @@ function testFlatCommentExtracting() {
690690
$this->assertEquals("Find Me!", $comments[0]->getComment());
691691
}
692692

693+
function testInnerCommentExtracting() {
694+
$parser = new Parser('div {left:10px;/*Find Me!*/text-align:left;}');
695+
$doc = $parser->parse();
696+
$contents = $doc->getContents();
697+
$divRules = $contents[0]->getRules();
698+
$comments = $divRules[1]->getComments();
699+
$this->assertCount(1, $comments);
700+
$this->assertEquals("Find Me!", $comments[0]->getComment());
701+
}
702+
693703
function testTopLevelCommentExtracting() {
694704
$parser = new Parser('/*Find Me!*/div {left:10px; text-align:left;}');
695705
$doc = $parser->parse();

0 commit comments

Comments
 (0)