\\~]+)[\\w]+ # elements | \\:{1,2}( # pseudo-elements after|before|first-letter|first-line|selection )) /ix'; /** * @var array> */ private static $cache = []; /** * Calculates the specificity of the given CSS selector. * * @return int<0, max> * * @internal */ public static function calculate(string $selector): int { if (!isset(self::$cache[$selector])) { $a = 0; /// @todo should exclude \# as well as "#" $matches = null; $b = \substr_count($selector, '#'); $c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $selector, $matches); $d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $selector, $matches); self::$cache[$selector] = ($a * 1000) + ($b * 100) + ($c * 10) + $d; } return self::$cache[$selector]; } /** * Clears the cache in order to lower memory usage. */ public static function clearCache(): void { self::$cache = []; } }