Skip to content

Commit 0ad0fab

Browse files
committed
CSSDocument->getAllValues may now also return CSSFunction argument values
1 parent 74f21c9 commit 0ad0fab

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

lib/CSSList.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,24 @@ protected function allRuleSets(&$aResult) {
4747
}
4848
}
4949

50-
protected function allValues($oElement, &$aResult, $sSearchString = null) {
50+
protected function allValues($oElement, &$aResult, $sSearchString = null, $bSearchInFunctionArguments = false) {
5151
if($oElement instanceof CSSList) {
5252
foreach($oElement->getContents() as $oContent) {
53-
$this->allValues($oContent, $aResult, $sSearchString);
53+
$this->allValues($oContent, $aResult, $sSearchString, $bSearchInFunctionArguments);
5454
}
5555
} else if($oElement instanceof CSSRuleSet) {
5656
foreach($oElement->getRules($sSearchString) as $oRule) {
57-
$this->allValues($oRule, $aResult, $sSearchString);
57+
$this->allValues($oRule, $aResult, $sSearchString, $bSearchInFunctionArguments);
5858
}
5959
} else if($oElement instanceof CSSRule) {
6060
foreach($oElement->getValues() as $aValues) {
6161
foreach($aValues as $mValue) {
6262
$aResult[] = $mValue;
63+
if($bSearchInFunctionArguments && $mValue instanceof CSSFunction) {
64+
foreach($mValue->getArguments() as $mArgument) {
65+
$aResult[] = $mArgument;
66+
}
67+
}
6368
}
6469
}
6570
}
@@ -113,8 +118,10 @@ public function getAllRuleSets() {
113118

114119
/**
115120
* Returns all CSSValue objects found recursively in the tree.
121+
* @param (object|string) $mElement the CSSList or CSSRuleSet to start the search from (defaults to the whole document). If a string is given, it is used as rule name filter (@see{CSSRuleSet->getRules()}).
122+
* @param (bool) $bSearchInFunctionArguments whether to also return CSSValue objects used as CSSFunction arguments.
116123
*/
117-
public function getAllValues($mElement = null) {
124+
public function getAllValues($mElement = null, $bSearchInFunctionArguments = false) {
118125
$sSearchString = null;
119126
if($mElement === null) {
120127
$mElement = $this;
@@ -123,7 +130,7 @@ public function getAllValues($mElement = null) {
123130
$mElement = $this;
124131
}
125132
$aResult = array();
126-
$this->allValues($mElement, $aResult, $sSearchString);
133+
$this->allValues($mElement, $aResult, $sSearchString, $bSearchInFunctionArguments);
127134
return $aResult;
128135
}
129136

lib/CSSRuleSet.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ public function addRule(CSSRule $oRule) {
1515
$this->aRules[$oRule->getRule()] = $oRule;
1616
}
1717

18+
/**
19+
* Returns all rules matching the given pattern
20+
* @param (null|string|CSSRule) $mRule pattern to search for. If null, returns all rules. if the pattern ends with a dash, all rules starting with the pattern are returned as well as one matching the pattern with the dash excluded. passing a CSSRule behaves like calling getRules($mRule->getRule()).
21+
* @example $oRuleSet->getRules('font-') //returns an array of all rules either beginning with font- or matching font.
22+
* @example $oRuleSet->getRules('font') //returns array('font' => $oRule) or array().
23+
*/
1824
public function getRules($mRule = null) {
1925
if($mRule === null) {
2026
return $this->aRules;

lib/CSSValue.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,23 @@ public function __toString() {
142142

143143
class CSSFunction extends CSSValue {
144144
private $sName;
145-
private $aContents;
145+
private $aArguments;
146146

147-
public function __construct($sName, $aContents) {
147+
public function __construct($sName, $aArguments) {
148148
$this->sName = $sName;
149-
$this->aContents = $aContents;
149+
$this->aArguments = $aArguments;
150150
}
151151

152152
public function getName() {
153153
return $this->sName;
154154
}
155155

156-
public function getContents() {
157-
return $this->aContents;
156+
public function getArguments() {
157+
return $this->aArguments;
158158
}
159159

160160
public function __toString() {
161-
$sContents = implode(',', $this->aContents);
162-
return "{$this->sName}({$sContents})";
161+
$aArguments = implode(',', $this->aArguments);
162+
return "{$this->sName}({$aArguments})";
163163
}
164164
}

tests/CSSParserTests.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,15 @@ function testSlashedValues() {
170170

171171
function testFunctionSyntax() {
172172
$oDoc = $this->parsedStructureForFile('functions');
173-
$this->assertSame('div.main {background-image: linear-gradient(rgb(0, 0, 0),rgb(255, 255, 255));}.collapser::before, .collapser::-moz-before, .collapser::-webkit-before {content: "»";font-size: 1.2em;margin-right: 0.2em;-moz-transition-property: -moz-transform;-moz-transition-duration: 0.2s;-moz-transform-origin: center 60%;}.collapser.expanded::before, .collapser.expanded::-moz-before, .collapser.expanded::-webkit-before {-moz-transform: rotate(90deg);}.collapser + * {height: 0;overflow: hidden;-moz-transition-property: height;-moz-transition-duration: 0.3s;}.collapser.expanded + * {height: auto;}', $oDoc->__toString());
173+
$sExpected = 'div.main {background-image: linear-gradient(rgb(0, 0, 0),rgb(255, 255, 255));}.collapser::before, .collapser::-moz-before, .collapser::-webkit-before {content: "»";font-size: 1.2em;margin-right: 0.2em;-moz-transition-property: -moz-transform;-moz-transition-duration: 0.2s;-moz-transform-origin: center 60%;}.collapser.expanded::before, .collapser.expanded::-moz-before, .collapser.expanded::-webkit-before {-moz-transform: rotate(90deg);}.collapser + * {height: 0;overflow: hidden;-moz-transition-property: height;-moz-transition-duration: 0.3s;}.collapser.expanded + * {height: auto;}';
174+
$this->assertSame($sExpected, $oDoc->__toString());
175+
foreach($oDoc->getAllValues(null, true) as $mValue) {
176+
if($mValue instanceof CSSSize && !$mValue->isRelative()) {
177+
$mValue->setSize($mValue->getSize()*2);
178+
}
179+
}
180+
$sExpected = str_replace(array('0.2s', '0.3s', '90deg'), array('0.4s', '0.6s', '180deg'), $sExpected);
181+
$this->assertSame($sExpected, $oDoc->__toString());
174182
}
175183

176184
function parsedStructureForFile($sFileName) {

tests/files/-tobedone.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.some[selectors-may='contain-a-{'] {
2+
3+
}
4+
5+
.some {
6+
filters: may(contain, a, ')');
7+
}
8+
9+
.ie-specific {
10+
filter: (may-contain=sign);
11+
}

0 commit comments

Comments
 (0)