Skip to content

Commit 44c3a55

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

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sabberworm\CSS\Parsing;
44

5+
use phpDocumentor\Reflection\Types\Boolean;
56
use Sabberworm\CSS\Comment\Comment;
67
use Sabberworm\CSS\Settings;
78

@@ -197,18 +198,24 @@ public function parseCharacter($bIsForIdentifier)
197198
}
198199

199200
/**
201+
* @param bool $consumeComments defaults to true
200202
* @return array<int, Comment>|void
201203
*
202204
* @throws UnexpectedEOFException
203205
* @throws UnexpectedTokenException
204206
*/
205-
public function consumeWhiteSpace()
207+
public function consumeWhiteSpace($consumeComments = true)
206208
{
207209
$comments = [];
208210
do {
209211
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
210212
$this->consume(1);
211213
}
214+
215+
if (!$consumeComments) {
216+
return [];
217+
}
218+
212219
if ($this->oParserSettings->bLenientParsing) {
213220
try {
214221
$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: 14 additions & 0 deletions
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)