Skip to content

Commit 9e23f77

Browse files
committed
Add option in OutputFormat to allow keeping comments
1 parent 0f4a139 commit 9e23f77

File tree

4 files changed

+131
-16
lines changed

4 files changed

+131
-16
lines changed

lib/Sabberworm/CSS/OutputFormat.php

+27
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ class OutputFormat {
7676
*/
7777
public $sIndentation = "\t";
7878

79+
/**
80+
* Indicates if comments should be kept or thrown away
81+
* @var bool
82+
*/
83+
private $bKeepComments = false;
84+
7985
/**
8086
* Output exceptions.
8187
* @var bool
@@ -220,6 +226,26 @@ public function level()
220226
return $this->iIndentationLevel;
221227
}
222228

229+
/**
230+
* Indicates if comments should be kept or thrown away
231+
* @param bool $toggle
232+
* @return $this
233+
*/
234+
public function setKeepComments($toggle)
235+
{
236+
$this->bKeepComments = $toggle;
237+
return $this;
238+
}
239+
240+
/**
241+
* Indicates if comments should be kept or thrown away
242+
* @return bool
243+
*/
244+
public function getKeepComments()
245+
{
246+
return $this->bKeepComments;
247+
}
248+
223249
/**
224250
* @return OutputFormat
225251
*/
@@ -247,6 +273,7 @@ public static function createCompact()
247273
public static function createPretty()
248274
{
249275
return self::create()
276+
->setKeepComments(true)
250277
->set('Space*Rules', "\n")
251278
->set('Space*Blocks', "\n")
252279
->setSpaceBetweenBlocks("\n\n")

lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,29 @@ public function render(OutputFormat $oOutputFormat) {
604604
// If all the selectors have been removed, this declaration block becomes invalid
605605
throw new OutputException("Attempt to print declaration block with missing selector", $this->iLineNo);
606606
}
607-
$sResult = $oOutputFormat->implode($oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(), $this->aSelectors) . $oOutputFormat->spaceBeforeOpeningBrace() . '{';
607+
608+
$sResult = '';
609+
610+
// render comments
611+
if ($oOutputFormat->getKeepComments()) {
612+
$comments = $this->getCommentsBefore();
613+
if (!empty($comments)) {
614+
$sResult .= implode(
615+
'',
616+
array_map(
617+
function (Comment $comment) use ($oOutputFormat) {
618+
return '/*' . $comment->getComment() . "*/\n";
619+
},
620+
$comments
621+
)
622+
);
623+
}
624+
}
625+
626+
$sResult .= $oOutputFormat->implode(
627+
$oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(),
628+
$this->aSelectors
629+
) . $oOutputFormat->spaceBeforeOpeningBrace() . '{';
608630
$sResult .= parent::render($oOutputFormat);
609631
$sResult .= '}';
610632
return $sResult;

lib/Sabberworm/CSS/RuleSet/RuleSet.php

+61-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sabberworm\CSS\RuleSet;
44

5+
use Sabberworm\CSS\Comment\Comment;
56
use Sabberworm\CSS\Rule\Rule;
67
use Sabberworm\CSS\Renderable;
78
use Sabberworm\CSS\Comment\Commentable;
@@ -47,12 +48,20 @@ public function addRule(Rule $oRule, Rule $oSibling = null) {
4748
array_splice($this->aRules[$sRule], $iPosition, 0, array($oRule));
4849
}
4950

50-
/**
51-
* Returns all rules matching the given rule name
52-
* @param (null|string|Rule) $mRule pattern to search for. If null, returns all rules. if the pattern ends with a dash, all rules starting with the pattern are returned as well as one matching the pattern with the dash excluded. passing a Rule behaves like calling getRules($mRule->getRule()).
53-
* @example $oRuleSet->getRules('font-') //returns an array of all rules either beginning with font- or matching font.
54-
* @example $oRuleSet->getRules('font') //returns array(0 => $oRule, …) or array().
55-
*/
51+
/**
52+
* Returns all rules matching the given rule name
53+
*
54+
* @param (null|string|Rule) $mRule pattern to search for.
55+
* If null, returns all rules. if the pattern ends with a dash, all rules starting with the pattern are
56+
* returned as well as one matching the pattern with the dash excluded.
57+
* Passing a Rule behaves like calling getRules($mRule->getRule()).
58+
*
59+
* @example $oRuleSet->getRules('font-') //returns an array of all rules either beginning with font- or matching
60+
* font.
61+
* @example $oRuleSet->getRules('font') //returns array(0 => $oRule, …) or array().
62+
*
63+
* @return array
64+
*/
5665
public function getRules($mRule = null) {
5766
if ($mRule instanceof Rule) {
5867
$mRule = $mRule->getRule();
@@ -78,11 +87,21 @@ public function setRules(array $aRules) {
7887
}
7988
}
8089

81-
/**
82-
* Returns all rules matching the given pattern and returns them in an associative array with the rule’s name as keys. This method exists mainly for backwards-compatibility and is really only partially useful.
83-
* @param (string) $mRule pattern to search for. If null, returns all rules. if the pattern ends with a dash, all rules starting with the pattern are returned as well as one matching the pattern with the dash excluded. passing a Rule behaves like calling getRules($mRule->getRule()).
84-
* Note: This method loses some information: Calling this (with an argument of 'background-') on a declaration block like { background-color: green; background-color; rgba(0, 127, 0, 0.7); } will only yield an associative array containing the rgba-valued rule while @link{getRules()} would yield an indexed array containing both.
85-
*/
90+
/**
91+
* Returns all rules matching the given pattern and returns them in an associative array with the rule’s name as
92+
* keys. This method exists mainly for backwards-compatibility and is really only partially useful.
93+
*
94+
* @param (string) $mRule pattern to search for.
95+
* If null, returns all rules. if the pattern ends with a dash, all rules starting with the pattern are
96+
* returned as well as one matching the pattern with the dash excluded.
97+
* Passing a Rule behaves like calling getRules($mRule->getRule()).
98+
* Note: This method loses some information: Calling this (with an argument of 'background-')
99+
* on a declaration block like { background-color: green; background-color; rgba(0, 127, 0, 0.7); }
100+
* will only yield an associative array containing the rgba-valued rule while @link{getRules()}
101+
* would yield an indexed array containing both.
102+
*
103+
* @return array
104+
*/
86105
public function getRulesAssoc($mRule = null) {
87106
$aResult = array();
88107
foreach($this->getRules($mRule) as $oRule) {
@@ -92,9 +111,19 @@ public function getRulesAssoc($mRule = null) {
92111
}
93112

94113
/**
95-
* Remove a rule from this RuleSet. This accepts all the possible values that @link{getRules()} accepts. If given a Rule, it will only remove this particular rule (by identity). If given a name, it will remove all rules by that name. Note: this is different from pre-v.2.0 behaviour of PHP-CSS-Parser, where passing a Rule instance would remove all rules with the same name. To get the old behvaiour, use removeRule($oRule->getRule()).
96-
* @param (null|string|Rule) $mRule pattern to remove. If $mRule is null, all rules are removed. If the pattern ends in a dash, all rules starting with the pattern are removed as well as one matching the pattern with the dash excluded. Passing a Rule behaves matches by identity.
97-
*/
114+
* Remove a rule from this RuleSet.
115+
*
116+
* This accepts all the possible values that @link{getRules()} accepts.
117+
* If given a Rule, it will only remove this particular rule (by identity).
118+
* If given a name, it will remove all rules by that name.
119+
* Note: this is different from pre-v.2.0 behaviour of PHP-CSS-Parser, where passing a Rule instance would
120+
* remove all rules with the same name. To get the old behvaiour, use removeRule($oRule->getRule()).
121+
*
122+
* @param (null|string|Rule) $mRule pattern to remove.
123+
* If $mRule is null, all rules are removed. If the pattern ends in a dash, all rules starting with the pattern
124+
* are removed as well as one matching the pattern with the dash excluded.
125+
* Passing a Rule behaves matches by identity.
126+
*/
98127
public function removeRule($mRule) {
99128
if($mRule instanceof Rule) {
100129
$sRule = $mRule->getRule();
@@ -140,7 +169,7 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
140169
$sResult .= $sRendered;
141170
}
142171
}
143-
172+
144173
if(!$bIsFirst) {
145174
// Had some output
146175
$sResult .= $oOutputFormat->spaceAfterRules();
@@ -170,4 +199,21 @@ public function setComments(array $aComments) {
170199
$this->aComments = $aComments;
171200
}
172201

202+
/**
203+
* Returns all comments that were declared before this Rule
204+
* @return Comment[]
205+
*/
206+
public function getCommentsBefore()
207+
{
208+
$lineNo = $this->getLineNo();
209+
$return = array();
210+
/** @var Comment $comment */
211+
foreach ($this->aComments as $comment) {
212+
if ($comment->getLineNo() <= $lineNo) {
213+
$return[] = $comment;
214+
}
215+
}
216+
217+
return $return;
218+
}
173219
}

tests/Sabberworm/CSS/OutputFormatTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class OutputFormatTest extends \PHPUnit_Framework_TestCase
1515

1616
private static $testCSS = <<<EOT
1717
18+
/**
19+
* LICENSE comment
20+
*/
1821
.main, .test {
1922
font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;
2023
background: white;
@@ -53,6 +56,23 @@ public function testCompact()
5356
);
5457
}
5558

59+
public function testCompactWithComments()
60+
{
61+
$expected = <<<EOT
62+
/**
63+
* LICENSE comment
64+
*/
65+
.main,.test{font:italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background:white;}@media screen{.main{background-size:100% 100%;font-size:1.3em;background-color:#fff;}}
66+
EOT;
67+
$this->assertSame(
68+
$expected,
69+
$this->oDocument->render(
70+
OutputFormat::createCompact()
71+
->setKeepComments(true)
72+
)
73+
);
74+
}
75+
5676
public function testPretty()
5777
{
5878
$this->assertSame(

0 commit comments

Comments
 (0)