Skip to content

Add options for adding delimiters in output formatter #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
45 changes: 39 additions & 6 deletions lib/Sabberworm/CSS/OutputFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,6 +40,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 = ' ';
Expand All @@ -43,7 +52,12 @@ class OutputFormat {
public $sSpaceAfterListArgumentSeparator = '';

public $sSpaceBeforeOpeningBrace = ' ';


// Content injected in and around declaration blocks.
public $sBeforeDeclarationBlock = '';
public $sAfterDeclarationBlockSelectors = '';
public $sAfterDeclarationBlock = '';

/**
* Indentation
*/
Expand Down Expand Up @@ -141,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;
}
}

Expand Down
11 changes: 10 additions & 1 deletion lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public function setSelector($mSelector) {
$this->setSelectors($mSelector);
}

/**
* Get selectors.
*
* @return Selector[] Selectors.
*/
public function getSelectors() {
return $this->aSelectors;
}
Expand Down Expand Up @@ -610,9 +615,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;
}

Expand Down
10 changes: 6 additions & 4 deletions lib/Sabberworm/CSS/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -106,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();
Expand All @@ -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 Rule[] Rules.
*/
public function getRulesAssoc($mRule = null) {
$aResult = array();
Expand All @@ -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();
Expand Down