94c2240abc1615db728dd69a4ff0c147ab214eae
[yaffs-website] / web / core / lib / Drupal / Core / Asset / CssOptimizer.php
1 <?php
2
3 namespace Drupal\Core\Asset;
4
5 use Drupal\Component\Utility\Unicode;
6
7 /**
8  * Optimizes a CSS asset.
9  */
10 class CssOptimizer implements AssetOptimizerInterface {
11
12   /**
13    * The base path used by rewriteFileURI().
14    *
15    * @var string
16    */
17   public $rewriteFileURIBasePath;
18
19   /**
20    * {@inheritdoc}
21    */
22   public function optimize(array $css_asset) {
23     if ($css_asset['type'] != 'file') {
24       throw new \Exception('Only file CSS assets can be optimized.');
25     }
26     if (!$css_asset['preprocess']) {
27       throw new \Exception('Only file CSS assets with preprocessing enabled can be optimized.');
28     }
29
30     return $this->processFile($css_asset);
31   }
32
33   /**
34    * Processes the contents of a CSS asset for cleanup.
35    *
36    * @param string $contents
37    *   The contents of the CSS asset.
38    *
39    * @return string
40    *   Contents of the CSS asset.
41    */
42   public function clean($contents) {
43     // Remove multiple charset declarations for standards compliance (and fixing
44     // Safari problems).
45     $contents = preg_replace('/^@charset\s+[\'"](\S*?)\b[\'"];/i', '', $contents);
46
47     return $contents;
48   }
49
50   /**
51    * Build aggregate CSS file.
52    */
53   protected function processFile($css_asset) {
54     $contents = $this->loadFile($css_asset['data'], TRUE);
55
56     $contents = $this->clean($contents);
57
58     // Get the parent directory of this file, relative to the Drupal root.
59     $css_base_path = substr($css_asset['data'], 0, strrpos($css_asset['data'], '/'));
60     // Store base path.
61     $this->rewriteFileURIBasePath = $css_base_path . '/';
62
63     // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
64     return preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', [$this, 'rewriteFileURI'], $contents);
65   }
66
67   /**
68    * Loads the stylesheet and resolves all @import commands.
69    *
70    * Loads a stylesheet and replaces @import commands with the contents of the
71    * imported file. Use this instead of file_get_contents when processing
72    * stylesheets.
73    *
74    * The returned contents are compressed removing white space and comments only
75    * when CSS aggregation is enabled. This optimization will not apply for
76    * color.module enabled themes with CSS aggregation turned off.
77    *
78    * Note: the only reason this method is public is so color.module can call it;
79    * it is not on the AssetOptimizerInterface, so future refactorings can make
80    * it protected.
81    *
82    * @param $file
83    *   Name of the stylesheet to be processed.
84    * @param $optimize
85    *   Defines if CSS contents should be compressed or not.
86    * @param $reset_basepath
87    *   Used internally to facilitate recursive resolution of @import commands.
88    *
89    * @return
90    *   Contents of the stylesheet, including any resolved @import commands.
91    */
92   public function loadFile($file, $optimize = NULL, $reset_basepath = TRUE) {
93     // These statics are not cache variables, so we don't use drupal_static().
94     static $_optimize, $basepath;
95     if ($reset_basepath) {
96       $basepath = '';
97     }
98     // Store the value of $optimize for preg_replace_callback with nested
99     // @import loops.
100     if (isset($optimize)) {
101       $_optimize = $optimize;
102     }
103
104     // Stylesheets are relative one to each other. Start by adding a base path
105     // prefix provided by the parent stylesheet (if necessary).
106     if ($basepath && !file_uri_scheme($file)) {
107       $file = $basepath . '/' . $file;
108     }
109     // Store the parent base path to restore it later.
110     $parent_base_path = $basepath;
111     // Set the current base path to process possible child imports.
112     $basepath = dirname($file);
113
114     // Load the CSS stylesheet. We suppress errors because themes may specify
115     // stylesheets in their .info.yml file that don't exist in the theme's path,
116     // but are merely there to disable certain module CSS files.
117     $content = '';
118     if ($contents = @file_get_contents($file)) {
119       // If a BOM is found, convert the file to UTF-8, then use substr() to
120       // remove the BOM from the result.
121       if ($encoding = (Unicode::encodingFromBOM($contents))) {
122         $contents = mb_substr(Unicode::convertToUtf8($contents, $encoding), 1);
123       }
124       // If no BOM, check for fallback encoding. Per CSS spec the regex is very strict.
125       elseif (preg_match('/^@charset "([^"]+)";/', $contents, $matches)) {
126         if ($matches[1] !== 'utf-8' && $matches[1] !== 'UTF-8') {
127           $contents = substr($contents, strlen($matches[0]));
128           $contents = Unicode::convertToUtf8($contents, $matches[1]);
129         }
130       }
131
132       // Return the processed stylesheet.
133       $content = $this->processCss($contents, $_optimize);
134     }
135
136     // Restore the parent base path as the file and its children are processed.
137     $basepath = $parent_base_path;
138     return $content;
139   }
140
141   /**
142    * Loads stylesheets recursively and returns contents with corrected paths.
143    *
144    * This function is used for recursive loading of stylesheets and
145    * returns the stylesheet content with all url() paths corrected.
146    *
147    * @param array $matches
148    *   An array of matches by a preg_replace_callback() call that scans for
149    *   @import-ed CSS files, except for external CSS files.
150    *
151    * @return
152    *   The contents of the CSS file at $matches[1], with corrected paths.
153    *
154    * @see \Drupal\Core\Asset\AssetOptimizerInterface::loadFile()
155    */
156   protected function loadNestedFile($matches) {
157     $filename = $matches[1];
158     // Load the imported stylesheet and replace @import commands in there as
159     // well.
160     $file = $this->loadFile($filename, NULL, FALSE);
161
162     // Determine the file's directory.
163     $directory = dirname($filename);
164     // If the file is in the current directory, make sure '.' doesn't appear in
165     // the url() path.
166     $directory = $directory == '.' ? '' : $directory . '/';
167
168     // Alter all internal url() paths. Leave external paths alone. We don't need
169     // to normalize absolute paths here because that will be done later.
170     return preg_replace('/url\(\s*([\'"]?)(?![a-z]+:|\/+)([^\'")]+)([\'"]?)\s*\)/i', 'url(\1' . $directory . '\2\3)', $file);
171   }
172
173   /**
174    * Processes the contents of a stylesheet for aggregation.
175    *
176    * @param $contents
177    *   The contents of the stylesheet.
178    * @param $optimize
179    *   (optional) Boolean whether CSS contents should be minified. Defaults to
180    *   FALSE.
181    *
182    * @return
183    *   Contents of the stylesheet including the imported stylesheets.
184    */
185   protected function processCss($contents, $optimize = FALSE) {
186     // Remove unwanted CSS code that cause issues.
187     $contents = $this->clean($contents);
188
189     if ($optimize) {
190       // Perform some safe CSS optimizations.
191       // Regexp to match comment blocks.
192       $comment = '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/';
193       // Regexp to match double quoted strings.
194       $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
195       // Regexp to match single quoted strings.
196       $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
197       // Strip all comment blocks, but keep double/single quoted strings.
198       $contents = preg_replace(
199         "<($double_quot|$single_quot)|$comment>Ss",
200         "$1",
201         $contents
202       );
203       // Remove certain whitespace.
204       // There are different conditions for removing leading and trailing
205       // whitespace.
206       // @see http://php.net/manual/regexp.reference.subpatterns.php
207       $contents = preg_replace('<
208         # Do not strip any space from within single or double quotes
209           (' . $double_quot . '|' . $single_quot . ')
210         # Strip leading and trailing whitespace.
211         | \s*([@{};,])\s*
212         # Strip only leading whitespace from:
213         # - Closing parenthesis: Retain "@media (bar) and foo".
214         | \s+([\)])
215         # Strip only trailing whitespace from:
216         # - Opening parenthesis: Retain "@media (bar) and foo".
217         # - Colon: Retain :pseudo-selectors.
218         | ([\(:])\s+
219       >xSs',
220         // Only one of the four capturing groups will match, so its reference
221         // will contain the wanted value and the references for the
222         // two non-matching groups will be replaced with empty strings.
223         '$1$2$3$4',
224         $contents
225       );
226       // End the file with a new line.
227       $contents = trim($contents);
228       $contents .= "\n";
229     }
230
231     // Replaces @import commands with the actual stylesheet content.
232     // This happens recursively but omits external files.
233     $contents = preg_replace_callback('/@import\s*(?:url\(\s*)?[\'"]?(?![a-z]+:)(?!\/\/)([^\'"\()]+)[\'"]?\s*\)?\s*;/', [$this, 'loadNestedFile'], $contents);
234
235     return $contents;
236   }
237
238   /**
239    * Prefixes all paths within a CSS file for processFile().
240    *
241    * Note: the only reason this method is public is so color.module can call it;
242    * it is not on the AssetOptimizerInterface, so future refactorings can make
243    * it protected.
244    *
245    * @param array $matches
246    *   An array of matches by a preg_replace_callback() call that scans for
247    *   url() references in CSS files, except for external or absolute ones.
248    *
249    * @return string
250    *   The file path.
251    */
252   public function rewriteFileURI($matches) {
253     // Prefix with base and remove '../' segments where possible.
254     $path = $this->rewriteFileURIBasePath . $matches[1];
255     $last = '';
256     while ($path != $last) {
257       $last = $path;
258       $path = preg_replace('`(^|/)(?!\.\./)([^/]+)/\.\./`', '$1', $path);
259     }
260     return 'url(' . file_url_transform_relative(file_create_url($path)) . ')';
261   }
262
263 }