Skip to content

Add get all css functions method #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/CSSList/CSSBlockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,27 @@ protected function allSelectors(array &$aResult, $sSpecificitySearch = null)
}
}
}

/**
* @param CSSList|Rule|RuleSet|Value $oElement
* @param array<int, CSSFunction> $aResult
*
* @return void
*/
protected function allFunctions($oElement, array &$aResult)
{
if ($oElement instanceof CSSBlockList) {
foreach ($oElement->getContents() as $oContent) {
$this->allFunctions($oContent, $aResult);
}
} elseif ($oElement instanceof RuleSet) {
foreach ($oElement->getRules() as $oRule) {
$this->allFunctions($oRule, $aResult);
}
} elseif ($oElement instanceof Rule) {
$this->allFunctions($oElement->getValue(), $aResult);
} elseif ($oElement instanceof CSSFunction) {
$aResult[] = $oElement;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider using generators to reduce memory usage? Would it be worthy 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider using generators to reduce memory usage? Would it be worthy 🤔

Nice. Yet I don't think this would go well with upstream if this change makes it there :) (very conservative. Maybe with good reason)

}
}
}
20 changes: 20 additions & 0 deletions src/CSSList/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ public function getSelectorsBySpecificity($sSpecificitySearch = null)
return $aResult;
}

/**
* Returns all `CSSFunction` objects recursively found in the tree, no matter how deeply nested the rule sets are.
*
* @param CSSList|RuleSet|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.
*
* @return array<int, CSSFunction>
*/
public function getAllFunctions($mElement = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Could we think of a better name for this? Maybe getFunctionsRecursively()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Could we think of a better name for this? Maybe getFunctionsRecursively()?

I'm following the naming scheme of the file eg. getAllValues, getAllRulesets, etc.

{
if ($mElement === null) {
$mElement = $this;
}
/** @var array<int, Value> $aResult */
$aResult = [];
$this->allFunctions($mElement, $aResult);
return $aResult;
}

/**
* Expands all shorthand properties to their long value.
*
Expand Down
1 change: 1 addition & 0 deletions src/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Size extends PrimitiveValue
'vh', 'dvh', 'svh', 'lvh',
'vw', 'vmin', 'vmax', 'rem',
'svw', 'lvw', 'dvw',
'x', 'dppx', 'dpi', 'dpcm',
];

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1325,4 +1325,19 @@ public function escapedSpecialCaseTokens(): void
self::assertTrue(\is_a($urlRule->getValue(), '\\Sabberworm\\CSS\\Value\\URL'));
self::assertTrue(\is_a($calcRule->getValue(), '\\Sabberworm\\CSS\\Value\\CalcFunction'));
}

/**
* @test
*/
public function imagesetInFile(): void
{
$oDoc = self::parsedStructureForFile('image-set', Settings::create()->withMultibyteSupport(true));
$sExpected = sprintf(
'%s%s%s',
'.home_banner {background-image: image-set(url("https://www.example.us/images/home-banner.webp") 1x,',
'url("https://www.example.us/images/home-banner@2x.webp") 2x,',
'url("https://www.example.us/images/home-banner@3x.webp") 3x);}'
);
self::assertSame($sExpected, $oDoc->render());
}
}
1 change: 1 addition & 0 deletions tests/fixtures/image-set.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.