Skip to content

Commit 21ac4d9

Browse files
committed
Fixed comments parsing and test added
1 parent 7f03a04 commit 21ac4d9

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,10 @@ private function consumeWhiteSpace() {
546546

547547
private function consumeComment() {
548548
if ($this->comes('/*')) {
549-
$this->consume(2);
550-
while ($this->consumeUntil('*', false, true)) {
551-
if ($this->comes('/')) {
552-
$this->consume(1);
549+
$this->consume(1);
550+
while ($this->consume(1) !== '') {
551+
if ($this->comes('*/')) {
552+
$this->consume(2);
553553
return true;
554554
}
555555
}
@@ -567,6 +567,7 @@ private function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false)
567567
$start = $this->iCurrentPosition;
568568

569569
while (($char = $this->consume(1)) !== '') {
570+
$this->consumeComment();
570571
if (in_array($char, $aEnd)) {
571572
if ($bIncludeEnd) {
572573
$out .= $char;

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ function testListValueRemoval() {
343343
}#unrelated {other: yes;}' . "\n", $oDoc->__toString());
344344
}
345345

346+
function testComments() {
347+
$oDoc = $this->parsedStructureForFile('comments');
348+
$sExpected = '@import url("some/url.css") screen;.foo, #bar {background-color: #000;}
349+
@media screen {#foo.bar {position: absolute;}
350+
}';
351+
$this->assertSame($sExpected, $oDoc->__toString());
352+
}
353+
346354
function parsedStructureForFile($sFileName) {
347355
$sFile = dirname(__FILE__) . '/../../files' . DIRECTORY_SEPARATOR . "$sFileName.css";
348356
$oParser = new Parser(file_get_contents($sFile));

tests/files/comments.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Comments Hell.
3+
*/
4+
@import /* Number 1 */"some/url.css"/* Number 2 */ screen/* Number 3 */;
5+
6+
.foo, /* Number 4 */ #bar/* Number 5 */ {
7+
background-color/* Number 6 */: #000/* Number 7 */;
8+
}
9+
10+
@media /* Number 8 */screen /* Number 9 */{
11+
/** Number 10 **/
12+
#foo.bar {
13+
position: absolute;/**/
14+
}
15+
}
16+
/** Number 11 **/

0 commit comments

Comments
 (0)