Skip to content

Commit 9e3c5be

Browse files
scqcodyfinegan
authored andcommitted
Fix comments being omitted in the middle of a rule
Also fixes comments between CSSLists.
1 parent daaa45e commit 9e3c5be

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
@@ -90,7 +90,7 @@ public static function parseList(ParserState $oParserState, CSSList $oList)
9090
$oListItem->addComments($aComments);
9191
$oList->append($oListItem);
9292
}
93-
$aComments = $oParserState->consumeWhiteSpace();
93+
$aComments = $oParserState->consumeWhiteSpace(false);
9494
}
9595
$oList->addComments($aComments);
9696
if (!$bIsRoot && !$bLenientParsing) {

src/Parsing/ParserState.php

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

228228
/**
229+
* @param bool $consumeComments
229230
* @return array<int, Comment>|void
230231
*
231232
* @throws UnexpectedEOFException
232233
* @throws UnexpectedTokenException
233234
*/
234-
public function consumeWhiteSpace()
235+
public function consumeWhiteSpace($consumeComments = true)
235236
{
236237
$aComments = [];
237238
do {
238239
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
239240
$this->consume(1);
240241
}
242+
if (!$consumeComments) {
243+
return;
244+
}
241245
if ($this->oParserSettings->bLenientParsing) {
242246
try {
243247
$oComment = $this->consumeComment();

src/Rule/Rule.php

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

112112
return $oRule;
113113
}

tests/ParserTest.php

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

1176+
/**
1177+
* @test
1178+
*/
1179+
public function testInnerCommentExtracting() {
1180+
$parser = new Parser('div {left:10px;/*Find Me!*/text-align:left;}');
1181+
$doc = $parser->parse();
1182+
$contents = $doc->getContents();
1183+
$divRules = $contents[0]->getRules();
1184+
$comments = $divRules[1]->getComments();
1185+
$this->assertCount(1, $comments);
1186+
$this->assertEquals("Find Me!", $comments[0]->getComment());
1187+
}
1188+
11761189
/**
11771190
* @test
11781191
*/

0 commit comments

Comments
 (0)