Skip to content

Commit c87ffee

Browse files
committed
Make Format mandatory when calling render()
use __toString() for the default format except in \Sabberworm\CSS\CSSList\Document where null is still allowed This removes all those null checks.
1 parent 0a67867 commit c87ffee

21 files changed

+48
-93
lines changed

lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Sabberworm\CSS\Property\AtRule;
66

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

@@ -27,13 +27,10 @@ public function atRuleArgs() {
2727
}
2828

2929
public function __toString() {
30-
return $this->render();
30+
return $this->render(new \Sabberworm\CSS\OutputFormat());
3131
}
3232

33-
public function render($oOutputFormat = null) {
34-
if($oOutputFormat === null) {
35-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
36-
}
33+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
3734
$sResult = "@{$this->sType} {$this->sArgs}{";
3835
$sResult .= parent::render($oOutputFormat);
3936
$sResult .= '}';

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,10 @@ public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false
6969
}
7070

7171
public function __toString() {
72-
return $this->render();
72+
return $this->render(new \Sabberworm\CSS\OutputFormat());
7373
}
7474

75-
public function render($oOutputFormat = null) {
76-
if($oOutputFormat === null) {
77-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
78-
}
75+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
7976
$sResult = '';
8077
foreach ($this->aContents as $oContent) {
8178
$sResult .= $oContent->render($oOutputFormat->nextLevel());

lib/Sabberworm/CSS/CSSList/Document.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,21 @@ public function expandShorthands() {
7474
}
7575
}
7676

77-
/*
77+
/**
7878
* Create shorthands properties whenever possible
7979
*/
80-
8180
public function createShorthands() {
8281
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) {
8382
$oDeclaration->createShorthands();
8483
}
8584
}
8685

86+
// Override render() to make format argument optional
87+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat = null) {
88+
if($oOutputFormat === null) {
89+
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
90+
}
91+
return parent::render($oOutputFormat);
92+
}
93+
8794
}

lib/Sabberworm/CSS/CSSList/KeyFrame.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,10 @@ public function getAnimationName() {
3232
}
3333

3434
public function __toString() {
35-
return $this->render();
35+
return $this->render(new \Sabberworm\CSS\OutputFormat());
3636
}
3737

38-
public function render($oOutputFormat = null) {
39-
40-
if($oOutputFormat === null) {
41-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
42-
}
38+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
4339
$sResult = "@{$this->vendorKeyFrame} {$this->animationName} {";
4440
$sResult .= parent::render($oOutputFormat->nextLevel());
4541
$sResult .= '}';

lib/Sabberworm/CSS/Property/AtRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ interface AtRule {
1010
public function atRuleName();
1111
public function atRuleArgs();
1212
public function __toString();
13-
public function render($oOutputFormat = null);
13+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat);
1414
}

lib/Sabberworm/CSS/Property/CSSNamespace.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@ public function __construct($mUrl, $sPrefix = null) {
1515
}
1616

1717
public function __toString() {
18-
return $this->render();
18+
return $this->render(new \Sabberworm\CSS\OutputFormat());
1919
}
2020

21-
public function render($oOutputFormat = null) {
22-
if($oOutputFormat === null) {
23-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
24-
}
21+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
2522
return '@namespace '.($this->sPrefix === null ? '' : $this->sPrefix.' ').$this->mUrl->render($oOutputFormat).';';
2623
}
2724

lib/Sabberworm/CSS/Property/Charset.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ public function getCharset() {
2626
}
2727

2828
public function __toString() {
29-
return $this->render();
29+
return $this->render(new \Sabberworm\CSS\OutputFormat());
3030
}
3131

32-
public function render($oOutputFormat = null) {
33-
if($oOutputFormat === null) {
34-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
35-
}
32+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
3633
return "@charset {$this->sCharset->render($oOutputFormat)};";
3734
}
3835

lib/Sabberworm/CSS/Property/Import.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@ public function getLocation() {
2525
}
2626

2727
public function __toString() {
28-
return $this->render();
28+
return $this->render(new \Sabberworm\CSS\OutputFormat());
2929
}
3030

31-
public function render($oOutputFormat = null) {
32-
if($oOutputFormat === null) {
33-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
34-
}
31+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
3532
return "@import ".$this->oLocation->render($oOutputFormat).($this->sMediaQuery === null ? '' : ' '.$this->sMediaQuery).';';
3633
}
3734

lib/Sabberworm/CSS/Property/Selector.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,10 @@ public function setSelector($sSelector) {
5555
}
5656

5757
public function __toString() {
58-
return $this->render();
58+
return $this->render(new \Sabberworm\CSS\OutputFormat());
5959
}
6060

61-
public function render($oOutputFormat = null) {
62-
if($oOutputFormat === null) {
63-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
64-
}
61+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
6562
return $this->getSelector();
6663
}
6764

lib/Sabberworm/CSS/Rule/Rule.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,10 @@ public function getIsImportant() {
126126
}
127127

128128
public function __toString() {
129-
return $this->render();
129+
return $this->render(new \Sabberworm\CSS\OutputFormat());
130130
}
131131

132-
public function render($oOutputFormat = null) {
133-
if($oOutputFormat === null) {
134-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
135-
}
132+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
136133
$sResult = "{$this->sRule}: ";
137134
if ($this->mValue instanceof Value) { //Can also be a ValueList
138135
$sResult .= $this->mValue->render($oOutputFormat);

lib/Sabberworm/CSS/RuleSet/AtRuleSet.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Sabberworm\CSS\Property\AtRule;
66

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

@@ -27,13 +27,10 @@ public function atRuleArgs() {
2727
}
2828

2929
public function __toString() {
30-
return $this->render();
30+
return $this->render(new \Sabberworm\CSS\OutputFormat());
3131
}
3232

33-
public function render($oOutputFormat = null) {
34-
if($oOutputFormat === null) {
35-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
36-
}
33+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
3734
$sResult = "@{$this->sType} {$this->sArgs}{";
3835
$sResult .= parent::render($oOutputFormat);
3936
$sResult .= '}';

lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,10 @@ public function createFontShorthand() {
590590
}
591591

592592
public function __toString() {
593-
return $this->render();
593+
return $this->render(new \Sabberworm\CSS\OutputFormat());
594594
}
595595

596-
public function render($oOutputFormat = null) {
597-
if($oOutputFormat === null) {
598-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
599-
}
596+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
600597
if(count($this->aSelectors) === 0) {
601598
// If all the selectors have been removed, this declaration block becomes invalid
602599
throw new \Sabberworm\CSS\Parsing\OutputException("Attempt to print declaration block with missing selector");

lib/Sabberworm/CSS/RuleSet/RuleSet.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,10 @@ public function removeRule($mRule) {
8383
}
8484

8585
public function __toString() {
86-
return $this->render();
86+
return $this->render(new \Sabberworm\CSS\OutputFormat());
8787
}
8888

89-
public function render($oOutputFormat = null) {
90-
if($oOutputFormat === null) {
91-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
92-
}
89+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
9390
$sResult = '';
9491
foreach ($this->aRules as $aRules) {
9592
foreach($aRules as $oRule) {

lib/Sabberworm/CSS/Value/CSSFunction.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ public function getArguments() {
2828
}
2929

3030
public function __toString() {
31-
return $this->render();
31+
return $this->render(new \Sabberworm\CSS\OutputFormat());
3232
}
3333

34-
public function render($oOutputFormat = null) {
35-
if($oOutputFormat === null) {
36-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
37-
}
34+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
3835
$aArguments = parent::render($oOutputFormat);
3936
return "{$this->sName}({$aArguments})";
4037
}

lib/Sabberworm/CSS/Value/Color.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ public function getColorDescription() {
2222
}
2323

2424
public function __toString() {
25-
return $this->render();
25+
return $this->render(new \Sabberworm\CSS\OutputFormat());
2626
}
2727

28-
public function render($oOutputFormat = null) {
29-
if($oOutputFormat === null) {
30-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
31-
}
28+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
3229
// Shorthand RGB color values
3330
if($oOutputFormat->get('RGBHashNotation') && implode('', array_keys($this->aComponents)) === 'rgb') {
3431
$sResult = sprintf(

lib/Sabberworm/CSS/Value/Size.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,10 @@ public function isRelative() {
6060
}
6161

6262
public function __toString() {
63-
return $this->render();
63+
return $this->render(new \Sabberworm\CSS\OutputFormat());
6464
}
6565

66-
public function render($oOutputFormat = null) {
67-
if($oOutputFormat === null) {
68-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
69-
}
66+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
7067
$l = localeconv();
7168
$sPoint = preg_quote($l['decimal_point'], '/');
7269
return preg_replace(array("/$sPoint/", "/^(-?)0\./"), array('.', '$1.'), $this->fSize) . ($this->sUnit === null ? '' : $this->sUnit);

lib/Sabberworm/CSS/Value/String.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@ public function getString() {
1919
}
2020

2121
public function __toString() {
22-
return $this->render();
22+
return $this->render(new \Sabberworm\CSS\OutputFormat());
2323
}
2424

25-
public function render($oOutputFormat = null) {
26-
if($oOutputFormat === null) {
27-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
28-
}
25+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
2926
$sString = addslashes($this->sString);
3027
$sString = str_replace("\n", '\A', $sString);
3128
return '"' . $sString . '"';

lib/Sabberworm/CSS/Value/URL.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ public function getURL() {
2020
}
2121

2222
public function __toString() {
23-
return $this->render();
23+
return $this->render(new \Sabberworm\CSS\OutputFormat());
2424
}
2525

26-
public function render($oOutputFormat = null) {
27-
if($oOutputFormat === null) {
28-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
29-
}
26+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
3027
return "url({$this->oURL->render($oOutputFormat)})";
3128
}
3229

lib/Sabberworm/CSS/Value/Value.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
abstract class Value {
66
public abstract function __toString();
7-
public abstract function render($oOutputFormat = null);
7+
public abstract function render(\Sabberworm\CSS\OutputFormat $oOutputFormat);
88
}

lib/Sabberworm/CSS/Value/ValueList.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,10 @@ public function setListSeparator($sSeparator) {
3636
}
3737

3838
public function __toString() {
39-
return $this->render();
39+
return $this->render(new \Sabberworm\CSS\OutputFormat());
4040
}
4141

42-
public function render($oOutputFormat = null) {
43-
if($oOutputFormat === null) {
44-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
45-
}
42+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
4643
return implode($this->sSeparator, $this->aComponents);
4744
}
4845

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function testUnicodeParsing() {
8888
}
8989
$aContentRules = $oRuleSet->getRules('content');
9090
$aContents = $aContentRules[0]->getValues();
91-
$sString = $aContents[0][0]->render();
91+
$sString = $aContents[0][0]->__toString();
9292
if ($sSelector == '.test-1') {
9393
$this->assertSame('" "', $sString);
9494
}

0 commit comments

Comments
 (0)