Skip to content

Commit a7d232a

Browse files
authored
Merge pull request MyIntervals#153 from xwp/add/output-formatting
Add options for adding delimiters in output formatter
2 parents 3780f73 + 28e4274 commit a7d232a

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
3535
if($sArgs) {
3636
$sArgs = ' ' . $sArgs;
3737
}
38-
$sResult = "@{$this->sType}$sArgs{$oOutputFormat->spaceBeforeOpeningBrace()}{";
38+
$sResult = $oOutputFormat->sBeforeAtRuleBlock;
39+
$sResult .= "@{$this->sType}$sArgs{$oOutputFormat->spaceBeforeOpeningBrace()}{";
3940
$sResult .= parent::render($oOutputFormat);
4041
$sResult .= '}';
42+
$sResult .= $oOutputFormat->sAfterAtRuleBlock;
4143
return $sResult;
4244
}
4345

lib/Sabberworm/CSS/OutputFormat.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use Sabberworm\CSS\Parsing\OutputException;
66

7+
/**
8+
* Class OutputFormat
9+
*
10+
* @method OutputFormat setSemicolonAfterLastRule( bool $bSemicolonAfterLastRule ) Set whether semicolons are added after last rule.
11+
*/
712
class OutputFormat {
813
/**
914
* Value format
@@ -35,6 +40,10 @@ class OutputFormat {
3540
public $sSpaceAfterBlocks = '';
3641
public $sSpaceBetweenBlocks = "\n";
3742

43+
// Content injected in and around @-rule blocks.
44+
public $sBeforeAtRuleBlock = '';
45+
public $sAfterAtRuleBlock = '';
46+
3847
// This is what’s printed before and after the comma if a declaration block contains multiple selectors.
3948
public $sSpaceBeforeSelectorSeparator = '';
4049
public $sSpaceAfterSelectorSeparator = ' ';
@@ -43,7 +52,12 @@ class OutputFormat {
4352
public $sSpaceAfterListArgumentSeparator = '';
4453

4554
public $sSpaceBeforeOpeningBrace = ' ';
46-
55+
56+
// Content injected in and around declaration blocks.
57+
public $sBeforeDeclarationBlock = '';
58+
public $sAfterDeclarationBlockSelectors = '';
59+
public $sAfterDeclarationBlock = '';
60+
4761
/**
4862
* Indentation
4963
*/
@@ -141,17 +155,36 @@ public function getFormatter() {
141155
public function level() {
142156
return $this->iIndentationLevel;
143157
}
144-
158+
159+
/**
160+
* Create format.
161+
*
162+
* @return OutputFormat Format.
163+
*/
145164
public static function create() {
146165
return new OutputFormat();
147166
}
148-
167+
168+
/**
169+
* Create compact format.
170+
*
171+
* @return OutputFormat Format.
172+
*/
149173
public static function createCompact() {
150-
return self::create()->set('Space*Rules', "")->set('Space*Blocks', "")->setSpaceAfterRuleName('')->setSpaceBeforeOpeningBrace('')->setSpaceAfterSelectorSeparator('');
174+
$format = self::create();
175+
$format->set('Space*Rules', "")->set('Space*Blocks', "")->setSpaceAfterRuleName('')->setSpaceBeforeOpeningBrace('')->setSpaceAfterSelectorSeparator('');
176+
return $format;
151177
}
152-
178+
179+
/**
180+
* Create pretty format.
181+
*
182+
* @return OutputFormat Format.
183+
*/
153184
public static function createPretty() {
154-
return self::create()->set('Space*Rules', "\n")->set('Space*Blocks', "\n")->setSpaceBetweenBlocks("\n\n")->set('SpaceAfterListArgumentSeparator', array('default' => '', ',' => ' '));
185+
$format = self::create();
186+
$format->set('Space*Rules', "\n")->set('Space*Blocks', "\n")->setSpaceBetweenBlocks("\n\n")->set('SpaceAfterListArgumentSeparator', array('default' => '', ',' => ' '));
187+
return $format;
155188
}
156189
}
157190

lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public function setSelector($mSelector) {
7676
$this->setSelectors($mSelector);
7777
}
7878

79+
/**
80+
* Get selectors.
81+
*
82+
* @return Selector[] Selectors.
83+
*/
7984
public function getSelectors() {
8085
return $this->aSelectors;
8186
}
@@ -610,9 +615,13 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
610615
// If all the selectors have been removed, this declaration block becomes invalid
611616
throw new OutputException("Attempt to print declaration block with missing selector", $this->iLineNo);
612617
}
613-
$sResult = $oOutputFormat->implode($oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(), $this->aSelectors) . $oOutputFormat->spaceBeforeOpeningBrace() . '{';
618+
$sResult = $oOutputFormat->sBeforeDeclarationBlock;
619+
$sResult .= $oOutputFormat->implode($oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(), $this->aSelectors);
620+
$sResult .= $oOutputFormat->sAfterDeclarationBlockSelectors;
621+
$sResult .= $oOutputFormat->spaceBeforeOpeningBrace() . '{';
614622
$sResult .= parent::render($oOutputFormat);
615623
$sResult .= '}';
624+
$sResult .= $oOutputFormat->sAfterDeclarationBlock;
616625
return $sResult;
617626
}
618627

lib/Sabberworm/CSS/RuleSet/RuleSet.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function addRule(Rule $oRule, Rule $oSibling = null) {
8989
* @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()).
9090
* @example $oRuleSet->getRules('font-') //returns an array of all rules either beginning with font- or matching font.
9191
* @example $oRuleSet->getRules('font') //returns array(0 => $oRule, …) or array().
92+
* @return Rule[] Rules.
9293
*/
9394
public function getRules($mRule = null) {
9495
if ($mRule instanceof Rule) {
@@ -106,7 +107,7 @@ public function getRules($mRule = null) {
106107

107108
/**
108109
* Override all the rules of this set.
109-
* @param array $aRules The rules to override with.
110+
* @param Rule[] $aRules The rules to override with.
110111
*/
111112
public function setRules(array $aRules) {
112113
$this->aRules = array();
@@ -119,6 +120,7 @@ public function setRules(array $aRules) {
119120
* 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.
120121
* @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()).
121122
* 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.
123+
* @return Rule[] Rules.
122124
*/
123125
public function getRulesAssoc($mRule = null) {
124126
$aResult = array();
@@ -129,9 +131,9 @@ public function getRulesAssoc($mRule = null) {
129131
}
130132

131133
/**
132-
* 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()).
133-
* @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.
134-
*/
134+
* 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()).
135+
* @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.
136+
*/
135137
public function removeRule($mRule) {
136138
if($mRule instanceof Rule) {
137139
$sRule = $mRule->getRule();

0 commit comments

Comments
 (0)