Skip to content

Commit 603499c

Browse files
author
petkodimitrov
committed
Add get all css functions method
1 parent 7276b93 commit 603499c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/CSSList/CSSBlockList.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,27 @@ protected function allSelectors(array &$aResult, $sSpecificitySearch = null)
140140
}
141141
}
142142
}
143+
144+
/**
145+
* @param CSSList|Rule|RuleSet|Value $oElement
146+
* @param array<int, CSSFunction> $aResult
147+
*
148+
* @return void
149+
*/
150+
protected function allFunctions($oElement, array &$aResult)
151+
{
152+
if ($oElement instanceof CSSBlockList) {
153+
foreach ($oElement->getContents() as $oContent) {
154+
$this->allFunctions($oContent, $aResult);
155+
}
156+
} elseif ($oElement instanceof RuleSet) {
157+
foreach ($oElement->getRules() as $oRule) {
158+
$this->allFunctions($oRule, $aResult);
159+
}
160+
} elseif ($oElement instanceof Rule) {
161+
$this->allFunctions($oElement->getValue(), $aResult);
162+
} elseif ($oElement instanceof CSSFunction) {
163+
$aResult[] = $oElement;
164+
}
165+
}
143166
}

src/CSSList/Document.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ public function getSelectorsBySpecificity($sSpecificitySearch = null)
109109
return $aResult;
110110
}
111111

112+
/**
113+
* Returns all `CSSFunction` objects recursively found in the tree, no matter how deeply nested the rule sets are.
114+
*
115+
* @param CSSList|RuleSet|string $mElement
116+
* the `CSSList` or `RuleSet` to start the search from (defaults to the whole document).
117+
* If a string is given, it is used as rule name filter.
118+
*
119+
* @return array<int, CSSFunction>
120+
*/
121+
public function getAllFunctions($mElement = null)
122+
{
123+
if ($mElement === null) {
124+
$mElement = $this;
125+
}
126+
/** @var array<int, Value> $aResult */
127+
$aResult = [];
128+
$this->allFunctions($mElement, $aResult);
129+
return $aResult;
130+
}
131+
112132
/**
113133
* Expands all shorthand properties to their long value.
114134
*

0 commit comments

Comments
 (0)