Automattic\WooCommerce\Vendor\Pelago\Emogrifier

CssInliner::removeUnmatchablePseudoComponentsprivateWC 1.0

Removes pseudo-elements and dynamic pseudo-classes from a CSS selector, replacing them with "*" if necessary. If such a pseudo-component is within the argument of :not, the entire :not component is removed or replaced.

Method of the class: CssInliner{}

No Hooks.

Returns

String . selector which will match the relevant DOM elements if the pseudo-classes are assumed to apply, or in the case of pseudo-elements will match their originating element

Usage

// private - for code of main (parent) class only
$result = $this->removeUnmatchablePseudoComponents( $selector ): string;
$selector(string) (required)
.

CssInliner::removeUnmatchablePseudoComponents() code WC 10.9.4

private function removeUnmatchablePseudoComponents(string $selector): string
{
    $preg = (new Preg())->throwExceptions($this->debug);

    // The regex allows nested brackets via `(?2)`.
    // A space is temporarily prepended because the callback can't determine if the match was at the very start.
    $selectorWithoutNots = \ltrim((new Preg())->throwExceptions($this->debug)->replaceCallback(
        '/([\\s>+~]?+):not(\\([^()]*+(?:(?2)[^()]*+)*+\\))/i',
        /** @param array<array-key, string> $matches */
        function (array $matches): string {
            return $this->replaceUnmatchableNotComponent($matches);
        },
        ' ' . $selector
    ));

    $selectorWithoutUnmatchablePseudoComponents = $this->removeSelectorComponents(
        ':(?!' . self::PSEUDO_CLASS_MATCHER . '):?+[\\w\\-]++(?:\\([^\\)]*+\\))?+',
        $selectorWithoutNots
    );

    if (
        $preg->match(
            '/:(?:' . self::OF_TYPE_PSEUDO_CLASS_MATCHER . ')/i',
            $selectorWithoutUnmatchablePseudoComponents
        )
        === 0
    ) {
        return $selectorWithoutUnmatchablePseudoComponents;
    }
    return \implode('', \array_map(
        function (string $selectorPart): string {
            return $this->removeUnsupportedOfTypePseudoClasses($selectorPart);
        },
        $preg->split(
            '/(' . self::COMBINATOR_MATCHER . ')/',
            $selectorWithoutUnmatchablePseudoComponents,
            -1,
            PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
        )
    ));
}