Skip to content

Fix comments being omitted in the middle of a rule #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
@@ -89,7 +89,7 @@ public static function parseList(ParserState $oParserState, CSSList $oList)
$oListItem->setComments($comments);
$oList->append($oListItem);
}
$oParserState->consumeWhiteSpace();
$oParserState->consumeWhiteSpace(false);
}
if (!$bIsRoot && !$bLenientParsing) {
throw new SourceException("Unexpected end of document", $oParserState->currentLine());
8 changes: 7 additions & 1 deletion src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
@@ -197,18 +197,24 @@ public function parseCharacter($bIsForIdentifier)
}

/**
* @param bool $consumeComments defaults to true
* @return array<int, Comment>|void
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function consumeWhiteSpace()
public function consumeWhiteSpace($consumeComments = true)
{
$comments = [];
do {
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
$this->consume(1);
}

if (!$consumeComments) {
return [];
}

if ($this->oParserSettings->bLenientParsing) {
try {
$oComment = $this->consumeComment();
2 changes: 1 addition & 1 deletion src/Rule/Rule.php
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@ public static function parse(ParserState $oParserState)
while ($oParserState->comes(';')) {
$oParserState->consume(';');
}
$oParserState->consumeWhiteSpace();
$oParserState->consumeWhiteSpace(false);

return $oRule;
}
14 changes: 14 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
@@ -1120,6 +1120,20 @@ public function flatCommentExtracting()
self::assertSame("Find Me!", $comments[0]->getComment());
}

/**
* @test
*/
public function innerCommentExtracting()
{
$parser = new Parser('div {left:10px;/*Find Me!*/text-align:left;}');
$doc = $parser->parse();
$contents = $doc->getContents();
$divRules = $contents[0]->getRules();
$comments = $divRules[1]->getComments();
self::assertCount(1, $comments);
self::assertEquals("Find Me!", $comments[0]->getComment());
}

/**
* @test
*/