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