8000 Merge pull request #2 from NitroPack/update/image-set_basic_support · NitroPack/PHP-CSS-Parser@ac01e86 · GitHub
Skip to content

Commit ac01e86

Browse files
authored
Merge pull request #2 from NitroPack/update/image-set_basic_support
Add get all css functions method
2 parents 5d9902b + 3feb3ff commit ac01e86

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-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
*

src/Value/Size.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Size extends PrimitiveValue
2323
'vh', 'dvh', 'svh', 'lvh',
2424
'vw', 'vmin', 'vmax', 'rem',
2525
'svw', 'lvw', 'dvw',
26+
'x', 'dppx', 'dpi', 'dpcm',
2627
];
2728

2829
/**

tests/ParserTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,4 +1325,19 @@ public function escapedSpecialCaseTokens(): void
13251325
self::assertTrue(\is_a($urlRule->getValue(), '\\Sabberworm\\CSS\\Value\\URL'));
13261326
self::assertTrue(\is_a($calcRule->getValue(), '\\Sabberworm\\CSS\\Value\\CalcFunction'));
13271327
}
1328+
1329+
/**
1330+
* @test
1331+
*/
1332+
public function imagesetInFile(): void
1333+
{
1334+
$oDoc = self::parsedStructureForFile('image-set', Settings::create()->withMultibyteSupport(true));
1335+
$sExpected = sprintf(
1336+
'%s%s%s',
1337+
'.home_banner {background-image: image-set(url("https://www.example.us/images/home-banner.webp") 1x,',
1338+
'url("https://www.example.us/images/home-banner@2x.webp") 2x,',
1339+
'url("https://www.example.us/images/home-banner@3x.webp") 3x);}'
1340+
);
1341+
self::assertSame($sExpected, $oDoc->render());
1342+
}
13281343
}

tests/fixtures/image-set.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)