Skip to content

Commit d82e408

Browse files
committed
Make rendering comments an option in OutputFormat
1 parent afabc04 commit d82e408

16 files changed

+176
-91
lines changed

src/CSSList/AtRuleBlockList.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ public function __toString()
6161
*/
6262
public function render(OutputFormat $oOutputFormat)
6363
{
64+
$sResult = $oOutputFormat->comments($this);
65+
$sResult .= $oOutputFormat->sBeforeAtRuleBlock;
6466
$sArgs = $this->sArgs;
6567
if ($sArgs) {
6668
$sArgs = ' ' . $sArgs;
6769
}
68-
$sResult = $oOutputFormat->sBeforeAtRuleBlock;
6970
$sResult .= "@{$this->sType}$sArgs{$oOutputFormat->spaceBeforeOpeningBrace()}{";
70-
$sResult .= parent::render($oOutputFormat);
71+
$sResult .= $this->renderListContents($oOutputFormat);
7172
$sResult .= '}';
7273
$sResult .= $oOutputFormat->sAfterAtRuleBlock;
7374
return $sResult;

src/CSSList/CSSList.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,9 @@ public static function parseList(ParserState $oParserState, CSSList $oList)
6969
$oParserState = new ParserState($oParserState, Settings::create());
7070
}
7171
$bLenientParsing = $oParserState->getSettings()->bLenientParsing;
72-
$comments = [];
72+
$aComments = [];
7373
while (!$oParserState->isEnd()) {
74-
if (empty($comments)) {
75-
$comments = $oParserState->consumeWhiteSpace();
76-
}
74+
$aComments = array_merge($aComments, $oParserState->consumeWhiteSpace());
7775
$oListItem = null;
7876
if ($bLenientParsing) {
7977
try {
@@ -89,11 +87,12 @@ public static function parseList(ParserState $oParserState, CSSList $oList)
8987
return;
9088
}
9189
if ($oListItem) {
92-
$oListItem->setComments($comments);
90+
$oListItem->addComments($aComments);
9391
$oList->append($oListItem);
9492
}
95-
$comments = $oParserState->consumeWhiteSpace();
93+
$aComments = $oParserState->consumeWhiteSpace();
9694
}
95+
$oList->addComments($aComments);
9796
if (!$bIsRoot && !$bLenientParsing) {
9897
throw new SourceException("Unexpected end of document", $oParserState->currentLine());
9998
}
@@ -175,10 +174,10 @@ private static function parseAtRule(ParserState $oParserState)
175174
$oParserState->consumeUntil([';', ParserState::EOF], true, true);
176175
return new Import($oLocation, $sMediaQuery ?: null, $iIdentifierLineNum);
177176
} elseif ($sIdentifier === 'charset') {
178-
$sCharset = CSSString::parse($oParserState);
177+
$oCharsetString = CSSString::parse($oParserState);
179178
$oParserState->consumeWhiteSpace();
180179
$oParserState->consumeUntil([';', ParserState::EOF], true, true);
181-
return new Charset($sCharset, $iIdentifierLineNum);
180+
return new Charset($oCharsetString, $iIdentifierLineNum);
182181
} elseif (self::identifierIs($sIdentifier, 'keyframes')) {
183182
$oResult = new KeyFrame($iIdentifierLineNum);
184183
$oResult->setVendorKeyFrame($sIdentifier);
@@ -405,7 +404,7 @@ public function __toString()
405404
/**
406405
* @return string
407406
*/
408-
public function render(OutputFormat $oOutputFormat)
407+
protected function renderListContents(OutputFormat $oOutputFormat)
409408
{
410409
$sResult = '';
411410
$bIsFirst = true;
@@ -415,18 +414,7 @@ public function render(OutputFormat $oOutputFormat)
415414
}
416415
foreach ($this->aContents as $oContent) {
417416
$sRendered = $oOutputFormat->safely(function () use ($oNextLevel, $oContent) {
418-
$sResult = '';
419-
$aComments = $oContent->getComments();
420-
$c = count($aComments);
421-
422-
foreach ($aComments as $i => $oComment) {
423-
$sResult .= $oComment->render($oNextLevel);
424-
$sResult .= $oNextLevel->spaceAfterBlocks();
425-
if ($c - 1 !== $i) {
426-
$sResult .= $oNextLevel->spaceAfterBlocks();
427-
}
428-
}
429-
return $sResult . $oContent->render($oNextLevel);
417+
return $oContent->render($oNextLevel);
430418
});
431419
if ($sRendered === null) {
432420
continue;

src/CSSList/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function render(OutputFormat $oOutputFormat = null)
159159
if ($oOutputFormat === null) {
160160
$oOutputFormat = new OutputFormat();
161161
}
162-
return parent::render($oOutputFormat);
162+
return $oOutputFormat->comments($this) . $this->renderListContents($oOutputFormat);
163163
}
164164

165165
/**

src/CSSList/KeyFrame.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ public function __toString()
7272
*/
7373
public function render(OutputFormat $oOutputFormat)
7474
{
75-
$sResult = "@{$this->vendorKeyFrame} {$this->animationName}{$oOutputFormat->spaceBeforeOpeningBrace()}{";
76-
$sResult .= parent::render($oOutputFormat);
75+
$sResult = $oOutputFormat->comments($this);
76+
$sResult .= "@{$this->vendorKeyFrame} {$this->animationName}{$oOutputFormat->spaceBeforeOpeningBrace()}{";
77+
$sResult .= $this->renderListContents($oOutputFormat);
7778
$sResult .= '}';
7879
return $sResult;
7980
}

src/OutputFormat.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ class OutputFormat
143143
*/
144144
public $bIgnoreExceptions = false;
145145

146+
/**
147+
* Render comments for lists and RuleSets
148+
*
149+
* @var bool
150+
*/
151+
public $bRenderComments = false;
152+
146153
/**
147154
* @var OutputFormatter|null
148155
*/
@@ -314,8 +321,12 @@ public static function create()
314321
public static function createCompact()
315322
{
316323
$format = self::create();
317-
$format->set('Space*Rules', "")->set('Space*Blocks', "")->setSpaceAfterRuleName('')
318-
->setSpaceBeforeOpeningBrace('')->setSpaceAfterSelectorSeparator('');
324+
$format->set('Space*Rules', "")
325+
->set('Space*Blocks', "")
326+
->setSpaceAfterRuleName('')
327+
->setSpaceBeforeOpeningBrace('')
328+
->setSpaceAfterSelectorSeparator('')
329+
->setRenderComments(false);
319330
return $format;
320331
}
321332

@@ -327,8 +338,11 @@ public static function createCompact()
327338
public static function createPretty()
328339
{
329340
$format = self::create();
330-
$format->set('Space*Rules', "\n")->set('Space*Blocks', "\n")
331-
->setSpaceBetweenBlocks("\n\n")->set('SpaceAfterListArgumentSeparator', ['default' => '', ',' => ' ']);
341+
$format->set('Space*Rules', "\n")
342+
->set('Space*Blocks', "\n")
343+
->setSpaceBetweenBlocks("\n\n")
344+
->set('SpaceAfterListArgumentSeparator', ['default' => '', ',' => ' '])
345+
->setRenderComments(true);
332346
return $format;
333347
}
334348
}

src/OutputFormatter.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sabberworm\CSS;
44

5+
use Sabberworm\CSS\Comment\Commentable;
56
use Sabberworm\CSS\Parsing\OutputException;
67

78
class OutputFormatter
@@ -211,6 +212,28 @@ public function removeLastSemicolon($sString)
211212
return implode(';', $sString);
212213
}
213214

215+
/**
216+
*
217+
* @param array<Commentable> $aComments
218+
* @return string
219+
*/
220+
public function comments(Commentable $oCommentable)
221+
{
222+
if (!$this->oFormat->bRenderComments) {
223+
return '';
224+
}
225+
226+
$sResult = '';
227+
$aComments = $oCommentable->getComments();
228+
$iLastCommentIndex = count($aComments) - 1;
229+
230+
foreach ($aComments as $i => $oComment) {
231+
$sResult .= $oComment->render($this->oFormat);
232+
$sResult .= $i === $iLastCommentIndex ? $this->spaceAfterBlocks() : $this->spaceBetweenBlocks();
233+
}
234+
return $sResult;
235+
}
236+
214237
/**
215238
* @param string $sSpaceString
216239
*

src/Parsing/ParserState.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function parseCharacter($bIsForIdentifier)
204204
*/
205205
public function consumeWhiteSpace()
206206
{
207-
$comments = [];
207+
$aComments = [];
208208
do {
209209
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
210210
$this->consume(1);
@@ -214,16 +214,16 @@ public function consumeWhiteSpace()
214214
$oComment = $this->consumeComment();
215215
} catch (UnexpectedEOFException $e) {
216216
$this->iCurrentPosition = $this->iLength;
217-
return;
217+
return $aComments;
218218
}
219219
} else {
220220
$oComment = $this->consumeComment();
221221
}
222222
if ($oComment !== false) {
223-
$comments[] = $oComment;
223+
$aComments[] = $oComment;
224224
}
225225
} while ($oComment !== false);
226-
return $comments;
226+
return $aComments;
227227
}
228228

229229
/**

src/Property/Charset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function __toString()
8282
*/
8383
public function render(OutputFormat $oOutputFormat)
8484
{
85-
return "@charset {$this->sCharset->render($oOutputFormat)};";
85+
return "{$oOutputFormat->comments($this)}@charset {$this->oCharset->render($oOutputFormat)};";
8686
}
8787

8888
/**

src/Property/Import.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function __toString()
8383
*/
8484
public function render(OutputFormat $oOutputFormat)
8585
{
86-
return "@import " . $this->oLocation->render($oOutputFormat)
86+
return $oOutputFormat->comments($this) . "@import " . $this->oLocation->render($oOutputFormat)
8787
. ($this->sMediaQuery === null ? '' : ' ' . $this->sMediaQuery) . ';';
8888
}
8989

src/Rule/Rule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ public function __toString()
346346
*/
347347
public function render(OutputFormat $oOutputFormat)
348348
{
349-
$sResult = "{$this->sRule}:{$oOutputFormat->spaceAfterRuleName()}";
350-
if ($this->mValue instanceof Value) { //Can also be a ValueList
349+
$sResult = "{$oOutputFormat->comments($this)}{$this->sRule}:{$oOutputFormat->spaceAfterRuleName()}";
350+
if ($this->mValue instanceof Value) { // Can also be a ValueList
351351
$sResult .= $this->mValue->render($oOutputFormat);
352352
} else {
353353
$sResult .= $this->mValue;

src/RuleSet/AtRuleSet.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ public function __toString()
6161
*/
6262
public function render(OutputFormat $oOutputFormat)
6363
{
64+
$sResult = $oOutputFormat->comments($this);
6465
$sArgs = $this->sArgs;
6566
if ($sArgs) {
6667
$sArgs = ' ' . $sArgs;
6768
}
68-
$sResult = "@{$this->sType}$sArgs{$oOutputFormat->spaceBeforeOpeningBrace()}{";
69-
$sResult .= parent::render($oOutputFormat);
69+
$sResult .= "@{$this->sType}$sArgs{$oOutputFormat->spaceBeforeOpeningBrace()}{";
70+
$sResult .= $this->renderRules($oOutputFormat);
7071
$sResult .= '}';
7172
return $sResult;
7273
}

src/RuleSet/DeclarationBlock.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,18 +812,19 @@ public function __toString()
812812
*/
813813
public function render(OutputFormat $oOutputFormat)
814814
{
815+
$sResult = $oOutputFormat->comments($this);
815816
if (count($this->aSelectors) === 0) {
816817
// If all the selectors have been removed, this declaration block becomes invalid
817818
throw new OutputException("Attempt to print declaration block with missing selector", $this->iLineNo);
818819
}
819-
$sResult = $oOutputFormat->sBeforeDeclarationBlock;
820+
$sResult .= $oOutputFormat->sBeforeDeclarationBlock;
820821
$sResult .= $oOutputFormat->implode(
821822
$oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(),
822823
$this->aSelectors
823824
);
824825
$sResult .= $oOutputFormat->sAfterDeclarationBlockSelectors;
825826
$sResult .= $oOutputFormat->spaceBeforeOpeningBrace() . '{';
826-
$sResult .= parent::render($oOutputFormat);
827+
$sResult .= $this->renderRules($oOutputFormat);
827828
$sResult .= '}';
828829
$sResult .= $oOutputFormat->sAfterDeclarationBlock;
829830
return $sResult;

src/RuleSet/RuleSet.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -266,34 +266,24 @@ public function __toString()
266266
/**
267267
* @return string
268268
*/
269-
public function render(OutputFormat $oOutputFormat)
269+
protected function renderRules(OutputFormat $oOutputFormat)
270270
{
271271
$sResult = '';
272272
$bIsFirst = true;
273+
$oNextLevel = $oOutputFormat->nextLevel();
273274
foreach ($this->aRules as $aRules) {
274275
foreach ($aRules as $oRule) {
275-
$sRendered = $oOutputFormat->safely(function () use ($oRule, $oOutputFormat) {
276-
$sResult = '';
277-
$aComments = $oRule->getComments();
278-
$c = count($aComments);
279-
280-
foreach ($aComments as $i => $oComment) {
281-
$sResult .= $oComment->render($oOutputFormat);
282-
$sResult .= $oOutputFormat->nextLevel()->spaceBeforeRules();
283-
if ($c - 1 !== $i) {
284-
$sResult .= $oOutputFormat->nextLevel()->spaceBeforeRules();
285-
}
286-
}
287-
return $sResult . $oRule->render($oOutputFormat->nextLevel());
276+
$sRendered = $oNextLevel->safely(function () use ($oRule, $oNextLevel) {
277+
return $oRule->render($oNextLevel);
288278
});
289279
if ($sRendered === null) {
290280
continue;
291281
}
292282
if ($bIsFirst) {
293283
$bIsFirst = false;
294-
$sResult .= $oOutputFormat->nextLevel()->spaceBeforeRules();
284+
$sResult .= $oNextLevel->spaceBeforeRules();
295285
} else {
296-
$sResult .= $oOutputFormat->nextLevel()->spaceBetweenRules();
286+
$sResult .= $oNextLevel->spaceBetweenRules();
297287
}
298288
$sResult .= $sRendered;
299289
}

0 commit comments

Comments
 (0)