CssDocument.php
188 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Pelago\Emogrifier\Css; |
| 6 | |
| 7 | use Sabberworm\CSS\CSSList\AtRuleBlockList as CssAtRuleBlockList; |
| 8 | use Sabberworm\CSS\CSSList\Document as SabberwormCssDocument; |
| 9 | use Sabberworm\CSS\Parser as CssParser; |
| 10 | use Sabberworm\CSS\Property\AtRule as CssAtRule; |
| 11 | use Sabberworm\CSS\Property\Charset as CssCharset; |
| 12 | use Sabberworm\CSS\Property\Import as CssImport; |
| 13 | use Sabberworm\CSS\Renderable as CssRenderable; |
| 14 | use Sabberworm\CSS\RuleSet\DeclarationBlock as CssDeclarationBlock; |
| 15 | use Sabberworm\CSS\RuleSet\RuleSet as CssRuleSet; |
| 16 | |
| 17 | /** |
| 18 | * Parses and stores a CSS document from a string of CSS, and provides methods to obtain the CSS in parts or as data |
| 19 | * structures. |
| 20 | * |
| 21 | * @internal |
| 22 | */ |
| 23 | class CssDocument |
| 24 | { |
| 25 | /** |
| 26 | * @var SabberwormCssDocument |
| 27 | */ |
| 28 | private $sabberwormCssDocument; |
| 29 | |
| 30 | /** |
| 31 | * `@import` rules must precede all other types of rules, except `@charset` rules. This property is used while |
| 32 | * rendering at-rules to enforce that. |
| 33 | * |
| 34 | * @var bool |
| 35 | */ |
| 36 | private $isImportRuleAllowed = true; |
| 37 | |
| 38 | /** |
| 39 | * @param string $css |
| 40 | */ |
| 41 | public function __construct(string $css) |
| 42 | { |
| 43 | $cssParser = new CssParser($css); |
| 44 | /** @var SabberwormCssDocument $sabberwormCssDocument */ |
| 45 | $sabberwormCssDocument = $cssParser->parse(); |
| 46 | $this->sabberwormCssDocument = $sabberwormCssDocument; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Collates the media query, selectors and declarations for individual rules from the parsed CSS, in order. |
| 51 | * |
| 52 | * @param array<array-key, string> $allowedMediaTypes |
| 53 | * |
| 54 | * @return array<int, StyleRule> |
| 55 | */ |
| 56 | public function getStyleRulesData(array $allowedMediaTypes): array |
| 57 | { |
| 58 | $ruleMatches = []; |
| 59 | /** @var CssRenderable $rule */ |
| 60 | foreach ($this->sabberwormCssDocument->getContents() as $rule) { |
| 61 | if ($rule instanceof CssAtRuleBlockList) { |
| 62 | $containingAtRule = $this->getFilteredAtIdentifierAndRule($rule, $allowedMediaTypes); |
| 63 | if (\is_string($containingAtRule)) { |
| 64 | /** @var CssRenderable $nestedRule */ |
| 65 | foreach ($rule->getContents() as $nestedRule) { |
| 66 | if ($nestedRule instanceof CssDeclarationBlock) { |
| 67 | $ruleMatches[] = new StyleRule($nestedRule, $containingAtRule); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } elseif ($rule instanceof CssDeclarationBlock) { |
| 72 | $ruleMatches[] = new StyleRule($rule); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $ruleMatches; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Renders at-rules from the parsed CSS that are valid and not conditional group rules (i.e. not rules such as |
| 81 | * `@media` which contain style rules whose data is returned by {@see getStyleRulesData}). Also does not render |
| 82 | * `@charset` rules; these are discarded (only UTF-8 is supported). |
| 83 | * |
| 84 | * @return string |
| 85 | */ |
| 86 | public function renderNonConditionalAtRules(): string |
| 87 | { |
| 88 | $this->isImportRuleAllowed = true; |
| 89 | /** @var array<int, CssRenderable> $cssContents */ |
| 90 | $cssContents = $this->sabberwormCssDocument->getContents(); |
| 91 | $atRules = \array_filter($cssContents, [$this, 'isValidAtRuleToRender']); |
| 92 | |
| 93 | if ($atRules === []) { |
| 94 | return ''; |
| 95 | } |
| 96 | |
| 97 | $atRulesDocument = new SabberwormCssDocument(); |
| 98 | $atRulesDocument->setContents($atRules); |
| 99 | |
| 100 | /** @var string $renderedRules */ |
| 101 | $renderedRules = $atRulesDocument->render(); |
| 102 | return $renderedRules; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param CssAtRuleBlockList $rule |
| 107 | * @param array<array-key, string> $allowedMediaTypes |
| 108 | * |
| 109 | * @return ?string |
| 110 | * If the nested at-rule is supported, it's opening declaration (e.g. "@media (max-width: 768px)") is |
| 111 | * returned; otherwise the return value is null. |
| 112 | */ |
| 113 | private function getFilteredAtIdentifierAndRule(CssAtRuleBlockList $rule, array $allowedMediaTypes): ?string |
| 114 | { |
| 115 | $result = null; |
| 116 | |
| 117 | if ($rule->atRuleName() === 'media') { |
| 118 | /** @var string $mediaQueryList */ |
| 119 | $mediaQueryList = $rule->atRuleArgs(); |
| 120 | [$mediaType] = \explode('(', $mediaQueryList, 2); |
| 121 | if (\trim($mediaType) !== '') { |
| 122 | $escapedAllowedMediaTypes = \array_map( |
| 123 | static function (string $allowedMediaType): string { |
| 124 | return \preg_quote($allowedMediaType, '/'); |
| 125 | }, |
| 126 | $allowedMediaTypes |
| 127 | ); |
| 128 | $mediaTypesMatcher = \implode('|', $escapedAllowedMediaTypes); |
| 129 | $isAllowed = \preg_match('/^\\s*+(?:only\\s++)?+(?:' . $mediaTypesMatcher . ')/i', $mediaType) > 0; |
| 130 | } else { |
| 131 | $isAllowed = true; |
| 132 | } |
| 133 | |
| 134 | if ($isAllowed) { |
| 135 | $result = '@media ' . $mediaQueryList; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return $result; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Tests if a CSS rule is an at-rule that should be passed though and copied to a `<style>` element unmodified: |
| 144 | * - `@charset` rules are discarded - only UTF-8 is supported - `false` is returned; |
| 145 | * - `@import` rules are passed through only if they satisfy the specification ("user agents must ignore any |
| 146 | * '@import' rule that occurs inside a block or after any non-ignored statement other than an '@charset' or an |
| 147 | * '@import' rule"); |
| 148 | * - `@media` rules are processed separately to see if their nested rules apply - `false` is returned; |
| 149 | * - `@font-face` rules are checked for validity - they must contain both a `src` and `font-family` property; |
| 150 | * - other at-rules are assumed to be valid and treated as a black box - `true` is returned. |
| 151 | * |
| 152 | * @param CssRenderable $rule |
| 153 | * |
| 154 | * @return bool |
| 155 | */ |
| 156 | private function isValidAtRuleToRender(CssRenderable $rule): bool |
| 157 | { |
| 158 | if ($rule instanceof CssCharset) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | if ($rule instanceof CssImport) { |
| 163 | return $this->isImportRuleAllowed; |
| 164 | } |
| 165 | |
| 166 | $this->isImportRuleAllowed = false; |
| 167 | |
| 168 | if (!$rule instanceof CssAtRule) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | switch ($rule->atRuleName()) { |
| 173 | case 'media': |
| 174 | $result = false; |
| 175 | break; |
| 176 | case 'font-face': |
| 177 | $result = $rule instanceof CssRuleSet |
| 178 | && $rule->getRules('font-family') !== [] |
| 179 | && $rule->getRules('src') !== []; |
| 180 | break; |
| 181 | default: |
| 182 | $result = true; |
| 183 | } |
| 184 | |
| 185 | return $result; |
| 186 | } |
| 187 | } |
| 188 |