1 // <script type="text/javascript">
4 * pack a javascript file, and return a shorter version!
6 * a bit picky at present with ; and crlf reading...
9 JSDOC.CompressWhite = function (ts, packer)
13 //var str = File.read(fn);
26 // just spaces, not \n!
27 //if (tok.data.indexOf("\n") < 0) {
34 // add semi-colon's where linebreaks are used... - not foolproof yet.!
35 if (tok.isTypeN(Script.TOKidentifier)) {
36 //var tokident = ts.look(-1).data + tok.data + ts.look(1).data + ts.look(2).data;
37 // a = new function() {}
38 if (ts.look(1).isTypeN(Script.TOKassign) && ts.look(2).isTypeN(Script.TOKnew) &&
39 ts.look(3).isTypeN(Script.TOKfunction)) {
47 // if next is not ';' -> make it so...
49 if (!ts.look(1).isTypeN(Script.TOKsemicolon) && !ts.look(1).isTypeN(Script.TOKrbrace) && ts.look(1,true).isLineBreak()) {
50 ts.cur().outData = ts.cur().data +";";
56 // a = function() { ...
58 if (ts.look(1).isTypeN(Script.TOKassign) && ts.look(2).isTypeN(Script.TOKfunction)) {
60 //println("got = function() ");
65 // if next is not ';' -> make it so...
66 // although this var a=function(){},v,c; causes
67 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
68 ts.cur().outData = ts.cur().data+";";
74 // function a () { ... };
76 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
78 //println("got = function() ");
83 // if next is not ';' -> make it so...
84 // although this var a=function(){},v,c; causes
85 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
86 ts.cur().outData = ts.cur().data+";";
96 if (ts.look(1).isTypeN(Script.TOKassign) && ts.look(2).isTypeN(Script.TOKlbrace)) {
98 //println("----------*** 3 *** --------------");
101 if (!ts.balance("lbrace") ){
102 throw "could not find end lbrace!!!";
104 // if next is not ';' -> make it so...
106 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
107 ts.cur().outData = ts.cur().data +";";
120 //println("got Token: " + tok.type);
125 // things that need space appending
126 case Script.TOKfunction:
127 case Script.TOKbreak:
128 case Script.TOKcontinue:
129 // if next item is a identifier..
130 if (ts.look(1).isTypeN(Script.TOKidentifier) || ts.look(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
131 tok.outData = tok.data + " ";
136 case Script.TOKreturn: // if next item is not a semi; (or }
137 if (ts.look(1).isData(';') || ts.look(1).isData('}')) {
140 tok.outData = tok.data + " ";
145 case Script.TOKelse: // if next item is not a semi; (or }
146 if (!ts.look(1).isTypeN(Script.TOKif)) {
150 tok.outData = tok.data + " ";
153 case Script.TOKplusplus: // if previous was a plus or next is a + add a space..
154 case Script.TOKminusminus: // if previous was a - or next is a - add a space..
156 var p = (Script.TOKminusminus == tok.tokN ? '-' : '+');
158 if (ts.look(1).data == p) {
159 tok.outData = tok.data + " ";
161 if (ts.look(-1).data == p) {
162 tok.outData = " " + tok.data;
167 case Script.TOKin: // before and after??
168 case Script.TOKinstanceof:
170 tok.outData = " " + tok.data + " ";
173 case Script.TOKvar: // always after..
175 case Script.TOKdelete:
176 case Script.TOKthrow:
179 case Script.TOKtypeof:
181 tok.outData = tok.data + " ";
184 case Script.TOKsemicolon:
185 //remove semicolon before brace --
186 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
199 //var f = new File(minfile, File.NEW);
203 out.length = ts.slen; // prealloc.
215 if (tok.isTypeN(Script.TOKidentifier) && tok.identifier && tok.identifier.mungedValue.length) {
216 //f.write(tok.identifier.mungedValue);
217 out +=tok.identifier.mungedValue;
221 // at this point we can apply a text translation kit...
223 if (tok.type == 'string') {
224 if (packer.stringHandler) {
225 out += packer.stringHandler(tok);
229 //f.write(tok.outData);
232 if ((tok.outData == ';') && (out.length - outoff > 255)) {
239 // remove the last ';' !!!
240 if (out.substring(out.length-1) == ';') {
241 return out.substring(0,out.length-1);