Chris@0: processFile($css_asset); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Processes the contents of a CSS asset for cleanup. Chris@0: * Chris@0: * @param string $contents Chris@0: * The contents of the CSS asset. Chris@0: * Chris@0: * @return string Chris@0: * Contents of the CSS asset. Chris@0: */ Chris@0: public function clean($contents) { Chris@0: // Remove multiple charset declarations for standards compliance (and fixing Chris@0: // Safari problems). Chris@0: $contents = preg_replace('/^@charset\s+[\'"](\S*?)\b[\'"];/i', '', $contents); Chris@0: Chris@0: return $contents; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Build aggregate CSS file. Chris@0: */ Chris@0: protected function processFile($css_asset) { Chris@0: $contents = $this->loadFile($css_asset['data'], TRUE); Chris@0: Chris@0: $contents = $this->clean($contents); Chris@0: Chris@0: // Get the parent directory of this file, relative to the Drupal root. Chris@0: $css_base_path = substr($css_asset['data'], 0, strrpos($css_asset['data'], '/')); Chris@0: // Store base path. Chris@0: $this->rewriteFileURIBasePath = $css_base_path . '/'; Chris@0: Chris@0: // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths. Chris@0: return preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', [$this, 'rewriteFileURI'], $contents); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads the stylesheet and resolves all @import commands. Chris@0: * Chris@0: * Loads a stylesheet and replaces @import commands with the contents of the Chris@0: * imported file. Use this instead of file_get_contents when processing Chris@0: * stylesheets. Chris@0: * Chris@0: * The returned contents are compressed removing white space and comments only Chris@0: * when CSS aggregation is enabled. This optimization will not apply for Chris@0: * color.module enabled themes with CSS aggregation turned off. Chris@0: * Chris@0: * Note: the only reason this method is public is so color.module can call it; Chris@0: * it is not on the AssetOptimizerInterface, so future refactorings can make Chris@0: * it protected. Chris@0: * Chris@0: * @param $file Chris@0: * Name of the stylesheet to be processed. Chris@0: * @param $optimize Chris@0: * Defines if CSS contents should be compressed or not. Chris@0: * @param $reset_basepath Chris@0: * Used internally to facilitate recursive resolution of @import commands. Chris@0: * Chris@0: * @return Chris@0: * Contents of the stylesheet, including any resolved @import commands. Chris@0: */ Chris@0: public function loadFile($file, $optimize = NULL, $reset_basepath = TRUE) { Chris@0: // These statics are not cache variables, so we don't use drupal_static(). Chris@0: static $_optimize, $basepath; Chris@0: if ($reset_basepath) { Chris@0: $basepath = ''; Chris@0: } Chris@0: // Store the value of $optimize for preg_replace_callback with nested Chris@0: // @import loops. Chris@0: if (isset($optimize)) { Chris@0: $_optimize = $optimize; Chris@0: } Chris@0: Chris@0: // Stylesheets are relative one to each other. Start by adding a base path Chris@0: // prefix provided by the parent stylesheet (if necessary). Chris@0: if ($basepath && !file_uri_scheme($file)) { Chris@0: $file = $basepath . '/' . $file; Chris@0: } Chris@0: // Store the parent base path to restore it later. Chris@0: $parent_base_path = $basepath; Chris@0: // Set the current base path to process possible child imports. Chris@0: $basepath = dirname($file); Chris@0: Chris@0: // Load the CSS stylesheet. We suppress errors because themes may specify Chris@0: // stylesheets in their .info.yml file that don't exist in the theme's path, Chris@0: // but are merely there to disable certain module CSS files. Chris@0: $content = ''; Chris@0: if ($contents = @file_get_contents($file)) { Chris@0: // If a BOM is found, convert the file to UTF-8, then use substr() to Chris@0: // remove the BOM from the result. Chris@0: if ($encoding = (Unicode::encodingFromBOM($contents))) { Chris@17: $contents = mb_substr(Unicode::convertToUtf8($contents, $encoding), 1); Chris@0: } Chris@0: // If no BOM, check for fallback encoding. Per CSS spec the regex is very strict. Chris@0: elseif (preg_match('/^@charset "([^"]+)";/', $contents, $matches)) { Chris@0: if ($matches[1] !== 'utf-8' && $matches[1] !== 'UTF-8') { Chris@0: $contents = substr($contents, strlen($matches[0])); Chris@0: $contents = Unicode::convertToUtf8($contents, $matches[1]); Chris@0: } Chris@0: } Chris@0: Chris@0: // Return the processed stylesheet. Chris@0: $content = $this->processCss($contents, $_optimize); Chris@0: } Chris@0: Chris@0: // Restore the parent base path as the file and its children are processed. Chris@0: $basepath = $parent_base_path; Chris@0: return $content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads stylesheets recursively and returns contents with corrected paths. Chris@0: * Chris@0: * This function is used for recursive loading of stylesheets and Chris@0: * returns the stylesheet content with all url() paths corrected. Chris@0: * Chris@0: * @param array $matches Chris@0: * An array of matches by a preg_replace_callback() call that scans for Chris@0: * @import-ed CSS files, except for external CSS files. Chris@0: * Chris@0: * @return Chris@0: * The contents of the CSS file at $matches[1], with corrected paths. Chris@0: * Chris@0: * @see \Drupal\Core\Asset\AssetOptimizerInterface::loadFile() Chris@0: */ Chris@0: protected function loadNestedFile($matches) { Chris@0: $filename = $matches[1]; Chris@0: // Load the imported stylesheet and replace @import commands in there as Chris@0: // well. Chris@0: $file = $this->loadFile($filename, NULL, FALSE); Chris@0: Chris@0: // Determine the file's directory. Chris@0: $directory = dirname($filename); Chris@0: // If the file is in the current directory, make sure '.' doesn't appear in Chris@0: // the url() path. Chris@0: $directory = $directory == '.' ? '' : $directory . '/'; Chris@0: Chris@0: // Alter all internal url() paths. Leave external paths alone. We don't need Chris@0: // to normalize absolute paths here because that will be done later. Chris@0: return preg_replace('/url\(\s*([\'"]?)(?![a-z]+:|\/+)([^\'")]+)([\'"]?)\s*\)/i', 'url(\1' . $directory . '\2\3)', $file); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Processes the contents of a stylesheet for aggregation. Chris@0: * Chris@0: * @param $contents Chris@0: * The contents of the stylesheet. Chris@0: * @param $optimize Chris@0: * (optional) Boolean whether CSS contents should be minified. Defaults to Chris@0: * FALSE. Chris@0: * Chris@0: * @return Chris@0: * Contents of the stylesheet including the imported stylesheets. Chris@0: */ Chris@0: protected function processCss($contents, $optimize = FALSE) { Chris@0: // Remove unwanted CSS code that cause issues. Chris@0: $contents = $this->clean($contents); Chris@0: Chris@0: if ($optimize) { Chris@0: // Perform some safe CSS optimizations. Chris@0: // Regexp to match comment blocks. Chris@17: $comment = '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/'; Chris@0: // Regexp to match double quoted strings. Chris@0: $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"'; Chris@0: // Regexp to match single quoted strings. Chris@0: $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'"; Chris@0: // Strip all comment blocks, but keep double/single quoted strings. Chris@0: $contents = preg_replace( Chris@0: "<($double_quot|$single_quot)|$comment>Ss", Chris@0: "$1", Chris@0: $contents Chris@0: ); Chris@0: // Remove certain whitespace. Chris@0: // There are different conditions for removing leading and trailing Chris@0: // whitespace. Chris@0: // @see http://php.net/manual/regexp.reference.subpatterns.php Chris@0: $contents = preg_replace('< Chris@0: # Do not strip any space from within single or double quotes Chris@0: (' . $double_quot . '|' . $single_quot . ') Chris@0: # Strip leading and trailing whitespace. Chris@0: | \s*([@{};,])\s* Chris@0: # Strip only leading whitespace from: Chris@0: # - Closing parenthesis: Retain "@media (bar) and foo". Chris@0: | \s+([\)]) Chris@0: # Strip only trailing whitespace from: Chris@0: # - Opening parenthesis: Retain "@media (bar) and foo". Chris@0: # - Colon: Retain :pseudo-selectors. Chris@0: | ([\(:])\s+ Chris@0: >xSs', Chris@0: // Only one of the four capturing groups will match, so its reference Chris@0: // will contain the wanted value and the references for the Chris@0: // two non-matching groups will be replaced with empty strings. Chris@0: '$1$2$3$4', Chris@0: $contents Chris@0: ); Chris@0: // End the file with a new line. Chris@0: $contents = trim($contents); Chris@0: $contents .= "\n"; Chris@0: } Chris@0: Chris@0: // Replaces @import commands with the actual stylesheet content. Chris@0: // This happens recursively but omits external files. Chris@0: $contents = preg_replace_callback('/@import\s*(?:url\(\s*)?[\'"]?(?![a-z]+:)(?!\/\/)([^\'"\()]+)[\'"]?\s*\)?\s*;/', [$this, 'loadNestedFile'], $contents); Chris@0: Chris@0: return $contents; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prefixes all paths within a CSS file for processFile(). Chris@0: * Chris@0: * Note: the only reason this method is public is so color.module can call it; Chris@0: * it is not on the AssetOptimizerInterface, so future refactorings can make Chris@0: * it protected. Chris@0: * Chris@0: * @param array $matches Chris@0: * An array of matches by a preg_replace_callback() call that scans for Chris@0: * url() references in CSS files, except for external or absolute ones. Chris@0: * Chris@0: * @return string Chris@0: * The file path. Chris@0: */ Chris@0: public function rewriteFileURI($matches) { Chris@0: // Prefix with base and remove '../' segments where possible. Chris@0: $path = $this->rewriteFileURIBasePath . $matches[1]; Chris@0: $last = ''; Chris@0: while ($path != $last) { Chris@0: $last = $path; Chris@0: $path = preg_replace('`(^|/)(?!\.\./)([^/]+)/\.\./`', '$1', $path); Chris@0: } Chris@0: return 'url(' . file_url_transform_relative(file_create_url($path)) . ')'; Chris@0: } Chris@0: Chris@0: }