WordPress at a glance
function is not described

Pelago

Emogrifier::parseCssRules() private WC 1.0

Extracts and parses the individual rules from a CSS string.

{} It's a method of the class: Emogrifier{}

No Hooks.

Return

String[][][]. A 2-entry array with the key "inlinable" containing rules which can be inlined as style attributes and the key "uninlinable" containing rules which cannot. Each value is an array of string sub-arrays with the keys "media" (the media query string, e.g. "@media screen and (max-width: 480px)", or an empty string if not from a @media rule), "selector" (the CSS selector, e.g., "*" or "header h1"), "hasUnmatchablePseudo" (true if that selector contains pseudo-elements or dynamic pseudo-classes such that the declarations cannot be applied inline), "declarationsBlock" (the semicolon-separated CSS declarations for that selector, e.g., "color: red; height: 4px;"), and "line" (the line number e.g. 42)

Usage

// private - for code of main (parent) class only
$result = $this->parseCssRules( $css );
$css(string) (required)
a string of raw CSS code with comments removed

Code of Emogrifier::parseCssRules() WC 5.0.0

<?php
private function parseCssRules($css)
{
    $cssKey = \md5($css);
    if (!isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) {
        $matches = $this->getCssRuleMatches($css);

        $cssRules = [
            'inlinable' => [],
            'uninlinable' => [],
        ];
        /** @var string[][] $matches */
        /** @var string[] $cssRule */
        foreach ($matches as $key => $cssRule) {
            $cssDeclaration = \trim($cssRule['declarations']);
            if ($cssDeclaration === '') {
                continue;
            }

            foreach (\explode(',', $cssRule['selectors']) as $selector) {
                // don't process pseudo-elements and behavioral (dynamic) pseudo-classes;
                // only allow structural pseudo-classes
                $hasPseudoElement = \strpos($selector, '::') !== false;
                $hasUnsupportedPseudoClass = (bool)\preg_match(
                    '/:(?!' . self::PSEUDO_CLASS_MATCHER . ')[\\w\\-]/i',
                    $selector
                );
                $hasUnmatchablePseudo = $hasPseudoElement || $hasUnsupportedPseudoClass;

                $parsedCssRule = [
                    'media' => $cssRule['media'],
                    'selector' => \trim($selector),
                    'hasUnmatchablePseudo' => $hasUnmatchablePseudo,
                    'declarationsBlock' => $cssDeclaration,
                    // keep track of where it appears in the file, since order is important
                    'line' => $key,
                ];
                $ruleType = ($cssRule['media'] === '' && !$hasUnmatchablePseudo) ? 'inlinable' : 'uninlinable';
                $cssRules[$ruleType][] = $parsedCssRule;
            }
        }

        \usort($cssRules['inlinable'], [$this, 'sortBySelectorPrecedence']);

        $this->caches[self::CACHE_KEY_CSS][$cssKey] = $cssRules;
    }

    return $this->caches[self::CACHE_KEY_CSS][$cssKey];
}