jquery/build/js/pack.js

1 line
12 KiB
JavaScript
Raw Normal View History

2006-08-13 17:12:35 +02:00
/* packer, version 2.0.2 (2005-08-19) Copyright 2004-2005, Dean Edwards License: http://creativecommons.org/licenses/LGPL/2.1/ */ function pack(_script, _encoding, _fastDecode, _specialChars) { // constants var $IGNORE = "$1"; // validate parameters _script += "\n"; _encoding = Math.min(parseInt(_encoding), 95); // apply all parsing routines function _pack($script) { var i, $parse; for (i = 0; ($parse = _parsers[i]); i++) { $script = $parse($script); } return $script; }; // unpacking function - this is the boot strap function // data extracted from this packing routine is passed to // this function when decoded in the target var _unpack = function($packed, $ascii, $count, $keywords, $encode, $decode) { while ($count--) if ($keywords[$count]) $packed = $packed.replace(new RegExp('\\b' + $encode($count) + '\\b', 'g'), $keywords[$count]); return $packed; }; // code-snippet inserted into the unpacker to speed up decoding var _decode = function() { // does the browser support String.replace where the // replacement value is a function? if (!''.replace(/^/, String)) { // decode all the values we need while ($count--) $decode[$encode($count)] = $keywords[$count] || $encode($count); // global replacement function $keywords = [function($encoded){return $decode[$encoded]}]; // generic match $encode = function(){return'\\w+'}; // reset the loop counter - we are now doing a global replace $count = 1; } }; // keep a list of parsing functions, they'll be executed all at once var _parsers = []; function _addParser($parser) { _parsers[_parsers.length] = $parser; }; // zero encoding - just removal of white space and comments function _basicCompression($script) { var $parser = new ParseMaster; // make safe $parser.escapeChar = "\\"; // protect strings $parser.add(/'[^'\n\r]*'/, $IGNORE); $parser.add(/"[^"\n\r]*"/, $IGNORE); // remove comments $parser.add(/\/\/[^\n\r]*[\n\r]/, " "); $parser.add(/\/\*[^*]*\*+([^\/][^*]*\*+)*\//, " "); // protect regular expressions $parser.add(/\s+(\/[^\/\n\r\*][^\/\n\r]*\/g?i?)/, "$2"); // IGNORE $parser.add(/[^\w\x24\/'"*)\?:]\/[^\/\n\r\*][^\/\n\r]*\/g?i?/, $IGNORE); // remove: ;;; doSomething(); if (_specialChars) $parser.add(/;;;[^\n\r]+[\n\r]/); // remove redundant semi-colons $parser.add(/\(;;\)/, $IGNORE); // protect for (;;) loops $parser.add(/;+\s*([};])/, "$2"); // apply the above $script = $parser.exec($script); // remove white-space $parser.add(/(\b|\x24)\s+(\b|\x24)/, "$2 $3"); $parser.add(/([+\-])\s+([+\-])/, "$2 $3"); $parser.add(/\s+/, ""); // done return $parser.exec($script); }; function _encodeSpecialChars($script) { var $parser = new ParseMaster; // replace: $name -> n, $$name -> na $parser.add(/((\x24+)([a-zA-Z$_]+))(\d*)/, function($match, $offset) { var $length = $match[$offset + 2].length; var $start = $length - Math.max($length - $match[$offset + 3].length, 0); return $match[$offset + 1].substr($start, $length) + $match[$offset + 4]; }); // replace: _name -> _0, double-underscore (__name) is ignored var $regexp = /\b_[A-Za-z\d]\w*/; // build the word list var $keywords = _analyze($script, _globalize($regexp), _encodePrivate); // quick ref var $encoded = $keywords.$encoded; $parser.add($regexp, function($match, $offset) { return $encoded[$match[$offset]]; }); return $parser.exec($script); }; function _encodeKeywords($script) { // escape high-ascii values already in the script (i.e.