Permalink
Browse files

Merge latest mnify changes mantaining the twitter bootstrap ui

  • Loading branch information...
1 parent 44e3258 commit 21aaf49838bc0afba9840b996c3b1d7af98475b1 @acidvertigo committed Dec 14, 2011
View
@@ -290,8 +290,7 @@ public static function serve($controller, $options = array())
} else {
// generate & cache content
try {
- require_once dirname(__FILE__) . '/Minify/Processor.php';
- $content = Minify_Processor::process($controller, self::$_options, self::$importWarning);
+ $content = self::_combineMinify();
} catch (Exception $e) {
self::$_controller->log($e->getMessage());
if (! self::$_options['quiet']) {
@@ -308,8 +307,7 @@ public static function serve($controller, $options = array())
// no cache
$cacheIsReady = false;
try {
- require_once dirname(__FILE__) . '/Minify/Processor.php';
- $content = Minify_Processor::process($controller, self::$_options, self::$importWarning);
+ $content = self::_combineMinify();
} catch (Exception $e) {
self::$_controller->log($e->getMessage());
if (! self::$_options['quiet']) {
@@ -458,6 +456,103 @@ protected static function _setupDebug($sources)
}
/**
+ * Combines sources and minifies the result.
+ *
+ * @return string
+ */
+ protected static function _combineMinify()
+ {
+ $type = self::$_options['contentType']; // ease readability
+
+ // when combining scripts, make sure all statements separated and
+ // trailing single line comment is terminated
+ $implodeSeparator = ($type === self::TYPE_JS)
+ ? "\n;"
+ : '';
+ // allow the user to pass a particular array of options to each
+ // minifier (designated by type). source objects may still override
+ // these
+ $defaultOptions = isset(self::$_options['minifierOptions'][$type])
+ ? self::$_options['minifierOptions'][$type]
+ : array();
+ // if minifier not set, default is no minification. source objects
+ // may still override this
+ $defaultMinifier = isset(self::$_options['minifiers'][$type])
+ ? self::$_options['minifiers'][$type]
+ : false;
+
+ // process groups of sources with identical minifiers/options
+ $content = array();
+ $i = 0;
+ $l = count(self::$_controller->sources);
+ $groupToProcessTogether = array();
+ $lastMinifier = null;
+ $lastOptions = null;
+ do {
+ // get next source
+ $source = null;
+ if ($i < $l) {
+ $source = self::$_controller->sources[$i];
+ /* @var Minify_Source $source */
+ $sourceContent = $source->getContent();
+
+ // allow the source to override our minifier and options
+ $minifier = (null !== $source->minifier)
+ ? $source->minifier
+ : $defaultMinifier;
+ $options = (null !== $source->minifyOptions)
+ ? array_merge($defaultOptions, $source->minifyOptions)
+ : $defaultOptions;
+ }
+ // do we need to process our group right now?
+ if ($i > 0 // yes, we have at least the first group populated
+ && (
+ ! $source // yes, we ran out of sources
+ || $type === self::TYPE_CSS // yes, to process CSS individually (avoiding PCRE bugs/limits)
+ || $minifier !== $lastMinifier // yes, minifier changed
+ || $options !== $lastOptions) // yes, options changed
+ )
+ {
+ // minify previous sources with last settings
+ $imploded = implode($implodeSeparator, $groupToProcessTogether);
+ $groupToProcessTogether = array();
+ if ($lastMinifier) {
+ self::$_controller->loadMinifier($lastMinifier);
+ try {
+ $content[] = call_user_func($lastMinifier, $imploded, $lastOptions);
+ } catch (Exception $e) {
+ throw new Exception("Exception in minifier: " . $e->getMessage());
+ }
+ } else {
+ $content[] = $imploded;
+ }
+ }
+ // add content to the group
+ if ($source) {
+ $groupToProcessTogether[] = $sourceContent;
+ $lastMinifier = $minifier;
+ $lastOptions = $options;
+ }
+ $i++;
+ } while ($source);
+
+ $content = implode($implodeSeparator, $content);
+
+ if ($type === self::TYPE_CSS && false !== strpos($content, '@import')) {
+ $content = self::_handleCssImports($content);
+ }
+
+ // do any post-processing (esp. for editing build URIs)
+ if (self::$_options['postprocessorRequire']) {
+ require_once self::$_options['postprocessorRequire'];
+ }
+ if (self::$_options['postprocessor']) {
+ $content = call_user_func(self::$_options['postprocessor'], $content, $type);
+ }
+ return $content;
+ }
+
+ /**
* Make a unique cache id for for this request.
*
* Any settings that could affect output are taken into consideration
@@ -480,4 +575,30 @@ protected static function _getCacheId($prefix = 'minify')
)));
return "{$prefix}_{$name}_{$md5}";
}
+
+ /**
+ * Bubble CSS @imports to the top or prepend a warning if an
+ * @import is detected not at the top.
+ */
+ protected static function _handleCssImports($css)
+ {
+ if (self::$_options['bubbleCssImports']) {
+ // bubble CSS imports
+ preg_match_all('/@import.*?;/', $css, $imports);
+ $css = implode('', $imports[0]) . preg_replace('/@import.*?;/', '', $css);
+ } else if ('' !== self::$importWarning) {
+ // remove comments so we don't mistake { in a comment as a block
+ $noCommentCss = preg_replace('@/\\*[\\s\\S]*?\\*/@', '', $css);
+ $lastImportPos = strrpos($noCommentCss, '@import');
+ $firstBlockPos = strpos($noCommentCss, '{');
+ if (false !== $lastImportPos
+ && false !== $firstBlockPos
+ && $firstBlockPos < $lastImportPos
+ ) {
+ // { appears before @import : prepend warning
+ $css = self::$importWarning . $css;
+ }
+ }
+ return $css;
+ }
}
@@ -45,7 +45,8 @@ public function setupSources($options) {
$this->log("Duplicate group key found.");
return $options;
}
- foreach (explode(',', $_GET['g']) as $key) {
+ $keys = explode(',', $_GET['g']);
+ foreach ($keys as $key) {
if (! isset($cOptions['groups'][$key])) {
$this->log("A group configuration for \"{$key}\" was not found");
return $options;
@@ -30,15 +30,6 @@ public static function minify($js, array $options = array())
}
/**
- * Get maximum # of bytes the minify function can handle (0 = no limit)
- * @return int
- */
- public static function getMaxBytes()
- {
- return 200000;
- }
-
- /**
*
* @param array $options
*
@@ -1,21 +1,21 @@
/*
* YUI Compressor
+ * http://developer.yahoo.com/yui/compressor/
* Author: Julien Lecomte - http://www.julienlecomte.net/
- * Author: Isaac Schlueter - http://foohack.com/
+ * Author: Isaac Schlueter - http://foohack.com/
* Author: Stoyan Stefanov - http://phpied.com/
- * Copyright (c) 2009 Yahoo! Inc. All rights reserved.
+ * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
* The copyrights embodied in the content of this file are licensed
* by Yahoo! Inc. under the BSD (revised) open source license.
*/
-
package com.yahoo.platform.yui.compressor;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
-import java.util.ArrayList;
+import java.util.ArrayList;
public class CssCompressor {
@@ -36,7 +36,7 @@ public void compress(Writer out, int linebreakpos)
Matcher m;
String css = srcsb.toString();
StringBuffer sb = new StringBuffer(css);
-
+
int startIndex = 0;
int endIndex = 0;
int i = 0;
@@ -47,14 +47,27 @@ public void compress(Writer out, int linebreakpos)
int totallen = css.length();
String placeholder;
+ // // leave data urls alone to increase parse performance.
+ // sb = new StringBuffer();
+ // p = Pattern.compile("url\\(.*data\\:(.*)\\)");
+ // m = p.matcher(css);
+ // while (m.find()) {
+ // token = m.group();
+ // token = token.substring(1, token.length() - 1);
+ // preservedTokens.add(token);
+ // String preserver = "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___";
+ // m.appendReplacement(sb, preserver);
+ // }
+ // m.appendTail(sb);
+ // css = sb.toString();
// collect all comment blocks...
while ((startIndex = sb.indexOf("/*", startIndex)) >= 0) {
endIndex = sb.indexOf("*/", startIndex + 2);
if (endIndex < 0) {
endIndex = totallen;
}
-
+
token = sb.substring(startIndex + 2, endIndex);
comments.add(token);
sb.replace(startIndex + 2, endIndex, "___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + (comments.size() - 1) + "___");
@@ -70,18 +83,18 @@ public void compress(Writer out, int linebreakpos)
token = m.group();
char quote = token.charAt(0);
token = token.substring(1, token.length() - 1);
-
+
// maybe the string contains a comment-like substring?
// one, maybe more? put'em back then
if (token.indexOf("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_") >= 0) {
for (i = 0, max = comments.size(); i < max; i += 1) {
token = token.replace("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", comments.get(i).toString());
}
}
-
+
// minify alpha opacity in filter strings
token = token.replaceAll("(?i)progid:DXImageTransform.Microsoft.Alpha\\(Opacity=", "alpha(opacity=");
-
+
preservedTokens.add(token);
String preserver = quote + "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___" + quote;
m.appendReplacement(sb, preserver);
@@ -95,26 +108,26 @@ public void compress(Writer out, int linebreakpos)
token = comments.get(i).toString();
placeholder = "___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___";
-
+
// ! in the first position of the comment means preserve
// so push to the preserved tokens while stripping the !
if (token.startsWith("!")) {
preservedTokens.add(token);
css = css.replace(placeholder, "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
continue;
}
-
+
// \ in the last position looks like hack for Mac/IE5
// shorten that to /*\*/ and the next one to /**/
if (token.endsWith("\\")) {
preservedTokens.add("\\");
css = css.replace(placeholder, "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
i = i + 1; // attn: advancing the loop
preservedTokens.add("");
- css = css.replace("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
+ css = css.replace("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
continue;
}
-
+
// keep empty comments after child selectors (IE7 hack)
// e.g. html >/**/ body
if (token.length() == 0) {
@@ -126,7 +139,7 @@ public void compress(Writer out, int linebreakpos)
}
}
}
-
+
// in all other cases kill the comment
css = css.replace("/*" + placeholder + "*/", "");
}
@@ -153,20 +166,20 @@ public void compress(Writer out, int linebreakpos)
css = css.replaceAll("\\s+([!{};:>+\\(\\)\\],])", "$1");
// bring back the colon
css = css.replaceAll("___YUICSSMIN_PSEUDOCLASSCOLON___", ":");
-
+
// retain space for special IE6 cases
css = css.replaceAll(":first\\-(line|letter)(\\{|,)", ":first-$1 $2");
-
+
// no space after the end of a preserved comment
- css = css.replaceAll("\\*/ ", "*/");
-
+ css = css.replaceAll("\\*/ ", "*/");
+
// If there is a @charset, then only allow one, and push to the top of the file.
css = css.replaceAll("^(.*)(@charset \"[^\"]*\";)", "$2$1");
css = css.replaceAll("^(\\s*@charset [^;]+;\\s*)+", "$1");
-
+
// Put the space back in some cases, to support stuff like
// @media screen and (-webkit-min-device-pixel-ratio:0){
- css = css.replaceAll("\\band\\(", "and (");
+ css = css.replaceAll("\\band\\(", "and (");
// Remove the spaces after the things that should not have spaces after them.
css = css.replaceAll("([!{}:;>+\\(\\[,])\\s+", "$1");
@@ -181,8 +194,8 @@ public void compress(Writer out, int linebreakpos)
css = css.replaceAll(":0 0 0 0(;|})", ":0$1");
css = css.replaceAll(":0 0 0(;|})", ":0$1");
css = css.replaceAll(":0 0(;|})", ":0$1");
-
-
+
+
// Replace background-position:0; with background-position:0 0;
// same for transform-origin
sb = new StringBuffer();
@@ -193,7 +206,7 @@ public void compress(Writer out, int linebreakpos)
}
m.appendTail(sb);
css = sb.toString();
-
+
// Replace 0.6 to .6, but only when preceded by : or a white-space
css = css.replaceAll("(:|\\s)0+\\.(\\d+)", "$1.$2");
@@ -227,12 +240,17 @@ public void compress(Writer out, int linebreakpos)
m = p.matcher(css);
sb = new StringBuffer();
while (m.find()) {
- // Test for AABBCC pattern
- if (m.group(3).equalsIgnoreCase(m.group(4)) &&
+ if (m.group(1).equals("}")) {
+ // Likely an ID selector. Don't touch.
+ // #AABBCC is a valid ID. IDs are case-sensitive.
+ m.appendReplacement(sb, m.group());
+ } else if (m.group(3).equalsIgnoreCase(m.group(4)) &&
m.group(5).equalsIgnoreCase(m.group(6)) &&
m.group(7).equalsIgnoreCase(m.group(8))) {
+ // #AABBCC pattern
m.appendReplacement(sb, (m.group(1) + m.group(2) + "#" + m.group(3) + m.group(5) + m.group(7)).toLowerCase());
} else {
+ // Any other color.
m.appendReplacement(sb, m.group().toLowerCase());
}
}
@@ -288,4 +306,4 @@ public void compress(Writer out, int linebreakpos)
// Write the output...
out.write(css);
}
-}
+}
@@ -15,6 +15,8 @@
/**
* Compress CSS (incomplete DO NOT USE)
+ *
+ * @see https://github.com/yui/yuicompressor/blob/master/src/com/yahoo/platform/yui/compressor/CssCompressor.java
*
* @package Minify
*/
@@ -0,0 +1,3 @@
+/* @import url('bad.css' ) bad; */
+adjacent2 foo { background: red url(/red.gif); }
+adjacent2 bar { background: url('../green.gif') }
Oops, something went wrong.

0 comments on commit 21aaf49

Please sign in to comment.