Skip to content

Commit dfcbf8c

Browse files
committed
Initial output formatting options
catching output exceptions not yet implemented
1 parent c87ffee commit dfcbf8c

File tree

19 files changed

+448
-57
lines changed

19 files changed

+448
-57
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ There are a few convenience methods on Document to ease finding, manipulating an
107107
## To-Do
108108

109109
* More convenience methods [like `selectorsWithElement($sId/Class/TagName)`, `attributesOfType($sType)`, `removeAttributesOfType($sType)`]
110-
* Options for output (compact, verbose, etc.)
111110
* Real multibyte support. Currently only multibyte charsets whose first 255 code points take up only one byte and are identical with ASCII are supported (yes, UTF-8 fits this description).
112111
* Named color support (using `Color` instead of an anonymous string literal)
113112

@@ -152,6 +151,18 @@ To output the entire CSS document into a variable, just use `->render()`:
152151
$oCssDocument = $oCssParser->parse();
153152
print $oCssDocument->render();
154153

154+
If you want to format the output, pass an instance of type `Sabberworm\CSS\OutputFormat`:
155+
156+
$oFormat = Sabberworm\CSS\OutputFormat::create()->indentWithSpaces(4)->setSpaceBetweenRules("\n");
157+
print $oCssDocument->render(oFormat);
158+
159+
Or use one of the predefined formats:
160+
161+
print $oCssDocument->render(Sabberworm\CSS\OutputFormat::createPretty());
162+
print $oCssDocument->render(Sabberworm\CSS\OutputFormat::createCompact());
163+
164+
To see what you can do with output formatting, look at the tests in `tests/Sabberworm/CSS/OutputFormatTest.php`.
165+
155166
## Examples
156167

157168
### Example 1 (At-Rules)

lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ public function __toString() {
3131
}
3232

3333
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
34-
$sResult = "@{$this->sType} {$this->sArgs}{";
34+
$sResult = "@{$this->sType} {$this->sArgs}{$oOutputFormat->spaceBeforeOpeningBrace()}{";
3535
$sResult .= parent::render($oOutputFormat);
3636
$sResult .= '}';
3737
return $sResult;
3838
}
3939

40+
public function isRootList() {
41+
return false;
42+
}
43+
4044
}

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,33 @@ public function __toString() {
7474

7575
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
7676
$sResult = '';
77+
$bIsFirst = true;
78+
$oNextLevel = $oOutputFormat;
79+
if(!$this->isRootList()) {
80+
$oNextLevel = $oOutputFormat->nextLevel();
81+
}
7782
foreach ($this->aContents as $oContent) {
78-
$sResult .= $oContent->render($oOutputFormat->nextLevel());
83+
if($bIsFirst) {
84+
$bIsFirst = false;
85+
$sResult .= $oNextLevel->spaceBeforeBlocks();
86+
} else {
87+
$sResult .= $oNextLevel->spaceBetweenBlocks();
88+
}
89+
$sResult .= $oContent->render($oNextLevel);
90+
}
91+
92+
if(!$bIsFirst) {
93+
// Had some output
94+
$sResult .= $oOutputFormat->spaceAfterBlocks();
7995
}
96+
8097
return $sResult;
8198
}
99+
100+
/**
101+
* Return true if the list can not be further outdented. Only important when rendering.
102+
*/
103+
public abstract function isRootList();
82104

83105
public function getContents() {
84106
return $this->aContents;

lib/Sabberworm/CSS/CSSList/Document.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,8 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat = null) {
9191
return parent::render($oOutputFormat);
9292
}
9393

94+
public function isRootList() {
95+
return true;
96+
}
97+
9498
}

lib/Sabberworm/CSS/CSSList/KeyFrame.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ public function __toString() {
3636
}
3737

3838
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
39-
$sResult = "@{$this->vendorKeyFrame} {$this->animationName} {";
39+
$sResult = "@{$this->vendorKeyFrame} {$this->animationName}{$oOutputFormat->spaceBeforeOpeningBrace()}{";
4040
$sResult .= parent::render($oOutputFormat->nextLevel());
4141
$sResult .= '}';
4242
return $sResult;
4343
}
4444

45+
public function isRootList() {
46+
return false;
47+
}
48+
4549
public function atRuleName() {
4650
return $this->vendorKeyFrame;
4751
}

0 commit comments

Comments
 (0)