Index: t3lib/class.t3lib_compressor.php =================================================================== --- t3lib/class.t3lib_compressor.php (revision 7483) +++ t3lib/class.t3lib_compressor.php (working copy) @@ -105,12 +105,13 @@ $targetFile = $this->targetDirectory . TYPO3_MODE . '-'. md5($unique) . '.css'; // if the file doesn't already exist, we create it - if (!file_exists($targetFile)) { + if (!file_exists(PATH_site . $targetFile)) { $concatenated = ''; // concatenate all the files together foreach ($filesToInclude as $filename) { - $contents = file_get_contents($GLOBALS['BACK_PATH'] . $filename); - $concatenated .= $this->cssFixRelativeUrlPaths($contents, dirname($filename) . '/', $this->targetDirectory); + $compressedFilename = $this->compressCssFile($filename); + $contents = file_get_contents(PATH_site . $compressedFilename); + $concatenated .= $this->cssFixRelativeUrlPaths($contents, dirname($filename) . '/'); } if (strlen($concatenated)) { @@ -121,6 +122,82 @@ } /** + * Compresses a CSS file + * + * removes comments and whitespaces + * Adopted from http://drupal.org/files/issues/minify_css.php__1.txt + * + * @param string $filename Source filename + * @return string Filename of the compressed file + */ + public function compressCssFile($filename) { + // generate the unique name of the file + $filenameFromMainDir = substr($filename, strlen($GLOBALS['BACK_PATH'])); + $unique = $filenameFromMainDir . filemtime($filename) . filesize($filename); + $targetFile = $this->targetDirectory . TYPO3_MODE . '-compressed-' . md5($unique) . '.css'; + // only create it, if it doesn't exist, yet + if (!file_exists(PATH_site . $targetFile)) { + $contents = file_get_contents($GLOBALS['BACK_PATH'] . $filename); + // Perform some safe CSS optimizations. + $contents = str_replace("\r", '', $contents); // Strip any and all carriage returns. + // Match and process strings, comments and everything else, one chunk at a time. + // To understand this regex, read: "Mastering Regular Expressions 3rd Edition" chapter 6. + $contents = preg_replace_callback('% + # One-regex-to-rule-them-all! - version: 20100220_0100 + # Group 1: Match a double quoted string. + ("[^"\\\\]*+(?:\\\\.[^"\\\\]*+)*+") | # or... + # Group 2: Match a single quoted string. + (\'[^\'\\\\]*+(?:\\\\.[^\'\\\\]*+)*+\') | # or... + # Group 3: Match a regular non-MacIE5-hack comment. + (/\*[^\\\\*]*+\*++(?:[^\\\\*/][^\\\\*]*+\*++)*+/) | # or... + # Group 4: Match a MacIE5-type1 comment. + (/\*(?:[^*\\\\]*+\**+(?!/))*+\\\\[^*]*+\*++(?:[^*/][^*]*+\*++)*+/(?{};,)])/S', '$1', $matches[6]); // Clean pre-punctuation. + $matches[6] = preg_replace('/([+>{}:;,(])\s++/S', '$1', $matches[6]); // Clean post-punctuation. + $matches[6] = preg_replace('/;?\}/S', "}\n", $matches[6]); // Add a touch of formatting. + return "\n/*T2\\*/" . $matches[6] . "\n/*T2E*/\n"; // Minify and reassemble composite type2 comment. + } elseif (isset($matches[8])) { // Group 8: Non-string, non-comment. Safe to clean whitespace here. + $matches[8] = preg_replace('/^\s++/', '', $matches[8]); // Strip all leading whitespace. + $matches[8] = preg_replace('/\s++$/', '', $matches[8]); // Strip all trailing whitespace. + $matches[8] = preg_replace('/\s{2,}+/', ' ', $matches[8]); // Consolidate multiple whitespace. + $matches[8] = preg_replace('/\s++([+>{};,)])/S', '$1', $matches[8]); // Clean pre-punctuation. + $matches[8] = preg_replace('/([+>{}:;,(])\s++/S', '$1', $matches[8]); // Clean post-punctuation. + $matches[8] = preg_replace('/;?\}/S', "}\n", $matches[8]); // Add a touch of formatting. + return $matches[8]; + } + return $matches[0] . "\n/* ERROR! Unexpected _proccess_css_minify() parameter */\n"; // never get here + } + + /** * Decides whether a CSS file comes from one of the baseDirectories * * @param string $filename Filename @@ -141,10 +218,9 @@ * * @param string $contents Data to process * @param string $oldDir Directory of the originial file, relative to TYPO3_mainDir - * @param string $newDir Directory of the resulting file * @return string Processed data */ - protected function cssFixRelativeUrlPaths($contents, $oldDir, $newDir) { + protected function cssFixRelativeUrlPaths($contents, $oldDir) { $matches = array(); preg_match_all('/url[\s]*\([\'\"]?(.*)[\'\"]?\)/iU', $contents, $matches); foreach ($matches[1] as $match) {