diff options
author | Egil Moeller <egil.moller@freecode.no> | 2010-03-21 23:33:06 +0100 |
---|---|---|
committer | Egil Moeller <egil.moller@freecode.no> | 2010-03-21 23:33:06 +0100 |
commit | d56b9b3b82cdebcaeb00eec0fcb4326ad21adaa8 (patch) | |
tree | b1dfe31956f3fc86e3408f1efac5e12acf65b11a /infrastructure/framework-src/modules/yuicompressor.js | |
parent | c1894c8e0a52f4e3d2f89fa92f0066bbf0fcf1b1 (diff) | |
parent | 103d4926ae6c61824dc0b48be7bf66f08830ed47 (diff) | |
download | etherpad-d56b9b3b82cdebcaeb00eec0fcb4326ad21adaa8.tar.gz etherpad-d56b9b3b82cdebcaeb00eec0fcb4326ad21adaa8.tar.xz etherpad-d56b9b3b82cdebcaeb00eec0fcb4326ad21adaa8.zip |
Merge branch 'master' of git@github.com:ether/pad
Diffstat (limited to 'infrastructure/framework-src/modules/yuicompressor.js')
-rw-r--r-- | infrastructure/framework-src/modules/yuicompressor.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/infrastructure/framework-src/modules/yuicompressor.js b/infrastructure/framework-src/modules/yuicompressor.js new file mode 100644 index 0000000..572cc0d --- /dev/null +++ b/infrastructure/framework-src/modules/yuicompressor.js @@ -0,0 +1,85 @@ +/** + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +jimport("java.lang.System.err") +jimport("yuicompressor.org.mozilla.javascript.ErrorReporter"); +jimport("com.yahoo.platform.yui.compressor.JavaScriptCompressor") +jimport("com.yahoo.platform.yui.compressor.CssCompressor") +jimport("java.io.StringReader"); +jimport("java.io.StringWriter"); + +/** + * Compresses the given JavaScript code into an equivalent, shorter string of code using + * YUICompressor. In addition to removing white-space and comments, YUICompressor + * does a full semantic parse of the code and renames non-global variables to have + * very short names. Scopes that are visible to "eval" and "with" are excluded from + * variable renaming, making the operation very safe. + * <p> + * For example, + * yuicompressor.compressJS("function foo() { var longVariableName = 3; return longVariableName }"); + * produces + * "function foo(){var A=3;return A;}" + */ + +function compressJS(code) { + function getComplaint(message, sourceName, line, lineSource, lineOffset) { + if (line < 0) return message; + else return (line+":"+lineOffset+":"+message); + } + function complaintHandler(func) { + return function(message, sourceName, line, lineSource, lineOffset) { + return func(getComplaint(message, sourceName, line, lineSource, lineOffset)); + } + } + var myErrorReporter = new JavaAdapter(ErrorReporter, { + warning: complaintHandler(function (msg) { + if (msg.indexOf("Try to use a single 'var' statement per scope.") >= 0) + return; + err.println("yuicompressor.compressJS warning: "+msg); + }), + error: complaintHandler(function (msg) { + throw new Error("yuicompressor.compressJS error: "+msg); + }), + runtimeError: complaintHandler(function (msg) { + throw new Error("yuicompressor.compressJS error: "+msg); + }) + }); + + var munge = true; + var verbose = false; + var optimize = true; + var wrapPos = 100; // characters, no wrap == -1 + var compressor = new JavaScriptCompressor(new StringReader(code), myErrorReporter); + var writer = new StringWriter(); + compressor.compress(writer, 100, munge, verbose, true, !optimize); + return String(writer.toString()); +} + +/** + * Compresses the given CSS code into an equivalent, shorter string of code using + * YUICompressor. Besides removing unnecessary white-space and comments, the operation + * performs an assortment of semantics-preserving optimizations. The operation attempts + * to preserve common "hacks" that take advantage of browser differences in parsing. + */ + +function compressCSS(code) { + var compressor = new CssCompressor(new StringReader(code)); + var wrapPos = 100; // characters, no wrap == -1 + var writer = new StringWriter(); + compressor.compress(writer, wrapPos); + return String(writer.toString()); +} |