Skip to content

Commit d1266a5

Browse files
committed
Merge branch 'master' into preserveHints
2 parents 590763c + d6f2f7b commit d1266a5

18 files changed

+85
-27
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ CVS/
1111
build/classes
1212
build/jar
1313
build/yuicompressor*.jar
14+
.project
15+
.classpath

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ TODO
6565
Build Status
6666
------------
6767

68-
[![Build Status](https://secure.travis-ci.org/yui/yuicompressor.png?branch=master)](http://travis-ci.org/yui/yuicompressor)
68+
[![Build Status](https://secure.travis-ci.org/yui/yuicompressor.svg?branch=master)](http://travis-ci.org/yui/yuicompressor)
6969

7070

7171
Global Options

src/com/yahoo/platform/yui/compressor/CssCompressor.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void compress(Writer out, int linebreakpos)
139139

140140

141141
css = this.preserveToken(css, "url", "(?i)url\\(\\s*([\"']?)data\\:", true, preservedTokens);
142-
css = this.preserveToken(css, "calc", "(?i)calc\\s*([\"']?)", false, preservedTokens);
142+
css = this.preserveToken(css, "calc", "(?i)calc\\(\\s*([\"']?)", false, preservedTokens);
143143
css = this.preserveToken(css, "progid:DXImageTransform.Microsoft.Matrix", "(?i)progid:DXImageTransform.Microsoft.Matrix\\s*([\"']?)", false, preservedTokens);
144144

145145

@@ -212,7 +212,13 @@ public void compress(Writer out, int linebreakpos)
212212
css = css.replace("/*" + placeholder + "*/", "");
213213
}
214214

215-
215+
// preserve \9 IE hack
216+
final String backslash9 = "\\9";
217+
while (css.indexOf(backslash9) > -1) {
218+
preservedTokens.add(backslash9);
219+
css = css.replace(backslash9, "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
220+
}
221+
216222
// Normalize all whitespace strings to single spaces. Easier to work with that way.
217223
css = css.replaceAll("\\s+", " ");
218224

@@ -255,7 +261,8 @@ public void compress(Writer out, int linebreakpos)
255261
p = Pattern.compile("(?i)^(.*)(@charset)( \"[^\"]*\";)");
256262
m = p.matcher(css);
257263
while (m.find()) {
258-
m.appendReplacement(sb, m.group(2).toLowerCase() + m.group(3) + m.group(1));
264+
String s = m.group(1).replaceAll("\\\\", "\\\\\\\\").replaceAll("\\$", "\\\\\\$");
265+
m.appendReplacement(sb, m.group(2).toLowerCase() + m.group(3) + s);
259266
}
260267
m.appendTail(sb);
261268
css = sb.toString();

src/com/yahoo/platform/yui/compressor/JarClassLoader.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,19 @@ private Class loadClassData(JarFile jarFile, String className) {
134134

135135
// Read the Jar File entry and define the class...
136136
Class c = null;
137+
InputStream is = null;
137138
try {
138-
InputStream is = jarFile.getInputStream(jarEntry);
139+
is = jarFile.getInputStream(jarEntry);
139140
ByteArrayOutputStream os = new ByteArrayOutputStream();
140141
copy(is, os);
141142
byte[] bytes = os.toByteArray();
142143
c = defineClass(className, bytes, 0, bytes.length);
143144
} catch (IOException ioe) {
144145
/* ignore */
146+
} finally {
147+
if (is != null) {
148+
try { is.close(); } catch(Exception e) {}
149+
}
145150
}
146151

147152
return c;

src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java

100644100755
Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ public class JavaScriptCompressor {
6262
for (char c = '0'; c <= '9'; c++)
6363
threes.add(two + Character.toString(c));
6464
}
65-
65+
6666
// Remove two-letter JavaScript reserved words and built-in globals...
6767
twos.remove("as");
6868
twos.remove("is");
6969
twos.remove("do");
7070
twos.remove("if");
7171
twos.remove("in");
7272
twos.removeAll(builtin);
73-
73+
7474
// Remove three-letter JavaScript reserved words and built-in globals...
7575
threes.remove("for");
7676
threes.remove("int");
@@ -316,7 +316,7 @@ private static ArrayList parse(Reader in, ErrorReporter reporter)
316316
String source = parser.getEncodedSource();
317317

318318
int offset = 0;
319-
int length = source.length();
319+
int length = (source != null) ? source.length() : 0;
320320
ArrayList tokens = new ArrayList();
321321
StringBuffer sb = new StringBuffer();
322322

@@ -361,31 +361,34 @@ private static void processStringLiterals(ArrayList tokens, boolean merge) {
361361
if (merge) {
362362

363363
// Concatenate string literals that are being appended wherever
364-
// it is safe to do so. Note that we take care of the case:
364+
// it is safe to do so. Note that we take care of the cases:
365365
// "a" + "b".toUpperCase()
366+
// "a" + "bcd"[i]
366367

367-
for (i = 0; i < length; i++) {
368+
for (i = 1; i < length - 1; i++) {
368369
token = (JavaScriptToken) tokens.get(i);
369-
switch (token.getType()) {
370-
371-
case Token.ADD:
372-
if (i > 0 && i < length) {
373-
prevToken = (JavaScriptToken) tokens.get(i - 1);
374-
nextToken = (JavaScriptToken) tokens.get(i + 1);
375-
if (prevToken.getType() == Token.STRING && nextToken.getType() == Token.STRING &&
376-
(i == length - 1 || ((JavaScriptToken) tokens.get(i + 2)).getType() != Token.DOT)) {
377-
tokens.set(i - 1, new JavaScriptToken(Token.STRING,
378-
prevToken.getValue() + nextToken.getValue()));
379-
tokens.remove(i + 1);
380-
tokens.remove(i);
381-
i = i - 1;
382-
length = length - 2;
383-
break;
370+
if (token.getType() == Token.ADD) {
371+
prevToken = (JavaScriptToken) tokens.get(i - 1);
372+
nextToken = (JavaScriptToken) tokens.get(i + 1);
373+
if (prevToken.getType() == Token.STRING &&
374+
nextToken.getType() == Token.STRING ) {
375+
if (i < length - 2) {
376+
JavaScriptToken nextNextToken = (JavaScriptToken) tokens.get(i + 2);
377+
if (nextNextToken.getType() == Token.DOT ||
378+
nextNextToken.getType() == Token.LB) {
379+
i += 3;
380+
continue;
384381
}
385382
}
383+
tokens.set(i - 1, new JavaScriptToken(Token.STRING,
384+
prevToken.getValue() + nextToken.getValue()));
385+
tokens.remove(i + 1);
386+
tokens.remove(i);
387+
i--;
388+
length -= 2;
389+
}
386390
}
387391
}
388-
389392
}
390393

391394
// Second pass...
@@ -536,7 +539,11 @@ public JavaScriptCompressor(Reader in, ErrorReporter reporter)
536539
this.logger = reporter;
537540
this.tokens = parse(in, reporter);
538541
}
539-
542+
public void compress(Writer out, int linebreak, boolean munge, boolean verbose,
543+
boolean preserveAllSemiColons, boolean disableOptimizations)
544+
throws IOException {
545+
compress(out, null, linebreak, munge, verbose, preserveAllSemiColons, disableOptimizations);
546+
}
540547
public void compress(Writer out, Writer mungemap, int linebreak, boolean munge, boolean verbose,
541548
boolean preserveAllSemiColons, boolean disableOptimizations, boolean preserveUnknownHints)
542549
throws IOException {

tests/_string_combo2.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function test(){
2+
var a = "a" +
3+
"b".toUpperCase();
4+
}

tests/_string_combo2.js.min

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function test(){var b="a"+"b".toUpperCase()};

tests/_string_combo3.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function test(){
2+
for ( var i = 0; i < 3; i++ ) {
3+
var a = "a" + "bcd"[i];
4+
}
5+
}

tests/_string_combo3.js.min

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function test(){for(var c=0;c<3;c++){var b="a"+"bcd"[c]}};

tests/bug-preservetoken-calc.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* test for not breaking class names with preserved tokens */
2+
.calculate-classname-test {
3+
width: calc(10% + 100px);
4+
}
5+
6+
.background-url-test {
7+
content: '/foo/bar';
8+
}

tests/bug-preservetoken-calc.css.min

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.calculate-classname-test{width:calc(10% + 100px)}.background-url-test{content:'/foo/bar'}

tests/ie-backslash9-hack.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
label{padding-left: 4px\9;}
2+
@charset "UTF-8";

tests/ie-backslash9-hack.css.min

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@charset "UTF-8";label{padding-left:4px\9}

tests/issue151.css.FAIL

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@keyframes fadeIn {
2+
0% {
3+
opacity: 0;
4+
}
5+
100% {
6+
opacity: 1;
7+
}
8+
}

tests/issue151.css.min

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@keyframes fadeIn{0%{opacity:0}100%{opacity:1}}

tests/issue205.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a[id$=_foo] { abc : abc;};
2+
@charset "utf-8";

tests/issue205.css.min

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@charset "utf-8";a[id$=_foo]{abc:abc};

0 commit comments

Comments
 (0)