Skip to content

Commit

Permalink
Fix comments being omitted in the middle of a rule
Browse files Browse the repository at this point in the history
  • Loading branch information
scq authored and derschatta committed Sep 13, 2021
1 parent 70024fb commit 44c3a55
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
9 changes: 8 additions & 1 deletion src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sabberworm\CSS\Parsing;

use phpDocumentor\Reflection\Types\Boolean;
use Sabberworm\CSS\Comment\Comment;
use Sabberworm\CSS\Settings;

Expand Down Expand Up @@ -197,18 +198,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();
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function parse(ParserState $oParserState)
while ($oParserState->comes(';')) {
$oParserState->consume(';');
}
$oParserState->consumeWhiteSpace();
$oParserState->consumeWhiteSpace(false);

return $oRule;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 44c3a55

Please sign in to comment.