]> source.dussan.org Git - jquery.git/commitdiff
Updated Packer - the current version was working incorrectly.
authorJohn Resig <jeresig@gmail.com>
Sat, 5 May 2007 17:37:38 +0000 (17:37 +0000)
committerJohn Resig <jeresig@gmail.com>
Sat, 5 May 2007 17:37:38 +0000 (17:37 +0000)
build/js/Packer.js
build/js/base2.js

index 4e907cfb1e2673c1a8e5f0ca2ce78bd642e91515..88c47d0f42862e4f1027aa3d1bd4486a3ad8ada9 100644 (file)
@@ -1,5 +1,5 @@
 /*\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
@@ -13,14 +13,14 @@ var WORDS = /\w+/g;
 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
@@ -59,8 +59,13 @@ var Packer = Base.extend({
                };\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
@@ -74,7 +79,7 @@ var Packer = Base.extend({
                // 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
@@ -93,8 +98,9 @@ var Packer = Base.extend({
                                // 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
@@ -106,13 +112,13 @@ var Packer = Base.extend({
                                        }\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
@@ -158,16 +164,15 @@ var Packer = Base.extend({
                        "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
@@ -178,17 +183,16 @@ var Packer = Base.extend({
                "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
index ee57e5d643861ed6bd85ecfb48042485f57d8df2..87eb37b6590f1eec35e3e947b9c0ca9db38e10f2 100644 (file)
@@ -1,4 +1,4 @@
-// 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
@@ -141,6 +141,12 @@ var assertType = function(object, type, message) {
        }\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
@@ -416,12 +422,12 @@ var Enumerable = Module.extend({
        },\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
@@ -448,7 +454,7 @@ var Enumerable = Module.extend({
                });\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
@@ -475,10 +481,10 @@ var IArray = Module.extend({
        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
@@ -904,20 +910,20 @@ var Namespace = Base.extend({
                \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
@@ -949,7 +955,7 @@ eval(this.exports);
 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