Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow string compression #343

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ private static ArrayList parse(Reader in, ErrorReporter reporter)
env.setLanguageVersion(Context.VERSION_1_7);
Parser parser = new Parser(env, reporter);
parser.parse(in, null, 1);
return parse(parser);
}

private static ArrayList parse(String script, ErrorReporter reporter)
throws EvaluatorException {

CompilerEnvirons env = new CompilerEnvirons();
env.setLanguageVersion(Context.VERSION_1_7);
Parser parser = new Parser(env, reporter);
parser.parse(script, null, 1);
return parse(parser);
}

private static ArrayList parse(Parser parser)
throws EvaluatorException {

String source = parser.getEncodedSource();

int offset = 0;
Expand Down Expand Up @@ -539,15 +555,32 @@ public JavaScriptCompressor(Reader in, ErrorReporter reporter)
this.logger = reporter;
this.tokens = parse(in, reporter);
}

public JavaScriptCompressor(String script, ErrorReporter reporter)
throws IOException, EvaluatorException {

this.logger = reporter;
this.tokens = parse(script, reporter);
}

public void compress(Writer out, int linebreak, boolean munge, boolean verbose,
boolean preserveAllSemiColons, boolean disableOptimizations)
boolean preserveAllSemiColons, boolean disableOptimizations)
throws IOException {
compress(out, null, linebreak, munge, verbose, preserveAllSemiColons,
disableOptimizations, false);
compress(out, null, null, linebreak, munge, verbose, preserveAllSemiColons,
disableOptimizations, false);
}

public void compress(Writer out, Writer mungemap, int linebreak, boolean munge, boolean verbose,
boolean preserveAllSemiColons, boolean disableOptimizations, boolean preserveUnknownHints)
throws IOException {
compress(out, mungemap, null, linebreak, munge, verbose, preserveAllSemiColons,
disableOptimizations, preserveUnknownHints);
}

public void compress(Writer out, Writer mungemap, StringBuffer writerBuffer, int linebreak, boolean munge,
boolean verbose, boolean preserveAllSemiColons,
boolean disableOptimizations, boolean preserveUnknownHints)
throws IOException {

this.munge = munge;
this.verbose = verbose;
Expand All @@ -565,11 +598,17 @@ public void compress(Writer out, Writer mungemap, int linebreak, boolean munge,
mungeSymboltree();
StringBuffer sb = printSymbolTree(linebreak, preserveAllSemiColons);

out.write(sb.toString());
if (out != null) {
out.write(sb.toString());
}

if (mungemap != null) {
printMungeMapping(mungemap);
}

if (writerBuffer != null) {
writerBuffer.append(sb.toString());
}
}

private ScriptOrFnScope getCurrentScope() {
Expand Down