-
Notifications
You must be signed in to change notification settings - Fork 4
/
Gui.Nav.js
executable file
·1350 lines (1267 loc) · 47.9 KB
/
Gui.Nav.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var BOOKMARKLET = false; //#define BOOKMARKLET
// Buttons source: http://icons.mysitemyway.com/category/simple-black-square-icons/
// http://icons.mysitemyway.com/gallery/post/simple-black-square-icons-natural-wonders/
// http://icons.mysitemyway.com/gallery/post/simple-black-square-icons-culture/
// http://icons.mysitemyway.com/simple-red-square-icons-alphanumeric/
Gui.Nav = function(gui){
this.gui = gui;
this.lastNavPos = {};
this.openFilters = {};
this.createNavContainer();
this.addNavHeader(); // top-right corner of menu
this.addNavCaretInfo();
this.addNavigators();
this.addMenuButtons();
// Z in bottom-right corner
this.createZButton();
};
Gui.Nav.prototype = {
gui: null,
nav: null,
z: null,
navInputLen: null,
minifyButton: null,
beautifyButton: null,
configButton: null,
pragmaButton: null,
treeButton: null,
removeButton: null,
jsdocButton: null,
varButton: null,
configPopup: null,
lastNavPos: null, // array with indices
hasHorizontalBar: null,
hasVerticalBar: null,
openFilters: null,
createNavContainer: function(){
this.nav = document.createElement('div');
this.nav.className = 'zeon-nav';
Gui.css(this.nav, {
position: 'absolute',
bottom: '20px',
right: '20px',
border: '1px solid black',
borderRight: '0px',
borderBottom: '0px',
backgroundColor: 'white',
color: 'black',
fontSize: '13px',
padding: '5px',
WebkitBorderRadius:'10px 0 0 0',
borderRadius: '10px 0 0 0',
WebkitBoxShadow: '-1px -1px 3px #ccc',
boxShadow: '-1px -1px 3px #ccc',
display: 'none',
fontFamily: 'Verdana',
userSelect: 'none',
webkitUserSelect: 'none',
zIndex: 9 // must be higher than highest panel and lower than Z...
});
this.gui.layerContainer.appendChild(this.nav);
this.nav.onclick = this.onNavClick.bind(this);
},
onNavClick: function(e){
var action = e.target.className;
var data = e.target.parentNode.querySelector('.data');
if (action == 'prev' || action == 'next' || action == 'data') {
var name = e.target.parentNode.className;
this.onNavAction(name, action);
} else if (action == 'name') {
var name = e.target.parentNode.parentNode.className;
if (name == 'errors' || name == 'warnings' || name == 'implicitGlobals' || name == 'knownGlobals' || name == 'todofix') {
this.openFilter(name);
}
}
if (e.target.nodeName != 'A') return false; // dont prevent a's
},
addNavHeader: function(){
this.nav.innerHTML = '<a href="http://zeonjs.com/" target="_blank" style="float:right; font-family:monospace; color:black;">Zeon.js</a>';
},
addNavCaretInfo: function(){
// initialization done in .update()
this.navInputLen = document.createElement('div');
this.nav.appendChild(this.navInputLen);
this.navInputLines = document.createElement('div');
this.nav.appendChild(this.navInputLines);
},
addNavigators: function(){
this.navs = document.createElement('div');
this.nav.appendChild(this.navs);
},
addMenuButtons: function(){
this.toolMenu = document.createElement('div');
Gui.css(this.toolMenu, {marginTop: '5px'});
this.nav.appendChild(this.toolMenu);
// menu buttons
this.addMinifyButton();
this.addBeautifyButton();
this.addConfigButton();
if (BOOKMARKLET) {//#ifdef BOOKMARKLET
this.addNavMaxButton();
}//#endif
this.addProtoButton();
this.addSaveButton();
this.addLoadButton();
this.addTreeButton();
this.addPragmaButton();
this.addJsdocButton();
this.addToJsStringButton();
this.addFromJsStringButton();
this.addBookmarkletButton();
this.addVarButton();
this.addTrimButton();
this.addInjectButton();
//#ifdef DEV_BUILD
/* this.addAltMinifyButton(); */
//#endif
this.addDisambiguationButton();
this.addProfilerButton();
this.addBranchButton();
this.addExtractButton();
if (BOOKMARKLET) {//#ifdef BOOKMARKLET
this.addRemoveButton();
}//#endif
this.addFuzzButton();
//#ifdef DEV_BUILD
if (false) this.addBinButton();
//#endif
},
addMinifyButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.minifyButton = this.createTextLink("Minify");
} else { //#elsedef
this.minifyButton = this.createButton('minify', 'Minify');
} //#endif
this.minifyButton.onclick = function(){
var textarea = this.gui.textarea;
var start = textarea.selectionStart;
var stop = textarea.selectionStop;
this.gui.setValue(this.gui.zeon.minify());
textarea.selectionStart = start;
textarea.selectionStop = stop;
textarea.focus();
}.bind(this);
this.toolMenu.appendChild(this.minifyButton);
},
addBeautifyButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.beautifyButton = this.createTextLink("Beautify");
} else { //#elsedef
this.beautifyButton = this.createButton('beautify','Beautify');
} //#endif
this.beautifyButton.onclick = function(){
var textarea = this.gui.textarea;
var start = textarea.selectionStart;
var stop = textarea.selectionStop;
if (this.gui.zeon.btree.length) {
var tree = new Ast(this.gui.zeon.tree, this.gui.zeon.btree); // this will be my new structure in the next iteration
// window.btoken = tree;
// console.log('window.btoken', tree);
this.gui.setValue(tree.beautify(0));
textarea.selectionStart = start;
textarea.selectionStop = stop;
textarea.focus();
}
}.bind(this);
this.toolMenu.appendChild(this.beautifyButton);
},
addConfigButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.configButton = this.createTextLink("Config");
} else { //#elsedef
this.configButton = this.createButton('config','Config');
} //#endif
this.configButton.onclick = function(){
if (this.configPopup) return;
this.configPopup = true;
new Gui.Config(this.gui, function(){ this.configPopup = false; }.bind(this));
}.bind(this);
this.toolMenu.appendChild(this.configButton);
},
//#ifdef BOOKMARKLET
addNavMaxButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
var max = document.createElement('div');
max.className = 'maximize';
Gui.css(max, {position:'absolute',bottom:'58px',right:'2px',margin:'0',padding:'0 3px 1px 3px',cursor:'pointer',fontWeight:'bold',fontSize:'12px',border:'1px solid black',WebkitBorderRadius:'2px',borderRadius:'2px',color:'black',backgroundColor:'white'});
max.innerHTML = '+';
this.nav.appendChild(max);
max.onclick = function(){
var lc = this.gui.layerContainer;
if (lc.parentNode == this.gui.rootContainer) {
Gui.css(lc, {position:'absolute',top:'1%',left:'1%',width:'98%',height:'98%',zIndex:'9000',backgroundColor:'white'});
max.innerHTML = '-';
document.body.appendChild(lc);
} else {
Gui.css(lc, {position:'relative',width:'100%',height:'100%',backgroundColor:'transparent'});
max.innerHTML = '+';
this.gui.rootContainer.appendChild(lc);
}
}.bind(this);
} else {//#elsedef
max = document.createElement('img');
max.src = 'icons/maximize.png';
max.title = max.alt = 'Maximize';
Gui.css(max, {cursor: 'pointer', width: '32px', height: '32px', cssFloat: 'left'});
this.toolMenu.appendChild(max);
max.onclick = function(){
var lc = this.gui.layerContainer;
if (lc.parentNode == this.gui.rootContainer) {
Gui.css(lc, {position:'absolute',top:'1%',left:'1%',width:'98%',height:'98%',zIndex:'9000',backgroundColor:'white'});
max.src = 'icons/minimize.png';
max.title = max.alt = 'Minimize';
document.body.appendChild(lc);
} else {
Gui.css(lc, {position:'relative',width:'100%',height:'100%',backgroundColor:'transparent'});
max.src = 'icons/maximize.png';
max.title = max.alt = 'Maximize';
this.gui.rootContainer.appendChild(lc);
}
}.bind(this);
}//#endif
},
//#endif
addPragmaButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.pragmaButton = this.createTextLink("Process pragmas");
} else { //#elsedef
this.pragmaButton = this.createButton('pragma','Process Pragmas');
} //#endif
this.pragmaButton.onclick = function(){
var tree = this.gui.zeon.wtree; // the stream of tokens
this.processPragmas(tree);
// now update the textarea
this.gui.setValue(tree.map(function(token){ return token.value; }).join(''));
}.bind(this);
this.toolMenu.appendChild(this.pragmaButton);
},
addTreeButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.treeButton = this.createTextLink("Show tree");
} else { //#elsedef
this.treeButton = this.createButton('tree', 'Show parse tree', true);
} //#endif
this.treeButton.onclick = function(_, immediatelyClose){
var root = this.gui.zeon.tree;
var tostring = function(stack,depth){
var arr = [];
for (var i=0; i<stack.length; ++i) {
if (stack[i] instanceof Array) {
var pairs = [];
for (var key in stack[i]) {
if (stack[i].hasOwnProperty(key) && key != +key+'' && !(stack[i][key] instanceof Array)) {
pairs.push(key+':'+Gui.noctrl(Gui.escape(stack[i][key])));
}
}
var s = '\n'+(new Array(depth+1).join('\t'))+'[('+stack[i].length+') ' + pairs.join(', ');
s += tostring(stack[i], depth+1);
s += '\n'+(new Array(depth+1).join('\t'))+']';
arr.push(s);
} else {
var pairs = [];
for (var key in stack[i]) {
if (stack[i].hasOwnProperty(key)) {
if (typeof stack[i][key] == 'object') {
if (stack[i][key] instanceof Array) pairs.push(key+':'+Gui.escape('<arr:'+stack[i][key].length+'>'));
else pairs.push(key+':'+Gui.escape('<obj>'));
}
else pairs.push(key+':'+Gui.noctrl(Gui.escape(stack[i][key])));
}
}
var s = '\n'+(new Array(depth+1).join('\t'))+'{'+pairs.join(', ')+'}';
arr.push(s);
}
}
return arr.join();
}.bind(this);
root.root = true;
var s = tostring([root],0);
var e = document.createElement('pre');
Gui.css(e, {position:'absolute', zIndex:500, backgroundColor:'white', color:'black',padding: '5px', border: '1px solid black', fontSize:'12px' });
e.innerHTML = s;
var f = document.createElement('div');
Gui.css(f, 'cursor', 'pointer');
f.innerHTML = 'Close';
f.onclick = function(){ document.body.removeChild(e); };
e.insertBefore(f, e.firstChild);
document.body.insertBefore(e, document.body.firstChild);
// for fuzz testing...
if (immediatelyClose) f.onclick();
}.bind(this);
this.toolMenu.appendChild(this.treeButton);
},
//#ifdef BOOKMARKLET
addRemoveButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.removeButton = this.createTextLink("Remove Zeon");
} else { //#elsedef
this.removeButton = this.createButton('delete','Remove Zeon');
} //#endif
this.removeButton.onclick = function(){
this.gui.remove();
}.bind(this);
this.toolMenu.appendChild(this.removeButton);
},
//#endif
addJsdocButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.jsdocButton = this.createTextLink("Generate JSDoc");
} else { //#elsedef
this.jsdocButton = this.createButton('jsdocs', 'Generate JSDocs');
} //#endif
this.jsdocButton.onclick = function(){
var tree = this.gui.zeon.wtree;
tree.forEach(function(token, i){
if (token.isFuncDeclKeyword || token.isFuncExprKeyword || token.isVarKeyword) {
// console.log("found", token, token.isVarKeyword,(token.isFuncDeclKeyword || token.isFuncExprKeyword))
if (token.isVarKeyword) {
var jsdoc = this.gui.zeon.generateVarJsdoc(token);
} else {
var jsdoc = this.gui.zeon.generateFunctionJsdoc(token);
}
if (jsdoc) {
// slipstream the jsdoc on the previous line, or inside the existing jsdoc
if (token.jsdoc) {
token.jsdoc.value = jsdoc;
} else {
var line = token.startLineId;
var pos = token.tokposw;
while (pos && tree[--pos].startLineId == line);
if (tree[pos].name == 10/*lineterminator*/) ++pos; // if not start of input, skip newline we should be currently at
// save it in a new property, otherwise the indentation (using .value) might cause a jsdoc to repeatively be reinserted...
if (!tree[pos].futureJsdoc) tree[pos].futureJsdoc = '';
tree[pos].futureJsdoc += jsdoc;
}
}
}
}, this);
// now update the textarea
this.gui.setValue(tree.map(function(token){ return (token.futureJsdoc||'')+token.value; }).join(''));
}.bind(this);
this.toolMenu.appendChild(this.jsdocButton);
//setTimeout(this.jsdocButton.onclick.bind(this), 200);
},
addVarButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.varButton = this.createTextLink("Fix hoisting");
} else { //#elsedef
this.varButton = this.createButton('var','Fix hoisting', true);
} //#endif
this.varButton.onclick = function(){
var tree = this.gui.zeon.wtree;
var treenw = this.gui.zeon.btree;
this.processGlobalVars(tree,treenw);
this.processLocalVars(tree,treenw);
this.removeVarStatements(tree,treenw);
if (this.gui.config['hoisting fix moves func decl to top']) {
this.processFuncDecls(tree,treenw);
}
// now update the textarea
this.gui.setValue(tree.map(function(token){ return (token.varValue || '') + ((typeof token.newValue == 'string') ? token.newValue : token.value); }).join(''));
}.bind(this);
this.toolMenu.appendChild(this.varButton);
},
processGlobalVars: function(tree,treenw){
var vars = this.gui.zeon.globalScope.filter(function(token){
return !token.isEcma && !token.isBrowser && !token.implicit && !(token instanceof Array) && !token.isFuncDecl;
}).map(function(token){
return token.value;
});
if (vars.length) {
// re-indent this statement the same as the next real token
var indent = this.getIndentation(treenw[0], true).map(function(o){ return o.value; }).join('');
// note that we must use , instead of ; because the whole must remain a single statement without having to check for it
tree[0].varValue = indent+'var '+vars.join(', ')+';\n';
tree[0].newValue = tree[0].value;
}
},
processLocalVars: function(tree,treenw){
// now all other scopes (functions)
this.gui.zeon.collects.functions.forEach(function(func){
// for each function, we have to search for the first token of the body.
var curly = func.lhc;
// should we add newlines?
var nonewline = (curly.startLineId == func.rhc.startLineId);
// next token, regardless. we will prepend to that token. so empty body is ok.
next = tree[curly.tokposw+1];
var vars = func.scope.filter(function(token){
return !token.isEcma && !(token instanceof Array) && !token.isFuncDecl && !token.isFuncParameter;
}).map(function(token){
return token.value;
});
if (vars.length) {
if (nonewline) {
// space to make `var` not stick to `{`
next.varValue = ' var '+vars.join(', ')+';';
next.newValue = next.value;
} else {
var indent = this.getIndentation(treenw[curly.tokposb+1], true).map(function(o){ return o.value; }).join('');
next.varValue = '\n'+indent+'var '+vars.join(', ')+';';
next.newValue = next.value;
}
}
}, this);
},
removeVarStatements: function(tree,treenw){
// remove all var statements, refactor initializers
var i = tree.length;
while (i--) {
token = tree[i];
if (token.isVarKeyword) {
if (token.value == 'var') {
token.newValue = '';
var pos = token.tokposw;
while (tree[++pos] && tree[pos].isWhite) tree[pos].newValue = '';
}
// get var stack
var stack = token.stack;
// either the semi colon or the ASI token. we replace the entire var statement like that.
var stop = stack[1];
// get to decl stack. cry fact; token.stack[0][0] == token.
// anyways, we just want the stuff that follows. all decls are grouped in arrays so we just need to filter for arrays now
var decls = stack[0].filter(function(o){
return o instanceof Array;
});
// now each decl will have either one or three elements depending on whether it has an initializer
var initialized = decls.filter(function(decl){
if (decl.length == 3) {
return true;
} else {
// erase non-initialized variable names and a comma before or after that token, if any
this.stackWalk(decl[0], function(token){
if (token.name == 2/*identifier*/) {
token.newValue = '';
if (treenw[token.tokposb+1] && treenw[token.tokposb+1].value == ',') {
treenw[token.tokposb+1].newValue = '';
} else if (token.tokposb && treenw[token.tokposb-1].value == ',') {
treenw[token.tokposb-1].newValue = '';
}
}
}.bind(this));
// now remove the token from this list
return false;
}
}, this);
// remove the close token (semi or asi) if the var only contained non-initialized names
if (initialized.length == 0) {
stop.newValue = '';
}
}
}
},
processFuncDecls: function(tree,treenw){
// get declarations for each scope, back to forward
this.gui.zeon.collects.functions.reverse().map(function(o){ return o.scope; }).concat([this.gui.zeon.globalScope]).forEach(function(scope){
// not all scopes have a function
if (scope.functions) {
// get only the declarations
var decls = scope.functions.filter(function(func){ return func.isFuncDeclKeyword; });
if (decls.length) {
var indent;
if (scope.global) indent = this.getIndentation(treenw[0], true).map(function(o){ return o.value; }).join('');
else indent = this.getIndentation(treenw[scope.scopeFor.lhc.tokposb+1], true).map(function(o){ return o.value; }).join('');
var val = decls.map(function(func){
var index = func.tokposw;
var end = func.rhc.tokposw;
var s = '';
var removedLineTerminator = false;
// remove leading whitespace up and including the line terminator
while (index-- && tree[index].isWhite) {
if (tree[index].name == 9/*WHITE_SPACE*/) {
tree[index].newValue = '';
tree[index].varValue = '';
} else {
if (tree[index].name == 10/*LINETERMINATOR*/) {
tree[index].newValue = '';
tree[index].varValue = '';
removedLineTerminator = true; // only remove one...
}
break;
}
}
index = func.tokposw;
do {
var token = tree[index];
s += (token.varValue || '') + ((typeof token.newValue == 'string') ? token.newValue : token.value);
token.newValue = '';
token.varValue = '';
} while (++index <= end);
return s;
}).reverse().join('\n'+indent);
if (scope.global) {
var first = tree[0];
var suffix = ((typeof first.newValue == 'string') ? first.newValue : first.value);
first.newValue =
(first.varValue || '') +
indent + val +
(suffix?'\n':'') + suffix
;
first.varValue = '';
} else {
var lhc = scope.scopeFor.lhc;
var prefix = ((typeof lhc.newValue == 'string') ? lhc.newValue : lhc.value);
lhc.newValue =
prefix +
'\n' +
(lhc.varValue || '') +
indent + val
;
lhc.varValue = '';
}
}
}
},this);
},
addTrimButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.trimButton = this.createTextLink("Trim trailing whitespace");
} else { //#elsedef
this.trimButton = this.createButton('trim','Trim trailing whitespace');
} //#endif
this.trimButton.onclick = function(){
var filtered = this.gui.zeon.wtree;
var n = filtered.length;
while (n-- && (filtered[n].isWhite || filtered[n].name == 13/*asi*/) && filtered[n].name != 10/*lineterminator*/) filtered[n] = '';
filtered = filtered.map(function(o){ if (o.isTrailingWhitespace) return ''; return o.value; });
this.gui.setValue(filtered.join(''));
}.bind(this);
this.toolMenu.appendChild(this.trimButton);
},
addInjectButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.injectButton = this.createTextLink("Inject calls");
} else { //#elsedef
this.injectButton = this.createButton('inject','Inject calls');
} //#endif
this.injectButton.onclick = function(_, testName){
Ast.injectName = testName || prompt('Enter name of function callback', 'callme') || 'callme';
var ast = new Ast(this.gui.zeon.tree, this.gui.zeon.btree); // this will be my new structure in the next iteration
this.gui.setValue(ast.heatmap());
}.bind(this);
this.toolMenu.appendChild(this.injectButton);
},
//#ifdef DEV_BUILD
addAltMinifyButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.altMinifyButton = this.createTextLink("Alternative minify");
} else { //#elsedef
this.altMinifyButton = this.createButton('minify', 'Alternative minify');
} //#endif
this.altMinifyButton.onclick = function(){
var ast = new Ast(this.gui.zeon.tree, this.gui.zeon.btree); // this will be my new structure in the next iteration
this.gui.setValue(ast.minify());
}.bind(this);
this.toolMenu.appendChild(this.altMinifyButton);
},
//#endif
addProfilerButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.profilerButton = this.createTextLink("Visual profiler");
} else { //#elsedef
this.profilerButton = this.createButton('profiler','Visual profiler',true);
} //#endif
this.profilerButton.onclick = function(){
var fn = 'callme';
Ast.injectName = fn;
var ast = new Ast(this.gui.zeon.tree, this.gui.zeon.btree); // this will be my new structure in the next iteration
var code = ast.heatmap();
// start profiler
var map = [];
var els = [];
window[fn] = function(index){
if (! map[index]) map[index] = 0;
map[index]++;
}.bind(this);
window.t = setTimeout(function(){
// remove previous red markings
els.forEach(function(o){
if (o) o.parentElement.removeChild(o);
});
els = [];
// get highest execution count, this is our ceiling
var max = Math.max.apply(null, map.filter(function(o){
return o;
}));
// add a new marker for every tracked statement
map.forEach(function(n, i){
// we need to determine the length of the statement
// at least up to the next statement.
// so starting at the current token, search for the next statement start,
// log the length. i'm not very bothered with inaccurate tabs here
var treenw = gui.zeon.btree;
var tree = gui.zeon.wtree;
var start = i = treenw[i].tokposw;
var next = tree[++i];
var lastBlack = i;
// get proper token range (we dont want trailing whitespace)
while (next && !next.statementStart) {
if (!next.isWhite) lastBlack = i;
next = tree[++i];
}
// now with the range, collect the lens
i = start;
var len = tree[i].len;
while (i++<lastBlack) len += tree[i].len;
// use the len for the length of the statement. multi-line statements are going to cause a problem...
var css = {opacity:(n / max), width:Math.floor(len * gui.fontSize.w) + 'px', height:'15px', 'padding-top':'5px', 'background-color':'red', 'z-index':'0'};
var bubble = gui.bubbleNoSheet('', tree[start], css, 7, 0, true);
els[els.length] = bubble;
}, this);
}.bind(this), 1000);
// run code
var e = document.createElement('script');
e.text = code;
document.body.appendChild(e);
document.body.removeChild(e);
}.bind(this);
this.toolMenu.appendChild(this.profilerButton);
},
addBranchButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.branchButton = this.createTextLink("Show branching");
} else { //#elsedef
this.branchButton = this.createButton('branch','Show branching');
} //#endif
this.branchButton.onclick = function walk(){
var ast = new Ast(this.gui.zeon.tree, this.gui.zeon.btree);
var s = ast.branch();
this.gui.setValue(s);
}.bind(this);
this.toolMenu.appendChild(this.branchButton);
},
addExtractButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.extractButton = this.createTextLink("Extract method");
} else { //#elsedef
this.extractButton = this.createButton('extract','Extract method');
} //#endif
this.extractButton.onclick = function(){
var ta = this.gui.textarea;
var start = ta.selectionStart;
var end = ta.selectionEnd;
//console.log("range", start, end)
// start token should be set by automated zeon systems, so check that first...
var startToken = this.gui.computeCaretPosAt(start);
var endToken = this.gui.computeCaretPosAt(end-1);
// start and end might not be found for whatever reason, if they are equal it regards the same token and we dont do anything
if (startToken && endToken && startToken != endToken) {
// now maybe do some validity checks? i think start and end token must be within same indentation level
var tree = this.gui.zeon.wtree;
var newName = 'newFunction';
var indentation = this.getIndentation(startToken).map(function(o){ return o.value; }).join('');
var index = startToken.tokposw;
// find var usages and declarations
var vars = [];
var decls = [];
var scopes = [];
while (index < endToken.tokposw) {
var current = tree[index];
// ignore white
if (current.isWhite) {}
// regular variable usages
else if (
current.meta == 'lead value' &&
current.name == 2/*identifier*/ &&
current.value != 'true' &&
current.value != 'false' &&
current.value != 'null' &&
current.value != 'this' &&
!current.trackingObject.implicit &&
!current.trackingObject.isEcma &&
!vars.some(function(o){ if (!o.wasAssignedSomething) o.wasAssignedSomething = current.wasAssignedSomething; return o.trackingObject == current.trackingObject; })
) {
vars.push(current);
}
// declared variables
else if (
current.meta == 'var name' &&
!decls.some(function(o){ return o.trackingObject == current.trackingObject; }) &&
!scopes.some(function(o){ return o == current.targetScope; })
) {
decls.push(current);
}
// functions (collect their scopes to discard vars declared in inner scopes)
else if (
current.value == 'function' &&
current.scope &&
scopes.indexOf(current.scope) < 0
) {
scopes.push(current.scope);
}
++index;
}
// remove any vars that were declared in this block or where declared in an inner scope
vars = vars.filter(function(o){ return scopes.indexOf(o.targetScope) < 0 && !decls.some(function(d){ return d.trackingObject == o.trackingObject; }); });
// only return those parameters which were changed. if a variable that was passed on did not change, we dont need to return it (because the outer scope variable will be the same var, regardless)
var assignees = vars.filter(function(o){ return o.wasAssignedSomething; });
// determine variables to return (may not be equal the original collection of vars, so it will not help to slice that array first...)
var toReturn = assignees.concat(decls);
// add prefix and postfix to create the function
startToken.newValue = 'var '+newName+' = function('+vars.map(function(o){ return o.value; }).join(', ')+'){\n'+indentation+'\t'+startToken.value;
// only return existing and new variables in this scope. we dont have to deal with inner scopes
if (toReturn.length == 1) {
endToken.value += '\n'+indentation+'\treturn '+toReturn[0].value+';';
} else if (toReturn.length) {
endToken.value += '\n'+indentation+'\treturn ['+toReturn.map(function(o){ return o.value; }).join(', ')+'];';
}
endToken.value += '\n'+indentation+'};\n'+indentation;
// if one value returned, process that here
if (toReturn.length == 1) {
if (decls.length) endToken.value += 'var '+decls[0].value+' = ';
else endToken.value += vars[0].value+' = ';
}
// if there were more, an array is returned, save it in a temp return value
else if (toReturn.length > 1) endToken.value += 'var tmpReturnValue = ';
// add new function call
endToken.value += newName+'('+vars.map(function(o){ return o.value; }).join(', ')+');';
// if multiple vars were returned, declare them here...
var returnIndex = 0;
if (decls.length && toReturn.length > 1) {
endToken.value += '\n'+indentation+decls.map(function(o){ return 'var '+o.value+' = tmpReturnValue['+(returnIndex++)+'];'}).join('\n'+indentation);
}
// copy new value to existing variables
if (assignees.length && toReturn.length > 1) {
endToken.value += '\n'+indentation+assignees.map(function(o){ return o.value+' = tmpReturnValue['+(returnIndex++)+'];'}).join('\n'+indentation);
}
// add one level of indentation to the (new) function body
var i = startToken.tokposw;
while (++i<endToken.tokposw) if (tree[i].name == 10/*lineterminator*/) tree[i].value += '\t';
// create new output and let it parse (take newValue if it exists, to prevent first token.value to mess up rest)
this.gui.setValue(this.gui.zeon.wtree.map(function(o){ return o.newValue || o.value; }).join(''));
}
}.bind(this);
this.toolMenu.appendChild(this.extractButton);
/*
setTimeout(function(){
this.gui.textarea.selectionStart = 330;
this.gui.textarea.selectionEnd = 568;
this.extractButton.onclick();
}.bind(this), 500);
*/
},
addDisambiguationButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.disambiguationButton = this.createTextLink("Disambiguate operators");
} else { //#elsedef
this.disambiguationButton = this.createButton('disambiguation','Disambiguate operators');
} //#endif
this.disambiguationButton.onclick = function(){
// stack tree
var tree = this.gui.zeon.tree;
// find all operators marked isAmbiguous. wrap their operands in parens.
this.gui.zeon.disambiguate();
// update output
this.gui.setValue(this.gui.zeon.wtree.map(function(o){ return o.value; }).join(''));
}.bind(this);
this.toolMenu.appendChild(this.disambiguationButton);
//setTimeout(this.disambiguationButton.onclick.bind(this),500);
},
addToJsStringButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.toJsstringButton = this.createTextLink("To JS string");
} else { //#elsedef
this.toJsstringButton = this.createButton('tojsstring','To JS string');
} //#endif
this.toJsstringButton.onclick = function(){
this.gui.setValue("'"+this.gui.getValue().replace(/\\/g,'\\\\').replace(/\n/g,'\\n').replace(/\t/g,'\\t').replace(/'/g,"\\'")+"'");
}.bind(this);
this.toolMenu.appendChild(this.toJsstringButton);
},
addFromJsStringButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.toJsstringButton = this.createTextLink("From JS string");
} else { //#elsedef
this.toJsstringButton = this.createButton('fromjsstring','From JS string');
} //#endif
this.toJsstringButton.onclick = function(){
var val = this.gui.getValue();
val = val.slice(1,-1).replace(/\\(.)/g,function(s, a){
if (a == 'n') return '\n';
if (a == 't') return '\t';
if (a== 'r') return '\r';
return a;
});
this.gui.setValue(val);
}.bind(this);
this.toolMenu.appendChild(this.toJsstringButton);
},
addBookmarkletButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.bookmarkletButton = this.createTextLink("To bookmarklet");
} else { //#elsedef
this.bookmarkletButton = this.createButton('bookmarklet','To bookmarklet');
} //#endif
this.bookmarkletButton.onclick = function(){
this.gui.setValue('javascript:'+this.gui.zeon.minify(true, true)); // no reduction, no newlines
}.bind(this);
this.toolMenu.appendChild(this.bookmarkletButton);
},
addSaveButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.saveButton = this.createTextLink("Save");
} else { //#elsedef
this.saveButton = this.createButton('save','Save');
} //#endif
this.saveButton.onclick = function(){
localStorage.setItem('doc', this.gui.getValue());
}.bind(this);
this.toolMenu.appendChild(this.saveButton);
},
addLoadButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.loadButton = this.createTextLink("Load");
} else { //#elsedef
this.loadButton = this.createButton('load','Load');
} //#endif
this.loadButton.onclick = function(){
this.gui.setValue(localStorage.getItem('doc'));
}.bind(this);
this.toolMenu.appendChild(this.loadButton);
},
addProtoButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.protoButton = this.createTextLink("Declare prototype properties");
} else { //#elsedef
this.protoButton = this.createButton('proto','Declare prototype properties');
} //#endif
this.protoButton.onclick = function(){
this.gui.zeon.btree.forEach(function(token){
if (token.leadValue && token.value == 'this' && token.leadValueTarget !== true && token.trackingObject && token.trackingObject.properties && token.targetScope && token.targetScope.scopeFor) {
var func = token.targetScope.scopeFor.functionStack;
if (func) {
var props = token.trackingObject.properties;
Object.keys(props).forEach(function(name){
if (!props[name].isDeclaredOnProto && !props[name].cannotDeterminePrototype) {
var constrName = 'fixme/*unable to determine name*/';
if (func.assignedToObjLitProperty) {
var obj = func.assignedToObjLitProperty.isPropertyOf;
} else {
if (func.assignedToLead && func.assignedToLead.isPropertyOf && func.assignedToLead.isPropertyOf.value == 'prototype') {
var proto = func.assignedToLead.isPropertyOf.trackingObject;
// yes, we can add more trickery and fetch the constructorname... but really.
} else if (func.assignedToLead && func.assignedToLead.trackingObject && func.assignedToLead.trackingObject.properties && func.assignedToLead.trackingObject.properties.prototype) {
var proto = func.assignedToLead.trackingObject.properties.prototype;
constrName = func[0].constructorName;
}
if (proto && proto.assignedObjLit) {
var obj = proto.assignedObjLit;
}
}
if (obj) {
if (!obj.definedProperties[name]) {
obj.value = obj.value + name + ':null,';
obj.definedProperties[name] = true;
}
} else if (proto) {
if (func && func[0] && func[0].rhc) {
var last = func[0].rhc;
if (func[0].isFuncExprKeyword && this.gui.zeon.btree[last.tokposb+1]) last = this.gui.zeon.btree[last.tokposb+1];
last.value += '\n'+constrName+'.prototype.'+name+' = null;';
}
}
}
},this);
}
}
},this);
this.gui.setValue(this.gui.zeon.wtree.map(function(o){ return o.value; }).join(''));
}.bind(this);
this.toolMenu.appendChild(this.protoButton);
//setTimeout(this.protoButton.onclick, 500)
},
addFuzzButton: function(){
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.fuzzButton = this.createTextLink("Start fuzzer");
} else { //#elsedef
this.fuzzButton = this.createButton('fuzz','Start fuzzer');
} //#endif
var options = [
function(){ this.minifyButton.onclick(); }.bind(this), // 0
function(){ this.beautifyButton.onclick(); }.bind(this), // 1
function(){ this.pragmaButton.onclick(); }.bind(this), // 2
function(){ this.treeButton.onclick(null,true); }.bind(this), // 3 (immediately closes)
function(){ this.jsdocButton.onclick(); }.bind(this), // 4
function(){ this.varButton.onclick(); }.bind(this), // 5
function(){ this.trimButton.onclick(); }.bind(this), // 6
function(){ this.injectButton.onclick(null,'fuzztest'); }.bind(this), // 5
function(){ this.branchButton.onclick(); }.bind(this), // 7
function(){ this.disambiguationButton.onclick }.bind(this), // 8
function(){ this.toJsstringButton.onclick(); }.bind(this), // 9
function(){ this.bookmarkletButton.onclick(); }.bind(this), // 10
function(){ this.saveButton.onclick(); }.bind(this), // 11
function(){ this.loadButton.onclick(); }.bind(this), // 12
function(){ this.protoButton.onclick(); }.bind(this) // 13
];
this.fuzzButton.onclick = function(){
if (this.fuzzTimer) {
clearInterval(this.fuzzTimer);
this.fuzzTimer = 0;
} else {
var toolsWithErrors = false;
this.fuzzTimer = setInterval(function(){
var code = false;
while (!code) {
try {
var next = Math.floor(Math.random()*4);
switch (next) {
case 0:
var code = fuzzRuderManOrg(19);
break;
case 1:
var code = fuzzRudermanMod(19);
break;
case 2:
var code = crapFuzzer();
break;
default:
var code = fuzzZee();
break;
}
} catch (e){}
}
try {
var screwed = ['bug', 'in zeon', code];
if (toolsWithErrors) this.gui.setValue(code.slice(3)); // cause a lot of errors
else this.gui.setValue(code);
this.gui.update(true);
if (toolsWithErrors || !this.gui.zeon.hasError) {
var lastId = Math.floor(Math.random()*options.length);
var screwed = ['bug', lastId, code];
options[lastId]();
}
screwed = null;
} finally {
if (screwed) {
setTimeout(function(){ console.log(screwed); },10);
}
}
}.bind(this), 10);
}
}.bind(this);
this.toolMenu.appendChild(this.fuzzButton);
},
//#ifdef DEV_BUILD
addBinButton: function(){ // just a test..
/*
if (BOOKMARKLET) { //#ifdef BOOKMARKLET
this.binButton = this.createTextLink("Create binary");
} else { //#elsedef
this.binButton = this.createButton('bin','Create binary',true);