Skip to content

Commit 665d957

Browse files
author
Frederic Massart
committed
Handle comments glued to block and rules
1 parent cb58f3b commit 665d957

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ private function parseCharacter($bIsForIdentifier) {
298298
}
299299

300300
private function parseSelector() {
301-
$aComments = $this->consumeWhiteSpace();
301+
$aComments = array();
302302
$oResult = new DeclarationBlock($this->iLineNo);
303-
$oResult->setSelector($this->consumeUntil('{', false, true));
303+
$oResult->setSelector($this->consumeUntil('{', false, true, $aComments));
304304
$oResult->setComments($aComments);
305305
$this->parseRuleSet($oResult);
306306
return $oResult;
@@ -608,13 +608,12 @@ private function isEnd() {
608608
return $this->iCurrentPosition >= $this->iLength;
609609
}
610610

611-
private function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false) {
611+
private function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, array &$comments = array()) {
612612
$aEnd = is_array($aEnd) ? $aEnd : array($aEnd);
613613
$out = '';
614614
$start = $this->iCurrentPosition;
615615

616616
while (($char = $this->consume(1)) !== '') {
617-
$this->consumeComment();
618617
if (in_array($char, $aEnd)) {
619618
if ($bIncludeEnd) {
620619
$out .= $char;
@@ -624,6 +623,9 @@ private function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false)
624623
return $out;
625624
}
626625
$out .= $char;
626+
if ($comment = $this->consumeComment()) {
627+
$comments[] = $comment;
628+
}
627629
}
628630

629631
$this->iCurrentPosition = $start;

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,30 @@ function testCommentExtracting() {
542542
$fooBarComments = $mediaRules[0]->getComments();
543543
$this->assertCount(1, $fooBarComments);
544544
$this->assertEquals("* Number 10 *", $fooBarComments[0]->getComment());
545+
546+
// Media -> declaration -> rule.
547+
$fooBarRules = $mediaRules[0]->getRules();
548+
$fooBarChildComments = $fooBarRules[0]->getComments();
549+
$this->assertCount(1, $fooBarChildComments);
550+
$this->assertEquals("* Number 10b *", $fooBarChildComments[0]->getComment());
551+
}
552+
553+
function testFlatCommentExtracting() {
554+
$parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}');
555+
$doc = $parser->parse();
556+
$contents = $doc->getContents();
557+
$divRules = $contents[0]->getRules();
558+
$comments = $divRules[0]->getComments();
559+
$this->assertCount(1, $comments);
560+
$this->assertEquals("Find Me!", $comments[0]->getComment());
545561
}
546562

563+
function testTopLevelCommentExtracting() {
564+
$parser = new Parser('/*Find Me!*/div {left:10px; text-align:left;}');
565+
$doc = $parser->parse();
566+
$contents = $doc->getContents();
567+
$comments = $contents[0]->getComments();
568+
$this->assertCount(1, $comments);
569+
$this->assertEquals("Find Me!", $comments[0]->getComment());
570+
}
547571
}

tests/files/comments.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@media /* Number 8 */screen /* Number 9 */{
1111
/** Number 10 **/
1212
#foo.bar {
13+
/** Number 10b **/
1314
position: absolute;/**/
1415
}
1516
}

0 commit comments

Comments
 (0)