Skip to content

Commit

Permalink
Make Format mandatory when calling render()
Browse files Browse the repository at this point in the history
use __toString() for the default format except in \Sabberworm\CSS\CSSList\Document where null is still allowed

This removes all those null checks.
  • Loading branch information
sabberworm committed Jul 3, 2014
1 parent 0a67867 commit c87ffee
Show file tree
Hide file tree
Showing 21 changed files with 48 additions and 93 deletions.
9 changes: 3 additions & 6 deletions lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Sabberworm\CSS\Property\AtRule;

/**
* A RuleSet constructed by an unknown @-rule. @font-face rules are rendered into AtRule objects.
* A BlockList constructed by an unknown @-rule. @media rules are rendered into AtRuleBlockList objects.
*/
class AtRuleBlockList extends CSSBlockList implements AtRule {

Expand All @@ -27,13 +27,10 @@ public function atRuleArgs() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sResult = "@{$this->sType} {$this->sArgs}{";
$sResult .= parent::render($oOutputFormat);
$sResult .= '}';
Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sResult = '';
foreach ($this->aContents as $oContent) {
$sResult .= $oContent->render($oOutputFormat->nextLevel());
Expand Down
11 changes: 9 additions & 2 deletions lib/Sabberworm/CSS/CSSList/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,21 @@ public function expandShorthands() {
}
}

/*
/**
* Create shorthands properties whenever possible
*/

public function createShorthands() {
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) {
$oDeclaration->createShorthands();
}
}

// Override render() to make format argument optional
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
return parent::render($oOutputFormat);
}

}
8 changes: 2 additions & 6 deletions lib/Sabberworm/CSS/CSSList/KeyFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ public function getAnimationName() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {

if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sResult = "@{$this->vendorKeyFrame} {$this->animationName} {";
$sResult .= parent::render($oOutputFormat->nextLevel());
$sResult .= '}';
Expand Down
2 changes: 1 addition & 1 deletion lib/Sabberworm/CSS/Property/AtRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ interface AtRule {
public function atRuleName();
public function atRuleArgs();
public function __toString();
public function render($oOutputFormat = null);
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat);
}
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Property/CSSNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ public function __construct($mUrl, $sPrefix = null) {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
return '@namespace '.($this->sPrefix === null ? '' : $this->sPrefix.' ').$this->mUrl->render($oOutputFormat).';';
}

Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Property/Charset.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ public function getCharset() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
return "@charset {$this->sCharset->render($oOutputFormat)};";
}

Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Property/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ public function getLocation() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
return "@import ".$this->oLocation->render($oOutputFormat).($this->sMediaQuery === null ? '' : ' '.$this->sMediaQuery).';';
}

Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Property/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,10 @@ public function setSelector($sSelector) {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
return $this->getSelector();
}

Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,10 @@ public function getIsImportant() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sResult = "{$this->sRule}: ";
if ($this->mValue instanceof Value) { //Can also be a ValueList
$sResult .= $this->mValue->render($oOutputFormat);
Expand Down
9 changes: 3 additions & 6 deletions lib/Sabberworm/CSS/RuleSet/AtRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Sabberworm\CSS\Property\AtRule;

/**
* A RuleSet constructed by an unknown @-rule. @font-face rules are rendered into AtRule objects.
* A RuleSet constructed by an unknown @-rule. @font-face rules are rendered into AtRuleSet objects.
*/
class AtRuleSet extends RuleSet implements AtRule {

Expand All @@ -27,13 +27,10 @@ public function atRuleArgs() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sResult = "@{$this->sType} {$this->sArgs}{";
$sResult .= parent::render($oOutputFormat);
$sResult .= '}';
Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,10 @@ public function createFontShorthand() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
if(count($this->aSelectors) === 0) {
// If all the selectors have been removed, this declaration block becomes invalid
throw new \Sabberworm\CSS\Parsing\OutputException("Attempt to print declaration block with missing selector");
Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,10 @@ public function removeRule($mRule) {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sResult = '';
foreach ($this->aRules as $aRules) {
foreach($aRules as $oRule) {
Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Value/CSSFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ public function getArguments() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$aArguments = parent::render($oOutputFormat);
return "{$this->sName}({$aArguments})";
}
Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ public function getColorDescription() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
// Shorthand RGB color values
if($oOutputFormat->get('RGBHashNotation') && implode('', array_keys($this->aComponents)) === 'rgb') {
$sResult = sprintf(
Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ public function isRelative() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$l = localeconv();
$sPoint = preg_quote($l['decimal_point'], '/');
return preg_replace(array("/$sPoint/", "/^(-?)0\./"), array('.', '$1.'), $this->fSize) . ($this->sUnit === null ? '' : $this->sUnit);
Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Value/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ public function getString() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
$sString = addslashes($this->sString);
$sString = str_replace("\n", '\A', $sString);
return '"' . $sString . '"';
Expand Down
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Value/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ public function getURL() {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
return "url({$this->oURL->render($oOutputFormat)})";
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Sabberworm/CSS/Value/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

abstract class Value {
public abstract function __toString();
public abstract function render($oOutputFormat = null);
public abstract function render(\Sabberworm\CSS\OutputFormat $oOutputFormat);
}
7 changes: 2 additions & 5 deletions lib/Sabberworm/CSS/Value/ValueList.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ public function setListSeparator($sSeparator) {
}

public function __toString() {
return $this->render();
return $this->render(new \Sabberworm\CSS\OutputFormat());
}

public function render($oOutputFormat = null) {
if($oOutputFormat === null) {
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
return implode($this->sSeparator, $this->aComponents);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Sabberworm/CSS/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function testUnicodeParsing() {
}
$aContentRules = $oRuleSet->getRules('content');
$aContents = $aContentRules[0]->getValues();
$sString = $aContents[0][0]->render();
$sString = $aContents[0][0]->__toString();
if ($sSelector == '.test-1') {
$this->assertSame('" "', $sString);
}
Expand Down

0 comments on commit c87ffee

Please sign in to comment.