Skip to content

Commit 987077b

Browse files
scqderschatta
authored andcommitted
Fix comments being omitted in the middle of a rule
1 parent 70024fb commit 987077b

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

src/CSSList/CSSList.php

+1-1
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

+7-1
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,24 @@ public function parseCharacter($bIsForIdentifier)
197197
}
198198

199199
/**
200+
* @param bool $consumeComments defaults to true
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+
214+
if (!$consumeComments) {
215+
return [];
216+
}
217+
212218
if ($this->oParserSettings->bLenientParsing) {
213219
try {
214220
$oComment = $this->consumeComment();

src/Rule/Rule.php

+1-1
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

+14
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,20 @@ public function flatCommentExtracting()
11201120
self::assertSame("Find Me!", $comments[0]->getComment());
11211121
}
11221122

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

0 commit comments

Comments
 (0)