Permalink
Browse files

preserve ! in special strings, preserve child >/**/ selector hack, up…

…dated tests
  • Loading branch information...
1 parent 336986b commit e7ea458ae009f02a1b90a405a23f220e12d2a483 @stoyan stoyan committed May 19, 2010
View
@@ -25,7 +25,8 @@ YAHOO.compressor.cssmin = function (css, linebreakpos) {
preservedTokens = [],
comments = [],
token = '',
- totallen = css.length;
+ totallen = css.length,
+ placeholder = '';
// collect all comment blocks...
while ((startIndex = css.indexOf("/*", startIndex)) >= 0) {
@@ -63,25 +64,42 @@ YAHOO.compressor.cssmin = function (css, linebreakpos) {
// strings are safe, now wrestle the comments
for (i = 0, max = comments.length; i < max; i = i + 1) {
+ token = comments[i];
+ 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 (comments[i].charAt(0) === "!") {
- preservedTokens.push(comments[i].slice(1));
- css = css.replace("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___");
+ // so push to the preserved tokens keeping the !
+ if (token.charAt(0) === "!") {
+ preservedTokens.push(token);
+ css = css.replace(placeholder, "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___");
continue;
}
+
// \ in the last position looks like hack for Mac/IE5
// shorten that to /*\*/ and the next one to /**/
- if (comments[i].charAt(comments[i].length - 1) === "\\") {
+ if (token.charAt(token.length - 1) === "\\") {
preservedTokens.push("\\");
- css = css.replace("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___");
+ css = css.replace(placeholder, "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___");
i = i + 1; // attn: advancing the loop
preservedTokens.push("");
css = css.replace("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___");
continue;
}
+
+ // keep empty comments after child selectors (IE7 hack)
+ // e.g. html >/**/ body
+ if (token.length === 0) {
+ startIndex = css.indexOf(placeholder);
+ if (startIndex > 2) {
+ if (css.charAt(startIndex - 3) === '>') {
+ preservedTokens.push("");
+ css = css.replace(placeholder, "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.length - 1) + "___");
+ }
+ }
+ }
+
// in all other cases kill the comment
- css = css.replace("/*___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___*/", "");
+ css = css.replace("/*" + placeholder + "*/", "");
}
@@ -45,6 +45,7 @@ public void compress(Writer out, int linebreakpos)
ArrayList comments = new ArrayList(0);
String token;
int totallen = css.length();
+ String placeholder;
// collect all comment blocks...
@@ -93,26 +94,41 @@ public void compress(Writer out, int linebreakpos)
for (i = 0, max = comments.size(); i < max; i += 1) {
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.substring(1));
- css = css.replace("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
+ 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("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
+ 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) + "___");
continue;
}
+
+ // keep empty comments after child selectors (IE7 hack)
+ // e.g. html >/**/ body
+ if (token.length() == 0) {
+ startIndex = css.indexOf(placeholder);
+ if (startIndex > 2) {
+ if (css.charAt(startIndex - 3) == '>') {
+ preservedTokens.add("");
+ css = css.replace(placeholder, "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
+ }
+ }
+ }
+
// in all other cases kill the comment
- css = css.replace("/*___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___*/", "");
+ css = css.replace("/*" + placeholder + "*/", "");
}
View
@@ -1 +1 @@
-/* $LastChangedRevision: 81 $ $LastChangedDate: 2009-05-27 17:41:02 +0100 (Wed, 27 May 2009) $ */body{yo:cats}
+/*! $LastChangedRevision: 81 $ $LastChangedDate: 2009-05-27 17:41:02 +0100 (Wed, 27 May 2009) $ */body{yo:cats}
View
@@ -1 +1 @@
-@media screen and/*YUI-Compresser */(-webkit-min-device-pixel-ratio:0){a{b:1}}@media screen and/* *//* */(-webkit-min-device-pixel-ratio:0){a{b:1}}@media -webkit-min-device-pixel-ratio:0{a{b:1}}
+@media screen and/*!YUI-Compresser */(-webkit-min-device-pixel-ratio:0){a{b:1}}@media screen and/*! *//*! */(-webkit-min-device-pixel-ratio:0){a{b:1}}@media -webkit-min-device-pixel-ratio:0{a{b:1}}
View
@@ -1 +1 @@
-/* special */
+/*! special */
View
@@ -1,3 +1,3 @@
-html >/*!*/ body p {
+html >/**/ body p {
color: blue;
}
@@ -1,3 +1,3 @@
-/*
+/*!
$Header: /temp/dirname/filename.css 3 2/02/08 3:37p JSmith $
*/foo{bar:baz}
@@ -1 +1 @@
-/* preserved */@import "another.css";@media print{.noprint{display:none}}@media screen{.printonly{display:none}}
+/*! preserved */@import "another.css";@media print{.noprint{display:none}}@media screen{.printonly{display:none}}
@@ -2,12 +2,12 @@
Preserving comments
as they are
********************
- Except for the initial !
+ Keep the initial !
*******************/
#yo {
ma: "ma";
}
-/*!
+/*!
I said
pre-
serve! */
@@ -1,9 +1,9 @@
-/*************88****
+/*!************88****
Preserving comments
as they are
********************
- Except for the initial !
- *******************/#yo{ma:"ma"}/*
+ Keep the initial !
+ *******************/#yo{ma:"ma"}/*!
I said
pre-
serve! */
@@ -1 +1 @@
-a{a:1}/*"preserve" me*/b{content:"/**/"}/*\*/c{c:3}/**/
+a{a:1}/*!"preserve" me*/b{content:"/**/"}/*\*/c{c:3}/**/

0 comments on commit e7ea458

Please sign in to comment.