3 namespace Drupal\Core\Asset;
5 use Drupal\Component\Utility\Unicode;
8 * Optimizes a CSS asset.
10 class CssOptimizer implements AssetOptimizerInterface {
13 * The base path used by rewriteFileURI().
17 public $rewriteFileURIBasePath;
22 public function optimize(array $css_asset) {
23 if ($css_asset['type'] != 'file') {
24 throw new \Exception('Only file CSS assets can be optimized.');
26 if (!$css_asset['preprocess']) {
27 throw new \Exception('Only file CSS assets with preprocessing enabled can be optimized.');
30 return $this->processFile($css_asset);
34 * Processes the contents of a CSS asset for cleanup.
36 * @param string $contents
37 * The contents of the CSS asset.
40 * Contents of the CSS asset.
42 public function clean($contents) {
43 // Remove multiple charset declarations for standards compliance (and fixing
45 $contents = preg_replace('/^@charset\s+[\'"](\S*?)\b[\'"];/i', '', $contents);
51 * Build aggregate CSS file.
53 protected function processFile($css_asset) {
54 $contents = $this->loadFile($css_asset['data'], TRUE);
56 $contents = $this->clean($contents);
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'], '/'));
61 $this->rewriteFileURIBasePath = $css_base_path . '/';
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);
68 * Loads the stylesheet and resolves all @import commands.
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
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.
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
83 * Name of the stylesheet to be processed.
85 * Defines if CSS contents should be compressed or not.
86 * @param $reset_basepath
87 * Used internally to facilitate recursive resolution of @import commands.
90 * Contents of the stylesheet, including any resolved @import commands.
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) {
98 // Store the value of $optimize for preg_replace_callback with nested
100 if (isset($optimize)) {
101 $_optimize = $optimize;
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;
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);
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.
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);
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]);
132 // Return the processed stylesheet.
133 $content = $this->processCss($contents, $_optimize);
136 // Restore the parent base path as the file and its children are processed.
137 $basepath = $parent_base_path;
142 * Loads stylesheets recursively and returns contents with corrected paths.
144 * This function is used for recursive loading of stylesheets and
145 * returns the stylesheet content with all url() paths corrected.
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.
152 * The contents of the CSS file at $matches[1], with corrected paths.
154 * @see \Drupal\Core\Asset\AssetOptimizerInterface::loadFile()
156 protected function loadNestedFile($matches) {
157 $filename = $matches[1];
158 // Load the imported stylesheet and replace @import commands in there as
160 $file = $this->loadFile($filename, NULL, FALSE);
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
166 $directory = $directory == '.' ? '' : $directory . '/';
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);
174 * Processes the contents of a stylesheet for aggregation.
177 * The contents of the stylesheet.
179 * (optional) Boolean whether CSS contents should be minified. Defaults to
183 * Contents of the stylesheet including the imported stylesheets.
185 protected function processCss($contents, $optimize = FALSE) {
186 // Remove unwanted CSS code that cause issues.
187 $contents = $this->clean($contents);
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",
203 // Remove certain whitespace.
204 // There are different conditions for removing leading and trailing
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.
212 # Strip only leading whitespace from:
213 # - Closing parenthesis: Retain "@media (bar) and foo".
215 # Strip only trailing whitespace from:
216 # - Opening parenthesis: Retain "@media (bar) and foo".
217 # - Colon: Retain :pseudo-selectors.
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.
226 // End the file with a new line.
227 $contents = trim($contents);
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);
239 * Prefixes all paths within a CSS file for processFile().
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
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.
252 public function rewriteFileURI($matches) {
253 // Prefix with base and remove '../' segments where possible.
254 $path = $this->rewriteFileURIBasePath . $matches[1];
256 while ($path != $last) {
258 $path = preg_replace('`(^|/)(?!\.\./)([^/]+)/\.\./`', '$1', $path);
260 return 'url(' . file_url_transform_relative(file_create_url($path)) . ')';