Skip to content

Commit 6b6640f

Browse files
committed
Fixed Unit testing + Fixed issue when unit testing + added empty line between comments except last one
1 parent cbb0a9d commit 6b6640f

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

src/CSSList/CSSList.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ public static function parseList(ParserState $oParserState, CSSList $oList)
6969
$oParserState = new ParserState($oParserState, Settings::create());
7070
}
7171
$bLenientParsing = $oParserState->getSettings()->bLenientParsing;
72+
$comments=[];
7273
while (!$oParserState->isEnd()) {
73-
$comments = $oParserState->consumeWhiteSpace();
74+
if (empty($comments)) {
75+
$comments = $oParserState->consumeWhiteSpace();
76+
}
7477
$oListItem = null;
7578
if ($bLenientParsing) {
7679
try {
@@ -89,6 +92,7 @@ public static function parseList(ParserState $oParserState, CSSList $oList)
8992
$oListItem->setComments($comments);
9093
$oList->append($oListItem);
9194
}
95+
$comments = $oParserState->consumeWhiteSpace();
9296
}
9397
if (!$bIsRoot && !$bLenientParsing) {
9498
throw new SourceException("Unexpected end of document", $oParserState->currentLine());
@@ -413,11 +417,15 @@ public function render(OutputFormat $oOutputFormat)
413417
$sRendered = $oOutputFormat->safely(function () use ($oNextLevel, $oContent) {
414418
$sResult = '';
415419
$aComments = $oContent->getComments();
420+
$c = count($aComments);
416421

417-
foreach ($aComments as $oComment)
422+
foreach ($aComments as $i => $oComment)
418423
{
419424
$sResult .= $oComment->render($oNextLevel);
420425
$sResult .= $oNextLevel->spaceAfterBlocks();
426+
if ($c-1 !== $i) {
427+
$sResult .= $oNextLevel->spaceAfterBlocks();
428+
}
421429
}
422430
return $sResult . $oContent->render($oNextLevel);
423431
});

src/Rule/Rule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public static function parse(ParserState $oParserState)
106106
while ($oParserState->comes(';')) {
107107
$oParserState->consume(';');
108108
}
109+
$oParserState->consumeWhiteSpace();
109110

110111
return $oRule;
111112
}

src/RuleSet/RuleSet.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,15 @@ public function render(OutputFormat $oOutputFormat)
275275
$sRendered = $oOutputFormat->safely(function () use ($oRule, $oOutputFormat) {
276276
$sResult = '';
277277
$aComments = $oRule->getComments();
278+
$c = count($aComments);
278279

279-
foreach ($aComments as $oComment)
280+
foreach ($aComments as $i => $oComment)
280281
{
281282
$sResult .= $oComment->render($oOutputFormat);
282283
$sResult .= $oOutputFormat->nextLevel()->spaceBeforeRules();
284+
if ($c-1 !== $i) {
285+
$sResult .= $oOutputFormat->nextLevel()->spaceBeforeRules();
286+
}
283287
}
284288
return $sResult . $oRule->render($oOutputFormat->nextLevel());
285289
});

tests/ParserTest.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,17 @@ public function manipulation()
309309
. "\n"
310310
. ' domain(mozilla.org),'
311311
. "\n"
312-
. ' regexp("https:.*") {body {color: purple;background: yellow;}}'
312+
. ' regexp("https:.*") {/* CSS rules here apply to:'
313+
. "\n"
314+
. ' + The page "https://www.w3.org/".'
315+
. "\n"
316+
. ' + Any page whose URL begins with "https://www.w3.org/Style/"'
317+
. "\n"
318+
. ' + Any page whose URL\'s host is "mozilla.org" or ends with'
319+
. "\n"
320+
. ' ".mozilla.org"'
321+
. "\n"
322+
. ' + Any page whose URL starts with "https:" *//* make the above-mentioned pages really ugly */body {color: purple;background: yellow;}}'
313323
. "\n"
314324
. '@media screen and (orientation: landscape) {@-ms-viewport {width: 1024px;height: 768px;}}'
315325
. "\n"
@@ -348,7 +358,17 @@ public function manipulation()
348358
. "\n"
349359
. ' domain(mozilla.org),'
350360
. "\n"
351-
. ' regexp("https:.*") {#my_id body {color: purple;background: yellow;}}'
361+
. ' regexp("https:.*") {/* CSS rules here apply to:'
362+
. "\n"
363+
. ' + The page "https://www.w3.org/".'
364+
. "\n"
365+
. ' + Any page whose URL begins with "https://www.w3.org/Style/"'
366+
. "\n"
367+
. ' + Any page whose URL\'s host is "mozilla.org" or ends with'
368+
. "\n"
369+
. ' ".mozilla.org"'
370+
. "\n"
371+
. ' + Any page whose URL starts with "https:" *//* make the above-mentioned pages really ugly */#my_id body {color: purple;background: yellow;}}'
352372
. "\n"
353373
. '@media screen and (orientation: landscape) {@-ms-viewport {width: 1024px;height: 768px;}}'
354374
. "\n"
@@ -532,9 +552,9 @@ public function createShorthands()
532552
public function namespaces()
533553
{
534554
$oDoc = $this->parsedStructureForFile('namespaces');
535-
$sExpected = '@namespace toto "http://toto.example.org";
555+
$sExpected = '/* From the spec at https://www.w3.org/TR/css3-namespace/ */@namespace toto "http://toto.example.org";
536556
@namespace "http://example.com/foo";
537-
@namespace foo url("http://www.example.com/");
557+
/* From an introduction at https://www.blooberry.com/indexdot/css/syntax/atrules/namespace.htm */@namespace foo url("http://www.example.com/");
538558
@namespace foo url("http://www.example.com/");
539559
foo|test {gaga: 1;}
540560
|test {gaga: 2;}';
@@ -626,9 +646,13 @@ public function selectorRemoval()
626646
public function comments()
627647
{
628648
$oDoc = $this->parsedStructureForFile('comments');
629-
$sExpected = '@import url("some/url.css") screen;
630-
.foo, #bar {background-color: #000;}
631-
@media screen {#foo.bar {position: absolute;}}';
649+
$sExpected = <<<EXPECTED
650+
/**
651+
* Comments Hell.
652+
*/@import url("some/url.css") screen;
653+
.foo, #bar {/* Number 6 */background-color: #000;}
654+
@media screen {/** Number 10 **/#foo.bar {/** Number 10b **/position: absolute;}}
655+
EXPECTED;
632656
self::assertSame($sExpected, $oDoc->render());
633657
}
634658

0 commit comments

Comments
 (0)