Skip to content

Commit 0a67867

Browse files
committed
Implement changes necessary to format output
* Universally use parse() instead of __toString * Introduce OutputFormat class whose instance can be passed to parse() * Prepare release for 6.0.0
1 parent 2ed8f45 commit 0a67867

26 files changed

+421
-174
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Revision History
22

3+
## 6.0
4+
5+
### 6.0.0
6+
7+
* Format output using Sabberworm\CSS\OutputFormat
8+
* *No backwards-incompatible changes*
9+
10+
#### Deprecations
11+
12+
* The parse() method replaces __toString with an optional argument (instance of the OutputFormat class)
13+
314
## 5.0
415

516
### 5.0.0 (2013-03-20)

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $oRule)`, `
8484

8585
#### Value
8686

87-
`Value` is an abstract class that only defines the `__toString` method. The concrete subclasses for atomic value types are:
87+
`Value` is an abstract class that only defines the `render` method. The concrete subclasses for atomic value types are:
8888

8989
* `Size` – consists of a numeric `size` value and a unit.
9090
* `Color` – colors can be input in the form #rrggbb, #rgb or schema(val1, val2, …) but are always stored as an array of ('s' => val1, 'c' => val2, 'h' => val3, …) and output in the second form.
@@ -146,11 +146,11 @@ There are a few convenience methods on Document to ease finding, manipulating an
146146

147147
### Output
148148

149-
To output the entire CSS document into a variable, just use `->__toString()`:
149+
To output the entire CSS document into a variable, just use `->render()`:
150150

151151
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
152152
$oCssDocument = $oCssParser->parse();
153-
print $oCssDocument->__toString();
153+
print $oCssDocument->render();
154154

155155
## Examples
156156

@@ -353,7 +353,7 @@ To output the entire CSS document into a variable, just use `->__toString()`:
353353
}
354354
}
355355

356-
#### Output (`__toString()`)
356+
#### Output (`render()`)
357357

358358
@charset "utf-8";@font-face {font-family: "CrassRoots";src: url("../media/cr.ttf");}html, body {font-size: 1.6em;}
359359
@keyframes mymove {from {top: 0px;}
@@ -489,7 +489,7 @@ To output the entire CSS document into a variable, just use `->__toString()`:
489489
}
490490
}
491491

492-
#### Output (`__toString()`)
492+
#### Output (`render()`)
493493

494494
#header {margin: 10px 2em 1cm 2%;font-family: Verdana,Helvetica,"Gill Sans",sans-serif;color: red !important;}
495495

lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ public function atRuleArgs() {
2727
}
2828

2929
public function __toString() {
30+
return $this->render();
31+
}
32+
33+
public function render($oOutputFormat = null) {
34+
if($oOutputFormat === null) {
35+
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
36+
}
3037
$sResult = "@{$this->sType} {$this->sArgs}{";
31-
$sResult .= parent::__toString();
38+
$sResult .= parent::render($oOutputFormat);
3239
$sResult .= '}';
3340
return $sResult;
3441
}

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,16 @@ public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false
6969
}
7070

7171
public function __toString() {
72+
return $this->render();
73+
}
74+
75+
public function render($oOutputFormat = null) {
76+
if($oOutputFormat === null) {
77+
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
78+
}
7279
$sResult = '';
7380
foreach ($this->aContents as $oContent) {
74-
$sResult .= $oContent->__toString();
81+
$sResult .= $oContent->render($oOutputFormat->nextLevel());
7582
}
7683
return $sResult;
7784
}

lib/Sabberworm/CSS/CSSList/Document.php

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,81 +7,81 @@
77
*/
88
class Document extends CSSBlockList {
99

10-
/**
11-
* Gets all DeclarationBlock objects recursively.
12-
*/
13-
public function getAllDeclarationBlocks() {
14-
$aResult = array();
15-
$this->allDeclarationBlocks($aResult);
16-
return $aResult;
17-
}
10+
/**
11+
* Gets all DeclarationBlock objects recursively.
12+
*/
13+
public function getAllDeclarationBlocks() {
14+
$aResult = array();
15+
$this->allDeclarationBlocks($aResult);
16+
return $aResult;
17+
}
1818

19-
/**
20-
* @deprecated use getAllDeclarationBlocks()
21-
*/
22-
public function getAllSelectors() {
23-
return $this->getAllDeclarationBlocks();
24-
}
19+
/**
20+
* @deprecated use getAllDeclarationBlocks()
21+
*/
22+
public function getAllSelectors() {
23+
return $this->getAllDeclarationBlocks();
24+
}
2525

26-
/**
27-
* Returns all RuleSet objects found recursively in the tree.
28-
*/
29-
public function getAllRuleSets() {
30-
$aResult = array();
31-
$this->allRuleSets($aResult);
32-
return $aResult;
33-
}
26+
/**
27+
* Returns all RuleSet objects found recursively in the tree.
28+
*/
29+
public function getAllRuleSets() {
30+
$aResult = array();
31+
$this->allRuleSets($aResult);
32+
return $aResult;
33+
}
3434

35-
/**
36-
* Returns all Value objects found recursively in the tree.
37-
* @param (object|string) $mElement the CSSList or RuleSet to start the search from (defaults to the whole document). If a string is given, it is used as rule name filter (@see{RuleSet->getRules()}).
38-
* @param (bool) $bSearchInFunctionArguments whether to also return Value objects used as Function arguments.
39-
*/
40-
public function getAllValues($mElement = null, $bSearchInFunctionArguments = false) {
41-
$sSearchString = null;
42-
if ($mElement === null) {
43-
$mElement = $this;
44-
} else if (is_string($mElement)) {
45-
$sSearchString = $mElement;
46-
$mElement = $this;
47-
}
48-
$aResult = array();
49-
$this->allValues($mElement, $aResult, $sSearchString, $bSearchInFunctionArguments);
50-
return $aResult;
51-
}
35+
/**
36+
* Returns all Value objects found recursively in the tree.
37+
* @param (object|string) $mElement the CSSList or RuleSet to start the search from (defaults to the whole document). If a string is given, it is used as rule name filter (@see{RuleSet->getRules()}).
38+
* @param (bool) $bSearchInFunctionArguments whether to also return Value objects used as Function arguments.
39+
*/
40+
public function getAllValues($mElement = null, $bSearchInFunctionArguments = false) {
41+
$sSearchString = null;
42+
if ($mElement === null) {
43+
$mElement = $this;
44+
} else if (is_string($mElement)) {
45+
$sSearchString = $mElement;
46+
$mElement = $this;
47+
}
48+
$aResult = array();
49+
$this->allValues($mElement, $aResult, $sSearchString, $bSearchInFunctionArguments);
50+
return $aResult;
51+
}
5252

53-
/**
54-
* Returns all Selector objects found recursively in the tree.
55-
* Note that this does not yield the full DeclarationBlock that the selector belongs to (and, currently, there is no way to get to that).
56-
* @param $sSpecificitySearch An optional filter by specificity. May contain a comparison operator and a number or just a number (defaults to "==").
57-
* @example getSelectorsBySpecificity('>= 100')
58-
*/
59-
public function getSelectorsBySpecificity($sSpecificitySearch = null) {
60-
if (is_numeric($sSpecificitySearch) || is_numeric($sSpecificitySearch[0])) {
61-
$sSpecificitySearch = "== $sSpecificitySearch";
62-
}
63-
$aResult = array();
64-
$this->allSelectors($aResult, $sSpecificitySearch);
65-
return $aResult;
66-
}
53+
/**
54+
* Returns all Selector objects found recursively in the tree.
55+
* Note that this does not yield the full DeclarationBlock that the selector belongs to (and, currently, there is no way to get to that).
56+
* @param $sSpecificitySearch An optional filter by specificity. May contain a comparison operator and a number or just a number (defaults to "==").
57+
* @example getSelectorsBySpecificity('>= 100')
58+
*/
59+
public function getSelectorsBySpecificity($sSpecificitySearch = null) {
60+
if (is_numeric($sSpecificitySearch) || is_numeric($sSpecificitySearch[0])) {
61+
$sSpecificitySearch = "== $sSpecificitySearch";
62+
}
63+
$aResult = array();
64+
$this->allSelectors($aResult, $sSpecificitySearch);
65+
return $aResult;
66+
}
6767

68-
/**
69-
* Expands all shorthand properties to their long value
70-
*/
71-
public function expandShorthands() {
72-
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) {
73-
$oDeclaration->expandShorthands();
74-
}
75-
}
68+
/**
69+
* Expands all shorthand properties to their long value
70+
*/
71+
public function expandShorthands() {
72+
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) {
73+
$oDeclaration->expandShorthands();
74+
}
75+
}
7676

77-
/*
78-
* Create shorthands properties whenever possible
79-
*/
77+
/*
78+
* Create shorthands properties whenever possible
79+
*/
8080

81-
public function createShorthands() {
82-
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) {
83-
$oDeclaration->createShorthands();
84-
}
85-
}
81+
public function createShorthands() {
82+
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) {
83+
$oDeclaration->createShorthands();
84+
}
85+
}
8686

8787
}
Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,56 @@
1-
<?php
2-
3-
namespace Sabberworm\CSS\CSSList;
4-
5-
use Sabberworm\CSS\Property\AtRule;
6-
7-
class KeyFrame extends CSSList implements AtRule {
8-
9-
private $vendorKeyFrame;
10-
private $animationName;
11-
12-
public function __construct() {
13-
parent::__construct();
14-
$this->vendorKeyFrame = null;
15-
$this->animationName = null;
16-
}
17-
18-
public function setVendorKeyFrame($vendorKeyFrame) {
19-
$this->vendorKeyFrame = $vendorKeyFrame;
20-
}
21-
22-
public function getVendorKeyFrame() {
23-
return $this->vendorKeyFrame;
24-
}
25-
26-
public function setAnimationName($animationName) {
27-
$this->animationName = $animationName;
28-
}
29-
30-
public function getAnimationName() {
31-
return $this->animationName;
32-
}
33-
34-
public function __toString() {
35-
$sResult = "@{$this->vendorKeyFrame} {$this->animationName} {";
36-
$sResult .= parent::__toString();
37-
$sResult .= '}';
38-
return $sResult;
39-
}
40-
41-
public function atRuleName() {
42-
return $this->vendorKeyFrame;
43-
}
44-
45-
public function atRuleArgs() {
46-
return $this->animationName;
47-
}
48-
}
1+
<?php
2+
3+
namespace Sabberworm\CSS\CSSList;
4+
5+
use Sabberworm\CSS\Property\AtRule;
6+
7+
class KeyFrame extends CSSList implements AtRule {
8+
9+
private $vendorKeyFrame;
10+
private $animationName;
11+
12+
public function __construct() {
13+
parent::__construct();
14+
$this->vendorKeyFrame = null;
15+
$this->animationName = null;
16+
}
17+
18+
public function setVendorKeyFrame($vendorKeyFrame) {
19+
$this->vendorKeyFrame = $vendorKeyFrame;
20+
}
21+
22+
public function getVendorKeyFrame() {
23+
return $this->vendorKeyFrame;
24+
}
25+
26+
public function setAnimationName($animationName) {
27+
$this->animationName = $animationName;
28+
}
29+
30+
public function getAnimationName() {
31+
return $this->animationName;
32+
}
33+
34+
public function __toString() {
35+
return $this->render();
36+
}
37+
38+
public function render($oOutputFormat = null) {
39+
40+
if($oOutputFormat === null) {
41+
$oOutputFormat = new \Sabberworm\CSS\OutputFormat();
42+
}
43+
$sResult = "@{$this->vendorKeyFrame} {$this->animationName} {";
44+
$sResult .= parent::render($oOutputFormat->nextLevel());
45+
$sResult .= '}';
46+
return $sResult;
47+
}
48+
49+
public function atRuleName() {
50+
return $this->vendorKeyFrame;
51+
}
52+
53+
public function atRuleArgs() {
54+
return $this->animationName;
55+
}
56+
}

0 commit comments

Comments
 (0)