From 032b86cc5f1b43969ea55a0ff6405cf7c8c67f26 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 15 Jan 2019 14:12:28 -0800 Subject: [PATCH 1/4] Extend output formatting with additional delimiter options Allow content to be added before/after at-rule blocks. Also allow content to be added before a declaration block, after its selectors, and then after the declaration block. --- lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php | 4 +++- lib/Sabberworm/CSS/OutputFormat.php | 11 ++++++++++- lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php | 6 +++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php b/lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php index a1ff8f8d1..24e79f02f 100644 --- a/lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php +++ b/lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php @@ -35,9 +35,11 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) { if($sArgs) { $sArgs = ' ' . $sArgs; } - $sResult = "@{$this->sType}$sArgs{$oOutputFormat->spaceBeforeOpeningBrace()}{"; + $sResult = $oOutputFormat->sBeforeAtRuleBlock; + $sResult .= "@{$this->sType}$sArgs{$oOutputFormat->spaceBeforeOpeningBrace()}{"; $sResult .= parent::render($oOutputFormat); $sResult .= '}'; + $sResult .= $oOutputFormat->sAfterAtRuleBlock; return $sResult; } diff --git a/lib/Sabberworm/CSS/OutputFormat.php b/lib/Sabberworm/CSS/OutputFormat.php index 45b97f529..7279bfb7b 100644 --- a/lib/Sabberworm/CSS/OutputFormat.php +++ b/lib/Sabberworm/CSS/OutputFormat.php @@ -35,6 +35,10 @@ class OutputFormat { public $sSpaceAfterBlocks = ''; public $sSpaceBetweenBlocks = "\n"; + // Content injected in and around @-rule blocks. + public $sBeforeAtRuleBlock = ''; + public $sAfterAtRuleBlock = ''; + // This is what’s printed before and after the comma if a declaration block contains multiple selectors. public $sSpaceBeforeSelectorSeparator = ''; public $sSpaceAfterSelectorSeparator = ' '; @@ -43,7 +47,12 @@ class OutputFormat { public $sSpaceAfterListArgumentSeparator = ''; public $sSpaceBeforeOpeningBrace = ' '; - + + // Content injected in and around declaration blocks. + public $sBeforeDeclarationBlock = ''; + public $sAfterDeclarationBlockSelectors = ''; + public $sAfterDeclarationBlock = ''; + /** * Indentation */ diff --git a/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php b/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php index 451eaa7e0..726d21548 100644 --- a/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php +++ b/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php @@ -610,9 +610,13 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) { // If all the selectors have been removed, this declaration block becomes invalid throw new OutputException("Attempt to print declaration block with missing selector", $this->iLineNo); } - $sResult = $oOutputFormat->implode($oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(), $this->aSelectors) . $oOutputFormat->spaceBeforeOpeningBrace() . '{'; + $sResult = $oOutputFormat->sBeforeDeclarationBlock; + $sResult .= $oOutputFormat->implode($oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(), $this->aSelectors); + $sResult .= $oOutputFormat->sAfterDeclarationBlockSelectors; + $sResult .= $oOutputFormat->spaceBeforeOpeningBrace() . '{'; $sResult .= parent::render($oOutputFormat); $sResult .= '}'; + $sResult .= $oOutputFormat->sAfterDeclarationBlock; return $sResult; } From 048af5b61b0ebd8a1eb910b3ae4e3a744c7038fa Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 15 Jan 2019 14:13:10 -0800 Subject: [PATCH 2/4] Improve phpdoc for methods --- lib/Sabberworm/CSS/OutputFormat.php | 34 ++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/Sabberworm/CSS/OutputFormat.php b/lib/Sabberworm/CSS/OutputFormat.php index 7279bfb7b..f7ebb5a57 100644 --- a/lib/Sabberworm/CSS/OutputFormat.php +++ b/lib/Sabberworm/CSS/OutputFormat.php @@ -4,6 +4,11 @@ use Sabberworm\CSS\Parsing\OutputException; +/** + * Class OutputFormat + * + * @method OutputFormat setSemicolonAfterLastRule( bool $bSemicolonAfterLastRule ) Set whether semicolons are added after last rule. + */ class OutputFormat { /** * Value format @@ -150,17 +155,36 @@ public function getFormatter() { public function level() { return $this->iIndentationLevel; } - + + /** + * Create format. + * + * @return OutputFormat Format. + */ public static function create() { return new OutputFormat(); } - + + /** + * Create compact format. + * + * @return OutputFormat Format. + */ public static function createCompact() { - return self::create()->set('Space*Rules', "")->set('Space*Blocks', "")->setSpaceAfterRuleName('')->setSpaceBeforeOpeningBrace('')->setSpaceAfterSelectorSeparator(''); + $format = self::create(); + $format->set('Space*Rules', "")->set('Space*Blocks', "")->setSpaceAfterRuleName('')->setSpaceBeforeOpeningBrace('')->setSpaceAfterSelectorSeparator(''); + return $format; } - + + /** + * Create pretty format. + * + * @return OutputFormat Format. + */ public static function createPretty() { - return self::create()->set('Space*Rules', "\n")->set('Space*Blocks', "\n")->setSpaceBetweenBlocks("\n\n")->set('SpaceAfterListArgumentSeparator', array('default' => '', ',' => ' ')); + $format = self::create(); + $format->set('Space*Rules', "\n")->set('Space*Blocks', "\n")->setSpaceBetweenBlocks("\n\n")->set('SpaceAfterListArgumentSeparator', array('default' => '', ',' => ' ')); + return $format; } } From c0800de1abb4229b444a1da287fa5a02adbb3183 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 15 Jan 2019 15:46:10 -0800 Subject: [PATCH 3/4] Add phpdoc return tags --- lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php | 5 +++++ lib/Sabberworm/CSS/RuleSet/RuleSet.php | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php b/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php index 726d21548..6614b1d1e 100644 --- a/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php +++ b/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php @@ -76,6 +76,11 @@ public function setSelector($mSelector) { $this->setSelectors($mSelector); } + /** + * Get selectors. + * + * @return Selector[] Selectors. + */ public function getSelectors() { return $this->aSelectors; } diff --git a/lib/Sabberworm/CSS/RuleSet/RuleSet.php b/lib/Sabberworm/CSS/RuleSet/RuleSet.php index 42b66509e..0f44b0912 100644 --- a/lib/Sabberworm/CSS/RuleSet/RuleSet.php +++ b/lib/Sabberworm/CSS/RuleSet/RuleSet.php @@ -89,6 +89,7 @@ public function addRule(Rule $oRule, Rule $oSibling = null) { * @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()). * @example $oRuleSet->getRules('font-') //returns an array of all rules either beginning with font- or matching font. * @example $oRuleSet->getRules('font') //returns array(0 => $oRule, …) or array(). + * @return Rule[] Rules. */ public function getRules($mRule = null) { if ($mRule instanceof Rule) { @@ -119,6 +120,7 @@ public function setRules(array $aRules) { * 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. * @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()). * 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. + * @return array Rules. */ public function getRulesAssoc($mRule = null) { $aResult = array(); @@ -129,9 +131,9 @@ public function getRulesAssoc($mRule = null) { } /** - * 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()). - * @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. - */ + * 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()). + * @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. + */ public function removeRule($mRule) { if($mRule instanceof Rule) { $sRule = $mRule->getRule(); From 28e4274cb108bedcdb0e8398a89122a63aafbb29 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 15 Jan 2019 15:47:49 -0800 Subject: [PATCH 4/4] Use Rule[] instead of generic array for return tags --- lib/Sabberworm/CSS/RuleSet/RuleSet.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Sabberworm/CSS/RuleSet/RuleSet.php b/lib/Sabberworm/CSS/RuleSet/RuleSet.php index 0f44b0912..e5d5e4154 100644 --- a/lib/Sabberworm/CSS/RuleSet/RuleSet.php +++ b/lib/Sabberworm/CSS/RuleSet/RuleSet.php @@ -107,7 +107,7 @@ public function getRules($mRule = null) { /** * Override all the rules of this set. - * @param array $aRules The rules to override with. + * @param Rule[] $aRules The rules to override with. */ public function setRules(array $aRules) { $this->aRules = array(); @@ -120,7 +120,7 @@ public function setRules(array $aRules) { * 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. * @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()). * 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. - * @return array Rules. + * @return Rule[] Rules. */ public function getRulesAssoc($mRule = null) { $aResult = array();