|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mpdf\Css; |
| 4 | + |
| 5 | +/** |
| 6 | + * Settles the at-rules in a stylesheet before it is split into rules at each brace. |
| 7 | + * |
| 8 | + * The rules in a matching @media block, and in @supports and @layer blocks, are unwrapped into the stylesheet. An |
| 9 | + * @supports condition is taken to pass, as its rules are unwrapped, so an @supports not block, the fallback for |
| 10 | + * engines without a feature, is removed. @page rules are kept, less any margin boxes nested in them. Every other |
| 11 | + * at-rule is removed whole, with its block if it has one: splitting at each brace would otherwise end the at-rule at |
| 12 | + * its first inner brace and take the rest of it as the start of the next rule. |
| 13 | + */ |
| 14 | +class AtRuleProcessor |
| 15 | +{ |
| 16 | + |
| 17 | + /** |
| 18 | + * @var MediaQueryProcessor |
| 19 | + */ |
| 20 | + private $mediaQueryProcessor; |
| 21 | + |
| 22 | + /** |
| 23 | + * @param MediaQueryProcessor $mediaQueryProcessor |
| 24 | + */ |
| 25 | + public function __construct(MediaQueryProcessor $mediaQueryProcessor) |
| 26 | + { |
| 27 | + $this->mediaQueryProcessor = $mediaQueryProcessor; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Unwrap, keep or remove each at-rule in a list of rules, copying the other rules as they are |
| 32 | + * |
| 33 | + * @param string $css A stylesheet with its comments removed |
| 34 | + * @return string The stylesheet with only plain rules and @page rules left |
| 35 | + */ |
| 36 | + public function process($css) |
| 37 | + { |
| 38 | + if (strpos($css, '@') === false) { |
| 39 | + return $css; |
| 40 | + } |
| 41 | + |
| 42 | + $processed = ''; |
| 43 | + $length = strlen($css); |
| 44 | + $pos = 0; |
| 45 | + |
| 46 | + while ($pos < $length) { |
| 47 | + $at = $this->find($css, $pos, '@'); |
| 48 | + if ($at >= $length || $css[$at] === '}') { |
| 49 | + $processed .= substr($css, $pos, $at + 1 - $pos); |
| 50 | + $pos = $at + 1; |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + $processed .= substr($css, $pos, $at - $pos) . ' '; |
| 55 | + list($name, $prelude, $body, $end) = $this->readAtRule($css, $at); |
| 56 | + if ($body !== null) { |
| 57 | + $processed .= $this->processBlock($name, $prelude, $body) . ' '; |
| 58 | + } |
| 59 | + |
| 60 | + $pos = $end + 1; |
| 61 | + } |
| 62 | + |
| 63 | + return $processed; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * What a block at-rule leaves in the stylesheet |
| 68 | + * |
| 69 | + * @param string $name The at-rule's name in lower case, without the @ |
| 70 | + * @param string $prelude What comes between the name and the block |
| 71 | + * @param string $body What is inside the block |
| 72 | + * @return string |
| 73 | + */ |
| 74 | + private function processBlock($name, $prelude, $body) |
| 75 | + { |
| 76 | + switch ($name) { |
| 77 | + case 'media': |
| 78 | + return $this->mediaQueryProcessor->matches($prelude) ? $this->process($body) : ''; |
| 79 | + |
| 80 | + case 'supports': |
| 81 | + return preg_match('/^not\b/i', $prelude) ? '' : $this->process($body); |
| 82 | + |
| 83 | + case 'layer': |
| 84 | + return $this->process($body); |
| 85 | + |
| 86 | + case 'page': |
| 87 | + return '@page ' . $prelude . ' {' . $this->removeNestedAtRules($body) . '}'; |
| 88 | + } |
| 89 | + |
| 90 | + return ''; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Remove the at-rules from a block of declarations, such as the margin boxes in an @page rule |
| 95 | + * |
| 96 | + * @param string $declarations |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + private function removeNestedAtRules($declarations) |
| 100 | + { |
| 101 | + if (strpos($declarations, '@') === false) { |
| 102 | + return $declarations; |
| 103 | + } |
| 104 | + |
| 105 | + $length = strlen($declarations); |
| 106 | + $kept = ''; |
| 107 | + $pos = 0; |
| 108 | + |
| 109 | + while ($pos < $length) { |
| 110 | + $pos += strspn($declarations, " \t\n\r\f", $pos); |
| 111 | + if ($pos >= $length) { |
| 112 | + break; |
| 113 | + } |
| 114 | + |
| 115 | + if ($declarations[$pos] === '@') { |
| 116 | + list(, , , $end) = $this->readAtRule($declarations, $pos); |
| 117 | + } else { |
| 118 | + $end = $this->find($declarations, $pos, ';'); |
| 119 | + $kept .= ' ' . substr($declarations, $pos, $end + 1 - $pos); |
| 120 | + } |
| 121 | + |
| 122 | + $pos = $end + 1; |
| 123 | + } |
| 124 | + |
| 125 | + return $kept; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Read the at-rule starting at $at |
| 130 | + * |
| 131 | + * @param string $css |
| 132 | + * @param int $at The position of the @ |
| 133 | + * @return array The name in lower case, the prelude, and the block's content or null for a statement at-rule, |
| 134 | + * then the position of the semicolon or brace that ends it |
| 135 | + */ |
| 136 | + private function readAtRule($css, $at) |
| 137 | + { |
| 138 | + $open = $this->find($css, $at, ';{'); |
| 139 | + if ($open >= strlen($css) || $css[$open] !== '{') { |
| 140 | + return [null, null, null, $open]; |
| 141 | + } |
| 142 | + |
| 143 | + $close = $this->find($css, $open + 1, ''); |
| 144 | + preg_match('/^@([-\w]*)(.*)$/s', substr($css, $at, $open - $at), $m); |
| 145 | + |
| 146 | + return [strtolower($m[1]), trim($m[2]), substr($css, $open + 1, $close - $open - 1), $close]; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * The position of the first of $chars at or after $pos that is outside any string or nested block |
| 151 | + * |
| 152 | + * A closing brace that is not nested in the search ends it as well, since it closes the block being searched. The |
| 153 | + * length of the stylesheet is returned if neither is found. |
| 154 | + * |
| 155 | + * @param string $css |
| 156 | + * @param int $pos |
| 157 | + * @param string $chars |
| 158 | + * @return int |
| 159 | + */ |
| 160 | + private function find($css, $pos, $chars) |
| 161 | + { |
| 162 | + $length = strlen($css); |
| 163 | + $stops = $chars . '{}"\'\\'; |
| 164 | + $depth = 0; |
| 165 | + |
| 166 | + while ($pos < $length) { |
| 167 | + $pos += strcspn($css, $stops, $pos); |
| 168 | + if ($pos >= $length) { |
| 169 | + break; |
| 170 | + } |
| 171 | + |
| 172 | + $char = $css[$pos]; |
| 173 | + if ($depth === 0 && ($char === '}' || strpos($chars, $char) !== false)) { |
| 174 | + return $pos; |
| 175 | + } |
| 176 | + |
| 177 | + if ($char === '"' || $char === '\'') { |
| 178 | + $pos = $this->endOfString($css, $pos); |
| 179 | + } elseif ($char === '\\') { |
| 180 | + $pos++; |
| 181 | + } elseif ($char === '{') { |
| 182 | + $depth++; |
| 183 | + } elseif ($char === '}') { |
| 184 | + $depth--; |
| 185 | + } |
| 186 | + |
| 187 | + $pos++; |
| 188 | + } |
| 189 | + |
| 190 | + return $length; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * The position of the quote that closes the string opening at $pos, or of the line break or end of the stylesheet |
| 195 | + * that ends it unclosed |
| 196 | + * |
| 197 | + * @param string $css |
| 198 | + * @param int $pos |
| 199 | + * @return int |
| 200 | + */ |
| 201 | + private function endOfString($css, $pos) |
| 202 | + { |
| 203 | + $length = strlen($css); |
| 204 | + $quote = $css[$pos]; |
| 205 | + |
| 206 | + while (++$pos < $length) { |
| 207 | + $pos += strcspn($css, $quote . "\\\n\r\f", $pos); |
| 208 | + if ($pos >= $length || $css[$pos] !== '\\') { |
| 209 | + return $pos; |
| 210 | + } |
| 211 | + |
| 212 | + $pos++; |
| 213 | + } |
| 214 | + |
| 215 | + return $length; |
| 216 | + } |
| 217 | +} |
0 commit comments