Automattic\WooCommerce\Vendor\Pelago\Emogrifier
CssInliner::generateStyleStringFromDeclarationsArrays
This method merges old or existing name/value array with new name/value array and then generates a string of the combined style suitable for placing inline. This becomes the single point for CSS string generation allowing for consistent CSS output no matter where the CSS originally came from.
Method of the class: CssInliner{}
No Hooks.
Returns
String.
Usage
// private - for code of main (parent) class only $result = $this->generateStyleStringFromDeclarationsArrays( $oldStyles, $newStyles ): string;
- $oldStyles(array) (required)
- .
- $newStyles(array) (required)
- .
CssInliner::generateStyleStringFromDeclarationsArrays() CssInliner::generateStyleStringFromDeclarationsArrays code WC 10.4.3
private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles): string
{
$cacheKey = \serialize([$oldStyles, $newStyles]);
if (isset($this->caches[self::CACHE_KEY_COMBINED_STYLES][$cacheKey])) {
return $this->caches[self::CACHE_KEY_COMBINED_STYLES][$cacheKey];
}
// Unset the overridden styles to preserve order, important if shorthand and individual properties are mixed
foreach ($oldStyles as $attributeName => $attributeValue) {
if (!isset($newStyles[$attributeName])) {
continue;
}
$newAttributeValue = $newStyles[$attributeName];
if (
$this->attributeValueIsImportant($attributeValue)
&& !$this->attributeValueIsImportant($newAttributeValue)
) {
unset($newStyles[$attributeName]);
} else {
unset($oldStyles[$attributeName]);
}
}
$combinedStyles = \array_merge($oldStyles, $newStyles);
$declarationBlockParser = new DeclarationBlockParser();
$style = '';
foreach ($combinedStyles as $attributeName => $attributeValue) {
$trimmedAttributeName = \trim($attributeName);
if ($trimmedAttributeName === '') {
throw new \UnexpectedValueException('An empty property name was encountered.', 1727046078);
}
$propertyName = $declarationBlockParser->normalizePropertyName($trimmedAttributeName);
$propertyValue = \trim($attributeValue);
$style .= $propertyName . ': ' . $propertyValue . '; ';
}
$trimmedStyle = \rtrim($style);
$this->caches[self::CACHE_KEY_COMBINED_STYLES][$cacheKey] = $trimmedStyle;
return $trimmedStyle;
}