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.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / vendor / pelago / emogrifier / src / Css / CssDocument.php
woocommerce / vendor / pelago / emogrifier / src / Css Last commit date
CssDocument.php 4 years ago StyleRule.php 4 years ago
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