withLenientParsing(!$debug || $this->hasNestedAtRule($css)); // CSS Parser currently throws exception with non-empty whitespace-only CSS in strict parsing mode, so `trim()` // @see https://github.com/sabberworm/PHP-CSS-Parser/issues/349 $this->sabberwormCssDocument = (new CssParser(\trim($css), $parserSettings))->parse(); } /** * Tests if a string of CSS appears to contain an at-rule with nested rules * (`@media`, `@supports`, `@keyframes`, `@document`, * the latter two additionally with vendor prefixes that may commonly be used). * * @see https://github.com/sabberworm/PHP-CSS-Parser/issues/127 */ private function hasNestedAtRule(string $css): bool { return (new Preg()) ->match('/@(?:media|supports|(?:-webkit-|-moz-|-ms-|-o-)?+(keyframes|document))\\b/', $css) !== 0; } /** * Collates the media query, selectors and declarations for individual rules from the parsed CSS, in order. * * @param array $allowedMediaTypes * * @return list */ public function getStyleRulesData(array $allowedMediaTypes): array { $ruleMatches = []; /** @var CssRenderable $rule */ foreach ($this->sabberwormCssDocument->getContents() as $rule) { if ($rule instanceof CssAtRuleBlockList) { $containingAtRule = $this->getFilteredAtIdentifierAndRule($rule, $allowedMediaTypes); if (\is_string($containingAtRule)) { /** @var CssRenderable $nestedRule */ foreach ($rule->getContents() as $nestedRule) { if ($nestedRule instanceof CssDeclarationBlock) { $ruleMatches[] = new StyleRule($nestedRule, $containingAtRule); } } } } elseif ($rule instanceof CssDeclarationBlock) { $ruleMatches[] = new StyleRule($rule); } } return $ruleMatches; } /** * Renders at-rules from the parsed CSS that are valid and not conditional group rules (i.e. not rules such as * `@media` which contain style rules whose data is returned by {@see getStyleRulesData}). Also does not render * `@charset` rules; these are discarded (only UTF-8 is supported). * * @return string */ public function renderNonConditionalAtRules(): string { $this->isImportRuleAllowed = true; $cssContents = $this->sabberwormCssDocument->getContents(); $atRules = \array_filter($cssContents, [$this, 'isValidAtRuleToRender']); if ($atRules === []) { return ''; } $atRulesDocument = new SabberwormCssDocument(); $atRulesDocument->setContents($atRules); return $atRulesDocument->render(); } /** * @param CssAtRuleBlockList $rule * @param array $allowedMediaTypes * * @return ?string * If the nested at-rule is supported, it's opening declaration (e.g. "@media (max-width: 768px)") is * returned; otherwise the return value is null. */ private function getFilteredAtIdentifierAndRule(CssAtRuleBlockList $rule, array $allowedMediaTypes): ?string { $result = null; if ($rule->atRuleName() === 'media') { $mediaQueryList = $rule->atRuleArgs(); [$mediaType] = \explode('(', $mediaQueryList, 2); if (\trim($mediaType) !== '') { $escapedAllowedMediaTypes = \array_map( static function (string $allowedMediaType): string { return \preg_quote($allowedMediaType, '/'); }, $allowedMediaTypes ); $mediaTypesMatcher = \implode('|', $escapedAllowedMediaTypes); $isAllowed = (new Preg())->match('/^\\s*+(?:only\\s++)?+(?:' . $mediaTypesMatcher . ')/i', $mediaType) !== 0; } else { $isAllowed = true; } if ($isAllowed) { $result = '@media ' . $mediaQueryList; } } return $result; } /** * Tests if a CSS rule is an at-rule that should be passed though and copied to a `