Skip to content

Commit 0f4a139

Browse files
committed
Comments and code formatting
1 parent 441d519 commit 0f4a139

File tree

7 files changed

+300
-178
lines changed

7 files changed

+300
-178
lines changed

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sabberworm\CSS\CSSList;
44

5+
use Sabberworm\CSS\OutputFormat;
56
use Sabberworm\CSS\Renderable;
67
use Sabberworm\CSS\RuleSet\DeclarationBlock;
78
use Sabberworm\CSS\RuleSet\RuleSet;
@@ -90,40 +91,45 @@ public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false
9091
}
9192

9293
public function __toString() {
93-
return $this->render(new \Sabberworm\CSS\OutputFormat());
94+
return $this->render(new OutputFormat());
9495
}
9596

96-
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
97-
$sResult = '';
98-
$bIsFirst = true;
99-
$oNextLevel = $oOutputFormat;
100-
if(!$this->isRootList()) {
101-
$oNextLevel = $oOutputFormat->nextLevel();
102-
}
103-
foreach ($this->aContents as $oContent) {
104-
$sRendered = $oOutputFormat->safely(function() use ($oNextLevel, $oContent) {
105-
return $oContent->render($oNextLevel);
106-
});
107-
if($sRendered === null) {
108-
continue;
109-
}
110-
if($bIsFirst) {
111-
$bIsFirst = false;
112-
$sResult .= $oNextLevel->spaceBeforeBlocks();
113-
} else {
114-
$sResult .= $oNextLevel->spaceBetweenBlocks();
115-
}
116-
$sResult .= $sRendered;
117-
}
118-
119-
if(!$bIsFirst) {
120-
// Had some output
121-
$sResult .= $oOutputFormat->spaceAfterBlocks();
122-
}
123-
124-
return $sResult;
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function render(OutputFormat $oOutputFormat) {
101+
$sResult = '';
102+
$bIsFirst = true;
103+
$oNextLevel = $oOutputFormat;
104+
if (!$this->isRootList()) {
105+
$oNextLevel = $oOutputFormat->nextLevel();
106+
}
107+
foreach ($this->aContents as $oContent) {
108+
$sRendered = $oOutputFormat->safely(
109+
function () use ($oNextLevel, $oContent) {
110+
return $oContent->render($oNextLevel);
111+
}
112+
);
113+
if ($sRendered === null) {
114+
continue;
115+
}
116+
if ($bIsFirst) {
117+
$bIsFirst = false;
118+
$sResult .= $oNextLevel->spaceBeforeBlocks();
119+
} else {
120+
$sResult .= $oNextLevel->spaceBetweenBlocks();
121+
}
122+
$sResult .= $sRendered;
123+
}
124+
125+
if (!$bIsFirst) {
126+
// Had some output
127+
$sResult .= $oOutputFormat->spaceAfterBlocks();
128+
}
129+
130+
return $sResult;
125131
}
126-
132+
127133
/**
128134
* Return true if the list can not be further outdented. Only important when rendering.
129135
*/

lib/Sabberworm/CSS/CSSList/Document.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sabberworm\CSS\CSSList;
44

5+
use Sabberworm\CSS\OutputFormat;
6+
57
/**
68
* The root CSSList of a parsed file. Contains all top-level css contents, mostly declaration blocks, but also any @-rules encountered.
79
*/
@@ -90,10 +92,16 @@ public function createShorthands() {
9092
}
9193
}
9294

93-
// Override render() to make format argument optional
94-
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat = null) {
95+
/**
96+
* {@inheritdoc}
97+
*
98+
* @param OutputFormat|null $oOutputFormat
99+
*
100+
* @return string
101+
*/
102+
public function render(OutputFormat $oOutputFormat = null) {
95103
if($oOutputFormat === null) {
96-
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
104+
$oOutputFormat = new OutputFormat();
97105
}
98106
return parent::render($oOutputFormat);
99107
}
@@ -102,4 +110,4 @@ public function isRootList() {
102110
return true;
103111
}
104112

105-
}
113+
}

lib/Sabberworm/CSS/Comment/Commentable.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22

33
namespace Sabberworm\CSS\Comment;
44

5-
interface Commentable {
6-
7-
/**
8-
* @param array $aComments Array of comments.
9-
*/
10-
public function addComments(array $aComments);
11-
12-
/**
13-
* @return array
14-
*/
15-
public function getComments();
16-
17-
/**
18-
* @param array $aComments Array containing Comment objects.
19-
*/
20-
public function setComments(array $aComments);
5+
interface Commentable
6+
{
7+
/**
8+
* @param Comment[] $aComments Array of comments.
9+
*/
10+
public function addComments(array $aComments);
2111

12+
/**
13+
* @return Comment[]
14+
*/
15+
public function getComments();
2216

17+
/**
18+
* @param Comment[] $aComments Array containing Comment objects.
19+
*/
20+
public function setComments(array $aComments);
2321
}

lib/Sabberworm/CSS/Renderable.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,26 @@
22

33
namespace Sabberworm\CSS;
44

5+
use Sabberworm\CSS\OutputFormat;
6+
57
interface Renderable {
8+
9+
/**
10+
* @return string
11+
*/
612
public function __toString();
7-
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat);
13+
14+
/**
15+
* Renders this component
16+
*
17+
* @param OutputFormat $oOutputFormat Formatting options
18+
*
19+
* @return string Rendered CSS
20+
*/
21+
public function render(OutputFormat $oOutputFormat);
22+
23+
/**
24+
* @return int Line number
25+
*/
826
public function getLineNo();
9-
}
27+
}

lib/Sabberworm/CSS/Rule/Rule.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sabberworm\CSS\Rule;
44

5+
use Sabberworm\CSS\Comment\Comment;
6+
use Sabberworm\CSS\OutputFormat;
57
use Sabberworm\CSS\Renderable;
68
use Sabberworm\CSS\Value\RuleValueList;
79
use Sabberworm\CSS\Value\Value;
@@ -152,11 +154,17 @@ public function getIsImportant() {
152154
return $this->bIsImportant;
153155
}
154156

157+
/**
158+
* {@inheritdoc}
159+
*/
155160
public function __toString() {
156-
return $this->render(new \Sabberworm\CSS\OutputFormat());
161+
return $this->render(new OutputFormat());
157162
}
158163

159-
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
164+
/**
165+
* {@inheritdoc}
166+
*/
167+
public function render(OutputFormat $oOutputFormat) {
160168
$sResult = "{$this->sRule}:{$oOutputFormat->spaceAfterRuleName()}";
161169
if ($this->mValue instanceof Value) { //Can also be a ValueList
162170
$sResult .= $this->mValue->render($oOutputFormat);
@@ -174,21 +182,21 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
174182
}
175183

176184
/**
177-
* @param array $aComments Array of comments.
185+
* {@inheritdoc}
178186
*/
179187
public function addComments(array $aComments) {
180188
$this->aComments = array_merge($this->aComments, $aComments);
181189
}
182190

183191
/**
184-
* @return array
192+
* {@inheritdoc}
185193
*/
186194
public function getComments() {
187195
return $this->aComments;
188196
}
189197

190198
/**
191-
* @param array $aComments Array containing Comment objects.
199+
* {@inheritdoc}
192200
*/
193201
public function setComments(array $aComments) {
194202
$this->aComments = $aComments;

lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sabberworm\CSS\RuleSet;
44

5+
use Sabberworm\CSS\Comment\Comment;
6+
use Sabberworm\CSS\OutputFormat;
57
use Sabberworm\CSS\Property\Selector;
68
use Sabberworm\CSS\Rule\Rule;
79
use Sabberworm\CSS\Value\RuleValueList;
@@ -87,7 +89,7 @@ public function expandShorthands() {
8789
public function createShorthands() {
8890
$this->createBackgroundShorthand();
8991
$this->createDimensionsShorthand();
90-
// border must be shortened after dimensions
92+
// border must be shortened after dimensions
9193
$this->createBorderShorthand();
9294
$this->createFontShorthand();
9395
$this->createListStyleShorthand();
@@ -446,7 +448,7 @@ public function createBorderShorthand() {
446448

447449
/*
448450
* Looks for long format CSS dimensional properties
449-
* (margin, padding, border-color, border-style and border-width)
451+
* (margin, padding, border-color, border-style and border-width)
450452
* and converts them into shorthand CSS properties.
451453
* */
452454

@@ -501,7 +503,7 @@ public function createDimensionsShorthand() {
501503
$oNewRule->addValue($aValues['bottom']);
502504
}
503505
} else {
504-
// No sides are equal
506+
// No sides are equal
505507
$oNewRule->addValue($aValues['top']);
506508
$oNewRule->addValue($aValues['left']);
507509
$oNewRule->addValue($aValues['bottom']);
@@ -516,8 +518,8 @@ public function createDimensionsShorthand() {
516518
}
517519

518520
/**
519-
* Looks for long format CSS font properties (e.g. <tt>font-weight</tt>) and
520-
* tries to convert them into a shorthand CSS <tt>font</tt> property.
521+
* Looks for long format CSS font properties (e.g. <tt>font-weight</tt>) and
522+
* tries to convert them into a shorthand CSS <tt>font</tt> property.
521523
* At least font-size AND font-family must be present in order to create a shorthand declaration.
522524
* */
523525
public function createFontShorthand() {
@@ -591,10 +593,13 @@ public function createFontShorthand() {
591593
}
592594

593595
public function __toString() {
594-
return $this->render(new \Sabberworm\CSS\OutputFormat());
596+
return $this->render(new OutputFormat());
595597
}
596598

597-
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
599+
/**
600+
* {@inheritdoc}
601+
*/
602+
public function render(OutputFormat $oOutputFormat) {
598603
if(count($this->aSelectors) === 0) {
599604
// If all the selectors have been removed, this declaration block becomes invalid
600605
throw new OutputException("Attempt to print declaration block with missing selector", $this->iLineNo);

0 commit comments

Comments
 (0)