CssOptimizer.php 9.4 KB

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