-
Notifications
You must be signed in to change notification settings - Fork 19
/
EnDeForm.js
477 lines (458 loc) · 16.2 KB
/
EnDeForm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* ========================================================================= //
# vi: set ts=4:
# vim: set ts=4:
#?
#? NAME
#? EnDeForm.js - formating JavaScript code and JSON data
#?
#? SYNOPSIS
#? <SCRIPT language="JavaScript1.3" type="text/javascript" charset="utf-8" src="JsColorizer.js"></SCRIPT>
#? <SCRIPT language="JavaScript1.3" type="text/javascript" charset="utf-8" src="JsDecoder.js"></SCRIPT>
#? <SCRIPT language="JavaScript1.3" type="text/javascript" charset="utf-8" src="JSReq.js"></SCRIPT>
#? <SCRIPT language="JavaScript1.5" type="text/javascript" charset="utf-8" src="EnDeForm.js"></SCRIPT>
#?
#? DESCRIPTION
#? The function herein tries to pretty print given data.
#?
#? This file contains functions/methods used to format a string in JSON.
#? syntax. It defines the EnDe.Form class with following variables and
#? functions:
#? white - true: keep all whitespace
#? - false: remove whitespace and add as needed
#? linked - number of elements allowed in one line
#? ident - ident string; default 4 spaces
#? trace - in case somthing goes wrong ..
#?
#? dispatch()- dispatcher function
#? format() - formatting function
#?
#? This file also conatins the function Form.image() to display an image
#? in the browser using the tag <img src=...>
#?
# HACKER's INFO
# For debugging __dbx() function is used. Unfortunately this function
# call may become a perfromance bottleneck.
#?
#? VERSION
#? @(#) EnDeForm.js 3.19 18/05/17 12:48:28
#?
#? AUTHOR
#? 15-nov-08 Achim Hoffmann, mailto: EnDe (at) my (dash) stp (dot) net
#?
* ========================================================================= */
/*
x=[[{abc:"1\23",xyz:8},/*leer * /leer:"",noch:'"x' ,x:var]];
y=[[{anArray:[2,3,4,["strin\"g1","string2",{aa:[]}],{"type":"simpleObject"},null],anotherProperty:"",anObject:{justANumber:2,func:function(){if(1>0)return false; else return true;}}}],true];
*/
if (typeof(EnDe)==='undefined') { EnDe = new function() {}; }
// ========================================================================= //
// EnDeform object methods //
// ========================================================================= //
EnDe.Form = new function() {
this.SID = '3.19';
this.sid = function() { return('@(#) EnDeForm.js 3.19 18/05/17 12:48:28 EnDe.Form'); };
// ===================================================================== //
// global EnDe.Form variables //
// ===================================================================== //
this.white = false;
this.linked = 5; // this.linked = parseInt(document.getElementById('j.linked').value, 10);
this.ident = ' ';
this.trace = false; // WARNING: enabling may produce huge data
// ===================================================================== //
// internal/private functions //
// ===================================================================== //
function __dbx(t,n) { if(EnDe.Form.trace===true) { EnDe.dbx(t,n); } };
function __err(src) { return '// **ERROR: ' + EnDe.Text.Entity(src); };
function __wrn(src) { return '// **WARNING: ' + EnDe.Text.Entity(src); };
function __src(src,idx) {
var s = src.substr( (idx-8), 16 );
return '\n// ** \u2025\u2025' + EnDe.Text.Entity(s) +
'\n// ** \u2025\u2025\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014^\u2025\u2025\u2025\u2025\u2025\u2025\u2025\n';
}; // __src
function _lookup(src,idx,chr) {
//#? lookup string starting at position idx 'til next occurance of chr
/* supported chr: ' " ( [ { < */
/* return matched string or empty string */
__dbx('_lookup(' + idx + ', ' + chr + ')');
var bux = '';
var end = chr; // symetric like " and '
switch (chr) {
case '(' : end = '\\)'; break; // some JavaScript's regex are so stupid
case '[' : end = '\\]'; break;
// case '[' : end = '\\]'; chr = '\\['; break;
// case '{' : end = '\\}'; chr = '\\{'; break;
case '{' : end = '}'; break;
case '<' : end = '>'; break;
}
var kkk = src.substring(idx);
var rex = new RegExp('([^' + end + ']*' + end + ')');
var ccc = kkk.match(rex);
__dbx('_lookup.string:`' + kkk + "'"); // WARNING: can be huge data
__dbx('_lookup.regex: /' + rex + '/');
if (ccc!==null) {
//__dbx('_lookup.match:' + ccc[0]);
bux = ccc[0];
while (ccc.pop()!=null) {}
}
ccc = null;
kkk = null;
rex = null;
return bux;
}; // _lookup
function _lookup_cmt(src,idx,chr,tab) {
//#? lookup comment starting at position idx 'til next occurance of chr
// chr: * to match /* .. */
// chr: ( to match //
/* return matched comment or empty string */
var bux = '';
var kkk = src.substring(idx);
var rex = null;
__dbx('_lookup_cmt.start:' + (idx));
if ((chr==='*') || (chr==='*/')) {
// try to match /*comment*/
rex = new RegExp('((:?.*?)\\*/)');
} else {
// try to match //comment
rex = new RegExp('(.*?)\\n');
}
var ccc = kkk.match(rex);
if (ccc!==null) {
bux = ccc[0];
//__dbx('_lookup_cmt.match:' + (bux));
while (ccc.pop()!=null) {}
} else {
bux = src.substr(idx, src.length);
bux += '\n' + __wrn('unterminated comment starting at postion ' + (idx));
bux += __src(src, idx);
bux += '\n' + _ident( tab );
}
ccc = null;
kkk = null;
rex = null;
__dbx('_lookup_cmt.end :' + (idx + bux.length-1));
return bux;
}; // _lookup_cmt
function _lookup_str(src,idx,chr,tab) {
//#? lookup "string" starting at position idx 'til next occurance of chr
/* return matched string or empty string */
var bux = '';
var kkk = '';
var bbb = 999; // infinite counter
__dbx('_lookup_str(' + idx + ', ' + chr + ', ' + tab + ')');
__dbx('_lookup_str.start[' + (idx-1) + ']: ' + src[idx-1]);
while(true) {
__dbx('_lookup_str.while: bbb=' + bbb + ', [' + idx + ']=' + src[idx]);
bbb--;
if (bbb <= 0) {
bux += '\n' + __wrn('too much escaped characters (999) ' + (idx-1));
break;
}
kkk = _lookup(src, idx, chr);
if (kkk.length <= 0) {
bux += src.substr(idx, src.length);
idx += bux.length;
bux += '\n' + __wrn('unterminated string starting at postion ' + (idx-1));
bux += __src(src, idx);
bux += '\n' + _ident( tab );
break;
}
__dbx('_lookup_str.match:' + kkk); // WARNING: can be huge data
bux += kkk;
idx += kkk.length + 1;
if (src[idx-3]!=='\\') {break; }
__dbx('_lookup_str.escape[' + (idx-3) + ']: ' + src[idx-3]);
idx--;
// if last matched character was escaped continue lookup
}
kkk = null;
__dbx('_lookup_str.end [' + (idx-1) + ']: ' + src[idx-1]);
__dbx('_lookup_str.return `' + bux + "'" );
return bux;
}; // _lookup_str
function _ident(tab) {
var bux = '';
for (var j=1; j<tab; j++) { bux += EnDe.Form.ident; }
// ToDo; return '<br>' + bux; // if white-space:pre not supported
return bux;
}; // _ident
// ===================================================================== //
// public formatting method //
// ===================================================================== //
this.format = function(src) {
//#? try to format given string, assuming JSON data
var bux = '';
var ccc = '';
var tab = 1;
var esc = false; // true if for \ character
var tmp = new Array(); tmp.length = src.length;
var i = 0;
var _c = 0; // for debugging only
var _l = 0; // for debugging only
var _s = 0; // for debugging only
/*
* mark all special characters in tmp[] array
*/
for (i=0; i<src.length; i++) {
ccc = src.charAt(i);
if (ccc==='\\') {
if (esc===true) {
tmp[i] = null;
} else {
tmp[i] = ccc;
}
esc = !esc;
continue;
}
if (esc===true) {
tmp[i] = null;
} else {
tmp[i] = null; // fallback
if (ccc.match(/[':",;=(){}]/)===null) {
// no match, check some others manually to avoid escaped characters in RegExp
switch (ccc) {
case '[' : tmp[i] = ccc; break;
case ']' : tmp[i] = ccc; break;
case '/' : tmp[i] = ccc; break;
case '\\' : tmp[i] = ccc; break;
}
} else {
tmp[i] = ccc;
}
}
esc = false;
} // src
if (this.trace > 0) { //#dbx
var dbx = '.Form marked tmp[] array:';
for (i=0; i<=src.length; i++) { dbx += '\n ['+i+']: '+src[i]+'\t'+tmp[i]; }
__dbx('#[' + dbx + '\n#]');
dbx = '';
} //#dbx
/*
* process string
*/
var kkk = '';
i = -1;
while (i <= src.length) {
i++;
ccc = src.charAt(i);
chr = src.charAt(i-1);
//__dbx('.>> : '+ccc);
if (tmp[i]===null) {
if (ccc.match(/[ \f\t\n\r]/)===null) {
// no whitespace, print
bux += ccc;
__dbx('.char['+i+']:' + ccc);
} else {
// got a whitespace, check how to handle ..
if (this.white===true) {
bux += ccc;
} else {
__dbx('.else : '+i);
if (chr.match(/[ \f\t\n\r]/)===null) {
// keep space if necessary
bux += ccc;
//if (tmp[i-1] !==null) { bux += ccc; }
} else {
// ignore space
__dbx('.space: '+i);
}
}
}
continue; // while (src)
}
__dbx('.next['+i+']:' + ccc);
switch (ccc) {
case "'" :
case '"' :
bux += ccc;
kkk = _lookup_str(src, (i+1), ccc, tab);
bux += kkk;
i += kkk.length;
_s++;
break;
case '/' :
if ((src[i+1]==='/') || (src[i+1]==='*')) { // try to match end of a comment
kkk = _lookup_cmt(src, i, src[i+1], tab);
bux += kkk;
i += kkk.length - 1;
if (kkk.length > 0) {
bux += '\n' + _ident( tab );
}
} else {
bux += ccc;
}
_c++;
break;
case ':' : if (this.white===true) { bux += ccc; } else { bux += '\t' + ': '; }; break;
case '=' : bux += '='; break;
case ';' :
case ',' : bux += ccc + '\n' + _ident( tab );break;
case '{' :
case '[' : bux += ccc + '\n' + _ident(++tab);break;
// ToDo: above { and [ do not match proper in _lookup()
case '(' :
bux += ccc;
kkk = _lookup(src, (i+1), ccc);
if (kkk.length <= this.linked) {
bux += kkk;
i += kkk.length;
} else {
bux += '\n' + _ident(++tab);
}
_l++;
break;
case ')' :
case ']' :
case '}' : bux += '\n' + _ident(--tab) + ccc;break;
case '\\' : bux += ccc; break;
default : bux += ccc; break; // fallback
} // switch(ccc)
kkk = null;
} // while src
__dbx('.format: lookup_str: '+_s+'; lookup_cmd: '+_c+'; lookup: '+_l);
return bux;
}; //format
this.dispatch= function(type,mode,src,white,linked) {
//#? dispatcher for various formattings; returns formated text
/* for type=JSReg white parameter has special meaning, see below */
// ToDo: quick&dirty use of white parameter
var bux = '';
var bbb = '';
var kkk = '';
this.white = white;
this.linked = linked;
__dbx('EnDe.Form.dispatch( ' + type + ', <src>, .. )');
switch (type) {
case 'JSDecode' :
bux = new JsDecoder();
bux.s = src;
// ToDo: return bux.decode(); // use this if garbage collector works
kkk = bux.decode();
bux.s = ''; bux = null;
return kkk;
break;
case 'JSFormat' : return this.format(src); break;
case 'XMLFormat' :
kkk = vkbeautify.xml(src, " ");
/*
alert('**FIXME: egenen XML-Parser bauen, vkbeautify.xml() ist buggy; kann nicht: <a x="</a>" >aaa</a>');
var x='<a id="a"><b id="b">hey!</b></a>';
var p=new DOMParser();
var dom=p.parseFromString(x,"text/xml");
var bux=dom.getElementsByTagName("*");
for (c=0;c<bux.length;c++) {
try { if (bux[c].childNodes.length == 0) { continue; } } // skip empty nodes
catch(e){ __dbx('EnDe.Form.dispatch: 0 bux['+c+'].childNodes.length failed: '+e); };
if (bux[c].nodeType != 1) { continue; } // skip empty or text nodes
kkk += '<' + bux[c].nodeName;
// loop attributes
kkk += '>';
bbb = bux[c].childNodes[0].nodeValue;
if (bbb !== null) { kkk += bbb; }
// loop childs
kkk += '</' + bux[c].nodeName + '>';
}
*/
return kkk;
break;
case 'toSource' :
kkk = src;
bux = 'EnDeJSwrapper=function(){' + src + ';}';
try { eval(bux); }
catch(e) { EnDeGUI.alert('EnDe.Form.dispatch','syntax error in code:\n' + e); }
delete this.bux;
bux = null;
// ToDo: should return here if there was a catch
/* from the new generated function code we remove the first and last line
* the second line and the the line before the last line are empty and
* hard to detect as such empty lines may be in the code also, so we leave
* them as is
*/
//return EnDeGUI.code( (EnDeJSwrapper).toSource(0).replace(/(^[^\n]*\n)/,'').replace(/(^\}\s*$)/,'') );
// replace last line does not work :-(
kkk = src;
try { kkk = (EnDeJSwrapper).toSource(0); }
catch(e) { EnDeGUI.alert('EnDe.Form.dispatch','browser does not support "().toSource"'); }
delete EnDeJSwrapper;
return kkk;
break;
case 'JSReg' :
/* sequence in "white" should be: doNotEval,doNotFunctionEval,doNotMainEval,runCheck */
var err = '';
var bbb = '';
var ccc = '';
var ggg = '';
var rrr = '';
var eee = '';
var ttt = '';
bux = window.JSReg.create();
bux.setDebugObjects({
functionCode: function(r){ccc=r;/*alert('f:'+r);*/},
evalCode: function(r){bbb=r;/*alert('c:'+r);*/},
converted: function(r){kkk=r;/*alert('c:'+r);*/},
result: function(r){rrr=r;/*alert('r:'+r);*/},
globalsCheck: function(r){ggg=r;/*alert('g:'+r);*/},
errorLog: function(r){eee=r;/*alert('e:'+r);*/},
parseTree: function(r){ttt=r;/*alert('t:'+r);*/},
errorHandler: function(r){eee=r;},
/*
clearTree: function(r){},
onComplete: function( ){},
onStart: function( ){},
*/
doNotEval: white.shift(),
doNotFunctionEval: white.shift(),
doNotMainEval: white.shift()
});
if (white.shift()==true) {
try { bux.runCheck(); } catch(e) { err = "'**ERROR: JSReg.runCheck():'\n// " + e + '\n\n'; }
}
try { bux.eval(src); } catch(e) { err += "'**ERROR: JSReg.eval():'\n// " + e.lineNumber + '\n\n'; }
return err
+ "'given Code:'\n" + src
+ "\n\n'JSReq Code:'\n" + kkk
+ "\n\n'globals.eval Code:'\n" + bbb
+ "\n\n'globals.Function Code:'\n" + ccc
+ "\n\n'Result:'\n" + rrr
// + "\n\n'globalsChecks:'\n" + ggg
+ "\n\n'Errors:'\n" + eee
+ "\n\n'Pase Tree:'\n" + ttt
;
// don't show globalsCheck 'cause it contains EnDe's internal variables too
break;
case 'data' :
kkk = EnDe.User.DAT.guess(src);
return 'data format (' + kkk + '): ' + EnDe.User.DAT.type(kkk) + '<br>';
break;
case 'image' :
bux = 'data:image/'; // data:image/gif;base64, + src
if (src.match(/^\s*data:image\//i)===true) {// lazy check should be ok
// already data:image/*
__dbx('EnDe.Form.dispatch: image: preformated');
return 'image: <img src="' + src + '" ><br><pre>' + src + '</pre>';
} else {
// src may be image data
__dbx('EnDe.Form.dispatch: image: check');
bbb = src.replace(/[\r\n]*/g,'').replace(/%2F/ig,'/').replace(/%3D/ig,'=').replace(/%2B/ig,'+'); // might be URL-encoded
// we could use image/x-icon but we try to give the browser some hints
if (EnDe.B64.isB64(bbb)===true) {
kkk = EnDe.B64.DE.dispatch('base64','','',bbb,'','',0);
__dbx('EnDe.Form.dispatch: image: base64, ');
} else {
bbb = EnDe.B64.EN.dispatch('base64','','',src,'','',0);
kkk = src;
__dbx('EnDe.Form.dispatch: image: not base64, ');
}
kkk = EnDe.User.IMG.guess(kkk);
}
bux = EnDe.User.IMG.getMIME64(kkk);
__dbx('EnDe.Form.dispatch: image: ' + kkk + ' # ' + bux);
// data source must be URL-encoded
bux += bbb.replace(/[\r\n]*/g,'').replace(/\//g,'%2F').replace(/=/g,'%3D').replace(/\+/g,'%2B');
bbb = null; kkk = null;
return 'image: <img src="' + bux + '" ><br><pre>' + bux + '</pre>';
break;
}
return '**ERROR: EnDe.Form.dispatch**'; // fallback
}; // dispatch
}; // EnDe.Form