Automattic\WooCommerce\Vendor\Pelago\Emogrifier\Utilities
DeclarationBlockParser::parse
Parses a CSS declaration block into property name/value pairs.
Example:
The declaration block
"color: #000; font-weight: bold;"
will be parsed into the following array:
"color" => "#000" "font-weight" => "bold"
Method of the class: DeclarationBlockParser{}
No Hooks.
Returns
Array
Usage
$DeclarationBlockParser = new DeclarationBlockParser(); $DeclarationBlockParser->parse( $declarationBlock ): array;
- $declarationBlock(string) (required)
- the CSS declarations block without the curly braces, may be empty.
DeclarationBlockParser::parse() DeclarationBlockParser::parse code WC 10.9.4
public function parse(string $declarationBlock): array
{
if (isset(self::$cache[$declarationBlock])) {
return self::$cache[$declarationBlock];
}
$preg = new Preg();
$declarations = $preg->split('/;(?!base64|charset)/', $declarationBlock);
$properties = [];
foreach ($declarations as $declaration) {
$matches = [];
if (
$preg->match(
'/^([A-Za-z\\-]+)\\s*:\\s*(.+)$/s',
\trim($declaration),
$matches
)
=== 0
) {
continue;
}
$propertyName = $matches[1];
if ($propertyName === '') {
// This cannot happen since the regular epression matches one or more characters.
throw new \UnexpectedValueException('An empty property name was encountered.', 1727046409);
}
$propertyValue = $matches[2];
$properties[$this->normalizePropertyName($propertyName)] = $propertyValue;
}
self::$cache[$declarationBlock] = $properties;
return $properties;
}