Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. //*****************************************************************************
  2. // Do not remove this notice.
  3. //
  4. // Copyright 2001 by Mike Hall.
  5. // See http://www.brainjar.com for terms of use.
  6. //*****************************************************************************
  7.  
  8.  
  9. // Modified to support DynAPI Compressor 20-Nov-2002
  10.  
  11. var literalStrings;  // For temporary storage of literal strings.
  12.  
  13. function Crunch(jsCode,level,callback) {
  14.         var i, jsTextOut;
  15.  
  16.         Crunch._level = level;
  17.         Crunch._textOut = jsCode;
  18.         Crunch._callBack = callback;
  19.         Crunch._startCruncher();
  20.  
  21. };
  22.  
  23. Crunch._textOut='';
  24. Crunch._textIn='';
  25. Crunch._startCruncher=function(){      
  26.         this._setStatus('Replacing literal strings... ');
  27.         if(this._level=='none') window.setTimeout('Crunch._makeCallBack()',100);
  28.         else {
  29.                 window.setTimeout('Crunch._replaceLiteralStrings()',100);  
  30.         }
  31. };
  32. Crunch._makeCallBack = function(){
  33.         if(this._callBack) this._callBack('complete',this._textOut);
  34. };
  35. Crunch._setStatus=function(t){
  36.         if(this._callBack) this._callBack('status',t);
  37. };
  38. Crunch._replaceLiteralStrings = function() {
  39.         var s=this._textOut;
  40.         var i, c, t, lines, escaped, quoteChar, inQuote, literal;
  41.  
  42.         literalStrings = new Array();
  43.         t = "";
  44.  
  45.         // Split script into individual lines.
  46.         lines = s.split("\n");
  47.        
  48.         for (i = 0; i < lines.length; i++) {
  49.                 j = 0;
  50.                 inQuote = false;
  51.                 while (j <= lines[i].length) {
  52.                         c = lines[i].charAt(j);
  53.  
  54.                         // If not already in a string, look for the start of one.
  55.                         if (!inQuote) {
  56.                                 if (c == '"' || c == "'") {
  57.                                         inQuote = true;
  58.                                         escaped = false;
  59.                                         quoteChar = c;
  60.                                         literal = c;
  61.                                 } else {
  62.                                         t += c;
  63.                                 }
  64.                         }
  65.                         // Already in a string, look for end and copy characters.
  66.                         else {
  67.                                 if (c == quoteChar && !escaped) {
  68.                                         inQuote = false;
  69.                                         literal += quoteChar;
  70.                                         t += "__" + literalStrings.length + "__";
  71.                                         literalStrings[literalStrings.length] = literal;
  72.                                 } else if (c == "\\" && !escaped) {
  73.                                         escaped = true;
  74.                                 } else {
  75.                                 escaped = false;
  76.                         }
  77.                                 literal += c;
  78.                         }
  79.                         j++;
  80.                 }
  81.                 t += "\n";
  82.         }
  83.  
  84.         // Save text and run next function
  85.         this._textOut=t;
  86.         this._setStatus('Removing comments...');
  87.         window.setTimeout('Crunch._removeComments()',100);
  88. }
  89.  
  90. Crunch._removeComments = function() {
  91.         var s=this._textOut;
  92.         var lines, i, t;
  93.  
  94.         // Remove '//' comments from each line.
  95.         lines = s.split("\n");
  96.         t = [''];
  97.         for (i = 0; i < lines.length; i++){
  98.                 if(lines[i].indexOf('//')<0) t[t.length] = lines[i];
  99.                 else{
  100.                         t[t.length] = lines[i].replace(/([^\x2f]*)\x2f\x2f.*$/, "$1");
  101.                 }
  102.         }
  103.  
  104.         t=(this._level=='low')? t.join('<$Link/>'):t.join('');
  105.  
  106.         // Replace newline characters with spaces.
  107.         //t = t.replace(/(.*)\n(.*)/g, "$1 $2");
  108.  
  109.         // Remove '/* ... */' comments.
  110.         lines = t.split("*/");
  111.         t = [''];
  112.         for (i = 0; i < lines.length; i++) {
  113.                 if(lines[i].indexOf('/*')<0) t[t.length] = lines[i];
  114.                 else{
  115.                         t[t.length] = lines[i].replace(/(.*)\x2f\x2a(.*)$/g, "$1 ");
  116.                 }
  117.         }
  118.        
  119.         t=t.join('');
  120.         if (this._level=='low') t=t.replace(/\<\$Link\/\>/g,'\n');
  121.        
  122.         // Save text and run next function
  123.         this._textOut=t;
  124.         if(this._level=='low'){
  125.                 this._setStatus('Combining literal strings...');
  126.                 window.setTimeout('Crunch._combineLiteralStrings()',100);      
  127.         }else {
  128.                 this._setStatus('Compressing white space...');
  129.                 window.setTimeout('Crunch._compressWhiteSpace()',100); 
  130.         }
  131.  
  132. };
  133. Crunch._compressWhiteSpace = function() {
  134.         var s=this._textOut;
  135.        
  136.         // Condense white space.
  137.         s = s.replace(/\s+/g, " ");
  138.         s = s.replace(/^\s(.*)/, "$1");
  139.         s = s.replace(/(.*)\s$/, "$1");
  140.  
  141.         // Remove uneccessary white space around operators, braces and parentices.
  142.         s = s.replace(/\s([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])/g, "$1");
  143.         s = s.replace(/([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])\s/g, "$1");
  144.  
  145.         // Save text and run next function
  146.         this._textOut=s;
  147.         this._setStatus('Combining literal strings...');
  148.         window.setTimeout('Crunch._combineLiteralStrings()',100);
  149.        
  150. };
  151. Crunch._combineLiteralStrings = function() {
  152.         var s=this._textOut;
  153.         var i;
  154.         s = s.replace(/"\+"/g, "");
  155.         s = s.replace(/'\+'/g, "");
  156.  
  157.         // Save text and run next function
  158.         this._textOut=s;
  159.         this._setStatus('Restoring literal strings...');
  160.     window.setTimeout('Crunch._restoreLiteralStrings()',100);
  161.  
  162. };
  163. Crunch._restoreLiteralStrings = function() {
  164.         var s=this._textOut;
  165.        
  166.         // modified: replace new RegExp("__" + i + "__") with "__" + i + "__"
  167.         var i;
  168.         for (i = 0; i < literalStrings.length; i++) {
  169.                 s = s.replace("__" + i + "__", literalStrings[i]);
  170.         }
  171.        
  172.         // Save text and run next function
  173.         this._textOut=s;
  174.         this._setStatus('done.');
  175.     window.setTimeout('Crunch._makeCallBack()',100);
  176. };