/*\r
- Packer version 3.0 (beta 5) - copyright 2004-2007, Dean Edwards\r
+ Packer version 3.0 (beta 8) - copyright 2004-2007, Dean Edwards\r
http://www.opensource.org/licenses/mit-license\r
*/\r
\r
var Packer = Base.extend({\r
minify: function(script) {\r
script = script.replace(Packer.CONTINUE, "");\r
- script = Packer.clean.exec(script);\r
+ script = Packer.data.exec(script);\r
script = Packer.whitespace.exec(script);\r
- script = Packer.clean.exec(script); // seem to grab a few more bytes on the second pass\r
+ script = Packer.clean.exec(script);\r
return script;\r
},\r
\r
pack: function(script, base62, shrink) {\r
- script = this.minify(script);\r
+ script = this.minify(script + "\n");\r
if (shrink) script = this._shrinkVariables(script);\r
if (base62) script = this._base62Encode(script); \r
return script;\r
};\r
\r
var data = []; // encoded strings and regular expressions\r
+ var REGEXP = /^[^'"]\//;\r
var store = function(string) {\r
var replacement = "#" + data.length;\r
+ if (REGEXP.test(string)) {\r
+ replacement = string.charAt(0) + replacement;\r
+ string = string.slice(1);\r
+ }\r
data.push(string);\r
return replacement;\r
};\r
// identify blocks, particularly identify function blocks (which define scope)\r
var BLOCK = /(function\s*[\w$]*\s*\(\s*([^\)]*)\s*\)\s*)?(\{([^{}]*)\})/;\r
var VAR_ = /var\s+/g;\r
- var VAR_NAME = /var\s+[\w$]{2,}/g; // > 1 char\r
+ var VAR_NAME = /var\s+[\w$]+/g;\r
var COMMA = /\s*,\s*/;\r
var blocks = []; // store program blocks (anything between braces {})\r
// encoder for program blocks\r
// process each identifier\r
var count = 0, shortId;\r
forEach (ids, function(id) {\r
- id = rescape(trim(id));\r
- if (id) {\r
+ id = trim(id);\r
+ if (id && id.length > 1) { // > 1 char\r
+ id = rescape(id);\r
// find the next free short name (check everything in the current scope)\r
do shortId = encode52(count++);\r
while (new RegExp("[^\\w$.]" + shortId + "[^\\w$:]").test(block));\r
}\r
});\r
}\r
- var replacement = "~" + blocks.length;\r
+ var replacement = "~" + blocks.length + "~";\r
blocks.push(block);\r
return replacement;\r
};\r
\r
// decoder for program blocks\r
- var ENCODED = /~(\d+)/;\r
+ var ENCODED = /~(\d+)~/;\r
var decode = function(script) {\r
while (ENCODED.test(script)) {\r
script = script.replace(global(ENCODED), function(match, index) {\r
"replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('%1',%2,%3,'%4'.split('|'),0,{}))",\r
\r
init: function() {\r
- this.data = reduce(this.data, new RegGrp, function(data, replacement, expression) {\r
+ this.data = reduce(this.data, function(data, replacement, expression) {\r
data.store(this.javascript.exec(expression), replacement);\r
return data;\r
- }, this);\r
+ }, new RegGrp, this);\r
this.clean = this.data.union(this.clean);\r
this.whitespace = this.data.union(this.whitespace);\r
},\r
\r
clean: {\r
- ";;;[^\\n]*": REMOVE, // triple semi-colons treated like line comments\r
"\\(\\s*;\\s*;\\s*\\)": "(;;)", // for (;;) loops\r
"throw[^};]+[};]": IGNORE, // a safari 1.3 bug\r
";+\\s*([};])": "$1"\r
"STRING1": IGNORE,\r
'STRING2': IGNORE,\r
"CONDITIONAL": IGNORE, // conditional comments\r
- "(COMMENT1)\\n\\s*(REGEXP)?": "\n$2",\r
+ "(COMMENT1)\\n\\s*(REGEXP)?": "\n$3",\r
"(COMMENT2)\\s*(REGEXP)?": " $3",\r
- "COMMENT1$": REMOVE,\r
"([\\[(\\^=,{}:;&|!*?])\\s*(REGEXP)": "$1$2"\r
},\r
\r
javascript: new RegGrp({\r
- COMMENT1: /\/\/[^\n]*/.source,\r
+ COMMENT1: /(\/\/|;;;)[^\n]*/.source,\r
COMMENT2: /\/\*[^*]*\*+([^\/][^*]*\*+)*\//.source,\r
CONDITIONAL: /\/\*@|@\*\/|\/\/@[^\n]*\n/.source,\r
- REGEXP: /\/(\\\/|[^*\/])(\\.|[^\/\n\\])*\//.source,\r
+ REGEXP: /\/(\\[\/\\]|[^*\/])(\\.|[^\/\n\\])*\/[gim]*/.source,\r
STRING1: /'(\\.|[^'\\])*'/.source,\r
STRING2: /"(\\.|[^"\\])*"/.source\r
}),\r
-// timestamp: Tue, 24 Apr 2007 09:57:15\r
+// timestamp: Tue, 01 May 2007 19:13:00\r
/*\r
base2.js - copyright 2007, Dean Edwards\r
http://www.opensource.org/licenses/mit-license\r
}\r
};\r
\r
+var copy = function(object) {\r
+ var fn = new Function;\r
+ fn.prototype = object;\r
+ return new fn;\r
+};\r
+\r
var format = function(string) {\r
// replace %n with arguments[n]\r
// e.g. format("%1 %2%3 %2a %1%3", "she", "se", "lls");\r
},\r
\r
filter: function(object, test, context) {\r
- return this.reduce(object, new Array2, function(result, value, key) {\r
+ return this.reduce(object, function(result, value, key) {\r
if (test.call(context, value, key, object)) {\r
result[result.length] = value;\r
}\r
return result;\r
- });\r
+ }, new Array2);\r
},\r
\r
invoke: function(object, method) {\r
});\r
},\r
\r
- reduce: function(object, result, block, context) {\r
+ reduce: function(object, block, result, context) {\r
this.forEach (object, function(value, key) {\r
result = block.call(context, result, value, key, object);\r
});\r
combine: function(keys, values) {\r
// combine two arrays to make a hash\r
if (!values) values = keys;\r
- return this.reduce(keys, {}, function(object, key, index) {\r
+ return this.reduce(keys, function(object, key, index) {\r
object[key] = values[index];\r
return object;\r
- });\r
+ }, {});\r
},\r
\r
copy: function(array) {\r
\r
var namespace = "var base=" + base + ";";\r
var imports = ("base2,lang," + this.imports).split(",");\r
- _private.imports = Enumerable.reduce(imports, namespace, function(namespace, name) {\r
+ _private.imports = Enumerable.reduce(imports, function(namespace, name) {\r
if (base2[name]) namespace += base2[name].namespace;\r
return namespace;\r
- });\r
+ }, namespace);\r
\r
var namespace = format("base2.%1=%1;", this.name);\r
var exports = this.exports.split(",");\r
- _private.exports = Enumerable.reduce(exports, namespace, function(namespace, name) {\r
+ _private.exports = Enumerable.reduce(exports, function(namespace, name) {\r
if (name) {\r
this.namespace += format("var %2=%1.%2;", this.name, name);\r
namespace += format("if(!%1.%2)%1.%2=%2;base2.%2=%1.%2;", this.name, name);\r
}\r
return namespace;\r
- }, this);\r
+ }, namespace, this);\r
\r
if (this.name != "base2") {\r
base2.namespace += format("var %1=base2.%1;", this.name);\r
var lang = new Namespace(this, {\r
name: "lang",\r
version: base2.version,\r
- exports: "K,assert,assertType,assignID,instanceOf,extend,format,forEach,match,rescape,slice,trim",\r
+ exports: "K,assert,assertType,assignID,copy,instanceOf,extend,format,forEach,match,rescape,slice,trim",\r
\r
init: function() {\r
this.extend = extend;\r