CssDocument.php
108 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Pelago\Emogrifier\Css; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Sabberworm\CSS\CSSList\AtRuleBlockList as CssAtRuleBlockList; |
| 6 | use MailPoetVendor\Sabberworm\CSS\CSSList\Document as SabberwormCssDocument; |
| 7 | use MailPoetVendor\Sabberworm\CSS\Parser as CssParser; |
| 8 | use MailPoetVendor\Sabberworm\CSS\Property\AtRule as CssAtRule; |
| 9 | use MailPoetVendor\Sabberworm\CSS\Property\Charset as CssCharset; |
| 10 | use MailPoetVendor\Sabberworm\CSS\Property\Import as CssImport; |
| 11 | use MailPoetVendor\Sabberworm\CSS\Renderable as CssRenderable; |
| 12 | use MailPoetVendor\Sabberworm\CSS\RuleSet\DeclarationBlock as CssDeclarationBlock; |
| 13 | use MailPoetVendor\Sabberworm\CSS\RuleSet\RuleSet as CssRuleSet; |
| 14 | use MailPoetVendor\Sabberworm\CSS\Settings as ParserSettings; |
| 15 | class CssDocument |
| 16 | { |
| 17 | private $sabberwormCssDocument; |
| 18 | private $isImportRuleAllowed = \true; |
| 19 | public function __construct(string $css, bool $debug) |
| 20 | { |
| 21 | // CSS Parser currently throws exception with nested at-rules (like `@media`) in strict parsing mode |
| 22 | $parserSettings = ParserSettings::create()->withLenientParsing(!$debug || $this->hasNestedAtRule($css)); |
| 23 | // CSS Parser currently throws exception with non-empty whitespace-only CSS in strict parsing mode, so `trim()` |
| 24 | // @see https://github.com/sabberworm/PHP-CSS-Parser/issues/349 |
| 25 | $this->sabberwormCssDocument = (new CssParser(\trim($css), $parserSettings))->parse(); |
| 26 | } |
| 27 | private function hasNestedAtRule(string $css) : bool |
| 28 | { |
| 29 | return \preg_match('/@(?:media|supports|(?:-webkit-|-moz-|-ms-|-o-)?+(keyframes|document))\\b/', $css) === 1; |
| 30 | } |
| 31 | public function getStyleRulesData(array $allowedMediaTypes) : array |
| 32 | { |
| 33 | $ruleMatches = []; |
| 34 | foreach ($this->sabberwormCssDocument->getContents() as $rule) { |
| 35 | if ($rule instanceof CssAtRuleBlockList) { |
| 36 | $containingAtRule = $this->getFilteredAtIdentifierAndRule($rule, $allowedMediaTypes); |
| 37 | if (\is_string($containingAtRule)) { |
| 38 | foreach ($rule->getContents() as $nestedRule) { |
| 39 | if ($nestedRule instanceof CssDeclarationBlock) { |
| 40 | $ruleMatches[] = new StyleRule($nestedRule, $containingAtRule); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } elseif ($rule instanceof CssDeclarationBlock) { |
| 45 | $ruleMatches[] = new StyleRule($rule); |
| 46 | } |
| 47 | } |
| 48 | return $ruleMatches; |
| 49 | } |
| 50 | public function renderNonConditionalAtRules() : string |
| 51 | { |
| 52 | $this->isImportRuleAllowed = \true; |
| 53 | $cssContents = $this->sabberwormCssDocument->getContents(); |
| 54 | $atRules = \array_filter($cssContents, [$this, 'isValidAtRuleToRender']); |
| 55 | if ($atRules === []) { |
| 56 | return ''; |
| 57 | } |
| 58 | $atRulesDocument = new SabberwormCssDocument(); |
| 59 | $atRulesDocument->setContents($atRules); |
| 60 | return $atRulesDocument->render(); |
| 61 | } |
| 62 | private function getFilteredAtIdentifierAndRule(CssAtRuleBlockList $rule, array $allowedMediaTypes) : ?string |
| 63 | { |
| 64 | $result = null; |
| 65 | if ($rule->atRuleName() === 'media') { |
| 66 | $mediaQueryList = $rule->atRuleArgs(); |
| 67 | [$mediaType] = \explode('(', $mediaQueryList, 2); |
| 68 | if (\trim($mediaType) !== '') { |
| 69 | $escapedAllowedMediaTypes = \array_map(static function (string $allowedMediaType) : string { |
| 70 | return \preg_quote($allowedMediaType, '/'); |
| 71 | }, $allowedMediaTypes); |
| 72 | $mediaTypesMatcher = \implode('|', $escapedAllowedMediaTypes); |
| 73 | $isAllowed = \preg_match('/^\\s*+(?:only\\s++)?+(?:' . $mediaTypesMatcher . ')/i', $mediaType) > 0; |
| 74 | } else { |
| 75 | $isAllowed = \true; |
| 76 | } |
| 77 | if ($isAllowed) { |
| 78 | $result = '@media ' . $mediaQueryList; |
| 79 | } |
| 80 | } |
| 81 | return $result; |
| 82 | } |
| 83 | private function isValidAtRuleToRender(CssRenderable $rule) : bool |
| 84 | { |
| 85 | if ($rule instanceof CssCharset) { |
| 86 | return \false; |
| 87 | } |
| 88 | if ($rule instanceof CssImport) { |
| 89 | return $this->isImportRuleAllowed; |
| 90 | } |
| 91 | $this->isImportRuleAllowed = \false; |
| 92 | if (!$rule instanceof CssAtRule) { |
| 93 | return \false; |
| 94 | } |
| 95 | switch ($rule->atRuleName()) { |
| 96 | case 'media': |
| 97 | $result = \false; |
| 98 | break; |
| 99 | case 'font-face': |
| 100 | $result = $rule instanceof CssRuleSet && $rule->getRules('font-family') !== [] && $rule->getRules('src') !== []; |
| 101 | break; |
| 102 | default: |
| 103 | $result = \true; |
| 104 | } |
| 105 | return $result; |
| 106 | } |
| 107 | } |
| 108 |