Skip to content

Commit 2141f50

Browse files
scqCody Finegan
authored andcommitted
Fix comments being omitted in the middle of a rule
Also fixes comments between CSSLists.
1 parent 28d9ed3 commit 2141f50

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/CSSList/CSSList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function parseList(ParserState $oParserState, CSSList $oList)
8989
$oListItem->setComments($comments);
9090
$oList->append($oListItem);
9191
}
92-
$oParserState->consumeWhiteSpace();
92+
$oParserState->consumeWhiteSpace(false);
9393
}
9494
if (!$bIsRoot && !$bLenientParsing) {
9595
throw new SourceException("Unexpected end of document", $oParserState->currentLine());

src/Parsing/ParserState.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,22 @@ public function parseCharacter($bIsForIdentifier)
197197
}
198198

199199
/**
200+
* @param bool $consumeComments
200201
* @return array<int, Comment>|void
201202
*
202203
* @throws UnexpectedEOFException
203204
* @throws UnexpectedTokenException
204205
*/
205-
public function consumeWhiteSpace()
206+
public function consumeWhiteSpace($consumeComments = true)
206207
{
207208
$comments = [];
208209
do {
209210
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
210211
$this->consume(1);
211212
}
213+
if (!$consumeComments) {
214+
return;
215+
}
212216
if ($this->oParserSettings->bLenientParsing) {
213217
try {
214218
$oComment = $this->consumeComment();

src/Rule/Rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function parse(ParserState $oParserState)
106106
while ($oParserState->comes(';')) {
107107
$oParserState->consume(';');
108108
}
109-
$oParserState->consumeWhiteSpace();
109+
$oParserState->consumeWhiteSpace(false);
110110

111111
return $oRule;
112112
}

tests/ParserTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,19 @@ public function flatCommentExtracting()
11201120
self::assertSame("Find Me!", $comments[0]->getComment());
11211121
}
11221122

1123+
/**
1124+
* @test
1125+
*/
1126+
public function testInnerCommentExtracting() {
1127+
$parser = new Parser('div {left:10px;/*Find Me!*/text-align:left;}');
1128+
$doc = $parser->parse();
1129+
$contents = $doc->getContents();
1130+
$divRules = $contents[0]->getRules();
1131+
$comments = $divRules[1]->getComments();
1132+
$this->assertCount(1, $comments);
1133+
$this->assertEquals("Find Me!", $comments[0]->getComment());
1134+
}
1135+
11231136
/**
11241137
* @test
11251138
*/

0 commit comments

Comments
 (0)