Skip to content

Commit c2529bd

Browse files
author
zhourenjian
committed
Fix bug of *.js compressing
1 parent fd105a9 commit c2529bd

File tree

1 file changed

+62
-18
lines changed

1 file changed

+62
-18
lines changed

src/net/sf/j2s/core/compiler/RegExCompress.java

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public static String mk(String str, String regex, String replacement) {
2929
StringBuffer sb = new StringBuffer();
3030
do {
3131
if (matcher.group(10) != null) {
32-
matcher.appendReplacement(sb, "$10 $11");
32+
matcher.appendReplacement(sb, "$11 $12");
3333
} else if (matcher.group(13) != null) {
34-
matcher.appendReplacement(sb, "$13 $14");
34+
matcher.appendReplacement(sb, "$14 $15");
3535
} else {
3636
matcher.appendReplacement(sb, replacement);
3737
}
@@ -43,34 +43,78 @@ public static String mk(String str, String regex, String replacement) {
4343
return str;
4444
}
4545
public static String regexCompress(String str) {
46+
str = str.replaceAll("(\r([^\n]))|(([^\r])\n)", "$4\r\n$2"); // fix line terminators
47+
boolean ignoreCSS = false;
48+
String cssCodes = null;
49+
int idx1 = str.indexOf("$WTC$$.registerCSS");
50+
int idx2 = -1;
51+
String specialFunKey = "@324@();\r\n";
52+
if (idx1 != -1) {
53+
idx2 = str.indexOf("\");\r\n", idx1);
54+
if (idx2 != -1) {
55+
ignoreCSS = true;
56+
cssCodes = str.substring(idx1, idx2 + 5);
57+
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2 + 5);
58+
}
59+
}
4660
String regEx = "('[^\\n\\r]*[^\\\\]')|" + // 1:1
47-
"(\"[^\\n\\r]*[^\\\\]\")|" + // 1:2
48-
"(\\/\\/[^\\n\\r]*$)|" + // 1:3 // line comments
49-
"(\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\/)|" + // 2:4,5 // block comments
50-
"(\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*))|" + // 2:6,7 // regular expression
51-
"([^\\w\\x24\\/'\"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*)"; // 1:8 // regular expression
61+
"(\"([^\\n\\r\\\"]|\\\\\\\")*[^\\\\]\")|" + // 1:3
62+
"(\\/\\/[^\\n\\r]*[\\n\\r])|" + // 1:4
63+
"(\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\/)|" + // 2:5,6
64+
"(\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*))|" + // 2:7,8
65+
"([^\\w\\x24\\/'\"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*)"; // 1:9
5266
//str = str.replaceAll(regEx, "$1$2$7$8");
5367
str = mk(str, regEx +
54-
"|((\\b|\\x24)\\s+(\\b|\\x24))|" + // 3:9,10,11
55-
"(([+\\-])\\s+([+\\-]))|" + // 3:12,13,14
68+
"|((\\b|\\x24)\\s+(\\b|\\x24))|" + // 3:10,11,12
69+
"(([+\\-])\\s+([+\\-]))|" + // 3:13,14,15
5670
"(\\s+)",
57-
"$1$2$7$8");
71+
"$1$2$8$9");
72+
if (ignoreCSS) {
73+
int idx = str.indexOf(specialFunKey);
74+
if (idx != -1) {
75+
str = str.substring(0, idx) + cssCodes + str.substring(idx + specialFunKey.length());
76+
} else {
77+
System.err.println("Error! Fail to ignore CSS codes!");
78+
}
79+
}
5880
return str;
5981
}
6082
public static String regexCompress2(String str) {
83+
str = str.replaceAll("(\r([^\n]))|(([^\r])\n)", "$4\r\n$2"); // fix line terminators
84+
boolean ignoreCSS = false;
85+
String cssCodes = null;
86+
int idx1 = str.indexOf("$WTC$$.registerCSS");
87+
int idx2 = -1;
88+
String specialFunKey = "@324@();\r\n";
89+
if (idx1 != -1) {
90+
idx2 = str.indexOf("\");\r\n", idx1);
91+
if (idx2 != -1) {
92+
ignoreCSS = true;
93+
cssCodes = str.substring(idx1, idx2 + 5);
94+
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2 + 5);
95+
}
96+
}
6197
String whiteSpace = "[ \\f\\t\\v]";
6298
String regEx = "('[^\\n\\r]*[^\\\\]')|" + // 1:1
63-
"(\"[^\\n\\r]*[^\\\\]\")|" + // 1:2
64-
"(\\/\\/[^\\n\\r]*$)|" + // 1:3 // line comments
65-
"(\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\/)|" + // 2:4,5 // block comments
66-
"(" + whiteSpace + "+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*))|" + // 2:6,7 // regular expression
67-
"([^\\w\\x24\\/'\"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*)"; // 1:8 // regular expression
99+
"(\"([^\\n\\r\\\"]|\\\\\\\")*[^\\\\]\")|" + // 2:2,3
100+
"(\\/\\/[^\\n\\r]*[\\n\\r])|" + // 1:4 // line comments
101+
"(\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\/)|" + // 2:5,6 // block comments
102+
"(" + whiteSpace + "+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*))|" + // 2:7,8 // regular expression
103+
"([^\\w\\x24\\/'\"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*)"; // 1:9 // regular expression
68104
//str = str.replaceAll(regEx, "$1$2$7$8");
69105
str = mk(str, regEx +
70-
"|((\\b|\\x24)" + whiteSpace + "+(\\b|\\x24))|" + // 3:9,10,11
71-
"(([+\\-])" + whiteSpace + "+([+\\-]))|" + // 3:12,13,14
106+
"|((\\b|\\x24)" + whiteSpace + "+(\\b|\\x24))|" + // 3:10,11,12
107+
"(([+\\-])" + whiteSpace + "+([+\\-]))|" + // 3:13,14,15
72108
"(" + whiteSpace + "+)",
73-
"$1$2$7$8");
109+
"$1$2$8$9");
110+
if (ignoreCSS) {
111+
int idx = str.indexOf(specialFunKey);
112+
if (idx != -1) {
113+
str = str.substring(0, idx) + cssCodes + str.substring(idx + specialFunKey.length());
114+
} else {
115+
System.err.println("Error! Fail to ignore CSS codes!");
116+
}
117+
}
74118
return str;
75119
}
76120

0 commit comments

Comments
 (0)