forked from firebug/firebug
/
commandLine.js
1433 lines (1189 loc) · 47.1 KB
/
commandLine.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
/* See license.txt for terms of usage */
define([
"firebug/lib/object",
"firebug/firebug",
"firebug/chrome/reps",
"firebug/lib/locale",
"firebug/lib/events",
"firebug/lib/wrapper",
"firebug/lib/url",
"firebug/lib/css",
"firebug/lib/dom",
"firebug/chrome/firefox",
"firebug/chrome/window",
"firebug/lib/system",
"firebug/lib/xpath",
"firebug/lib/string",
"firebug/lib/xml",
"firebug/lib/array",
"firebug/lib/persist",
"firebug/console/eventMonitor",
"firebug/lib/keywords",
"firebug/console/console",
"firebug/console/commandLineHelp",
"firebug/console/commandLineExposed",
"firebug/console/autoCompleter",
"firebug/console/commandHistory"
],
function(Obj, Firebug, FirebugReps, Locale, Events, Wrapper, Url, Css, Dom, Firefox, Win, System,
Xpath, Str, Xml, Arr, Persist, EventMonitor, Keywords, Console, CommandLineHelp) {
// ********************************************************************************************* //
// Constants
const Cc = Components.classes;
const Ci = Components.interfaces;
const commandPrefix = ">>>";
const reCmdSource = /^with\(_FirebugCommandLine\){(.*)};$/;
// ********************************************************************************************* //
// Command Line
Firebug.CommandLine = Obj.extend(Firebug.Module,
{
dispatchName: "commandLine",
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// targetWindow was needed by evaluateInSandbox, let's leave it for a while in case
// we rethink this yet again
initializeCommandLineIfNeeded: function (context, win)
{
if (!context || !win)
return;
// The command-line requires that the console has been initialized first,
// so make sure that's so. This call should have no effect if the console
// is already initialized.
var consoleIsReady = Firebug.Console.isReadyElsePreparing(context, win);
// Make sure the command-line is initialized. This call should have no
// effect if the command-line is already initialized.
var commandLineIsReady = Firebug.CommandLine.isReadyElsePreparing(context, win);
if (FBTrace.DBG_COMMANDLINE)
{
FBTrace.sysout("commandLine.initializeCommandLineIfNeeded console ready: " +
consoleIsReady + " commandLine ready: " + commandLineIsReady);
}
},
// returns user-level wrapped object I guess.
evaluate: function(expr, context, thisValue, targetWindow, successConsoleFunction,
exceptionFunction, noStateChange)
{
if (!context)
return;
try
{
var result = null;
var debuggerState = Firebug.Debugger.beginInternalOperation();
if (this.isSandbox(context))
{
result = this.evaluateInSandbox(expr, context, thisValue, targetWindow,
successConsoleFunction, exceptionFunction);
}
else if (Firebug.Debugger.hasValidStack(context))
{
result = this.evaluateInDebugFrame(expr, context, thisValue, targetWindow,
successConsoleFunction, exceptionFunction);
}
else
{
result = this.evaluateByEventPassing(expr, context, thisValue, targetWindow,
successConsoleFunction, exceptionFunction);
}
if (!noStateChange)
context.invalidatePanels("dom", "html");
}
catch (exc)
{
// XXX jjb, I don't expect this to be taken, the try here is for the finally
if (FBTrace.DBG_ERRORS && FBTrace.DBG_COMMANDLINE)
{
FBTrace.sysout("commandLine.evaluate with context.stopped:" + context.stopped +
" EXCEPTION " + exc, exc);
}
}
finally
{
Firebug.Debugger.endInternalOperation(debuggerState);
}
return result;
},
evaluateByEventPassing: function(expr, context, thisValue, targetWindow,
successConsoleFunction, exceptionFunction)
{
var win = targetWindow ? targetWindow :
(context.baseWindow ? context.baseWindow : context.window);
if (!win)
{
if (FBTrace.DBG_ERRORS && FBTrace.DBG_COMMANDLINE)
FBTrace.sysout("commandLine.evaluateByEventPassing: no targetWindow!");
return;
}
//xxxHonza: do not detach the command line here. In case where Firebug is
// halted in the debugger and debugging a function executed in the command line
// the command line handler needs to be yet used to display the return value.
// Inject commandLine APIs again.
this.initializeCommandLineIfNeeded(context, win);
// Make sure the command line script is attached.
if (!Firebug.CommandLine.isAttached(context, win))
{
FBTrace.sysout("commandLine: document does not have command line attached " +
"it's too early for command line "+Win.getWindowId(win)+" location:"+
Win.safeGetWindowLocation(win), document);
if (Xml.isXMLPrettyPrint(context, win))
{
var msg = Locale.$STR("commandline.disabledForXMLDocs");
var row = Firebug.Console.logFormatted([msg], context, "warn", true);
var objectBox = row.querySelector(".objectBox");
// Log a message with a clickable link that can be used to enable
// the command line - but the page will switch into HTML. The listener
// passed into the function is called when the user clicks the link.
FirebugReps.Description.render(msg, objectBox, Obj.bind(function()
{
// Reset the flag that protect script injection into the page.
context.isXMLPrettyPrint = false;
// Now inject the command line.
Firebug.CommandLine.initializeCommandLineIfNeeded(context, win);
}, this));
}
else
{
Firebug.Console.logFormatted(["Firebug cannot find firebug-CommandLineAttached " +
"through document.getUserData, it is too early for command line",
win], context, "error", true);
}
return;
}
var event = document.createEvent("Events");
event.initEvent("firebugCommandLine", true, false);
win.document.setUserData("firebug-methodName", "evaluate", null);
expr = expr.toString();
expr = "with(_FirebugCommandLine){\n" + expr + "\n};";
win.document.setUserData("firebug-expr", expr, null);
var consoleHandler = Firebug.Console.injector.getConsoleHandler(context, win);
if (!consoleHandler)
{
FBTrace.sysout("commandLine evaluateByEventPassing no consoleHandler "+
Win.safeGetWindowLocation(win));
return;
}
if (successConsoleFunction)
{
consoleHandler.setEvaluatedCallback( function useConsoleFunction(result)
{
var ignoreReturnValue = Console.getDefaultReturnValue(win);
if (result === ignoreReturnValue)
return;
successConsoleFunction(result, context);
});
}
if (exceptionFunction)
{
consoleHandler.setEvaluateErrorCallback(function useExceptionFunction(result)
{
exceptionFunction(result, context, "errorMessage");
});
}
else
{
consoleHandler.setEvaluateErrorCallback(function useErrorFunction(result)
{
if (result)
{
var m = reCmdSource.exec(result.source);
if (m && m.length > 0)
result.source = m[1];
}
Firebug.Console.logFormatted([result], context, "error", true);
});
}
if (FBTrace.DBG_COMMANDLINE)
{
FBTrace.sysout("commandLine.evaluateByEventPassing '" + expr +
"' using consoleHandler:", consoleHandler);
}
try
{
win.document.dispatchEvent(event);
// Clean up the command line APIs.
Firebug.CommandLine.injector.detachCommandLine(context, win);
}
catch(exc)
{
if (FBTrace.DBG_COMMANDLINE || FBTrace.DBG_ERRORS)
FBTrace.sysout("commandLine.evaluateByEventPassing dispatchEvent FAILS " + exc,
{exc:exc, event:event});
}
if (FBTrace.DBG_COMMANDLINE)
{
FBTrace.sysout("commandLine.evaluateByEventPassing return after firebugCommandLine " +
"event:", event);
}
},
evaluateInDebugFrame: function(expr, context, thisValue, targetWindow,
successConsoleFunction, exceptionFunction)
{
var result = null;
// targetWindow may be frame in HTML
var win = targetWindow ? targetWindow :
(context.baseWindow ? context.baseWindow : context.window);
if (!context.commandLineAPI)
context.commandLineAPI = new FirebugCommandLineAPI(context);
var htmlPanel = context.getPanel("html", true);
var scope = {
api : context.commandLineAPI,
vars : htmlPanel?htmlPanel.getInspectorVars():null,
thisValue : thisValue
};
try
{
result = Firebug.Debugger.evaluate(expr, context, scope);
successConsoleFunction(result, context);
}
catch (e)
{
exceptionFunction(e, context);
}
return result;
},
evaluateByPostMessage: function(expr, context, thisValue, targetWindow,
successConsoleFunction, exceptionFunction)
{
// targetWindow may be frame in HTML
var win = targetWindow ? targetWindow :
(context.baseWindow ? context.baseWindow : context.window);
if (!win)
{
if (FBTrace.DBG_ERRORS && FBTrace.DBG_COMMANDLINE)
FBTrace.sysout("commandLine.evaluateByPostMessage: no targetWindow!");
return;
}
// We're going to use some command-line facilities, but it may not have initialized yet.
this.initializeCommandLineIfNeeded(context, win);
expr = expr.toString();
expr = "with(_FirebugCommandLine){\n" + expr + "\n};";
var consoleHandler = Firebug.Console.injector.getConsoleHandler(context, win);
if (!consoleHandler)
{
FBTrace.sysout("commandLine evaluateByPostMessage no consoleHandler "+
Win.safeGetWindowLocation(win));
return;
}
if (successConsoleFunction)
{
consoleHandler.setEvaluatedCallback( function useConsoleFunction(result)
{
var ignoreReturnValue = Console.getDefaultReturnValue(win);
if (result === ignoreReturnValue)
return;
successConsoleFunction(result, context);
});
}
if (exceptionFunction)
{
consoleHandler.evaluateError = function useExceptionFunction(result)
{
exceptionFunction(result, context, "errorMessage");
}
}
else
{
consoleHandler.evaluateError = function useErrorFunction(result)
{
if (result)
{
var m = reCmdSource.exec(result.source);
if (m && m.length > 0)
result.source = m[1];
}
Firebug.Console.logFormatted([result], context, "error", true);
}
}
return win.postMessage(expr, "*");
},
evaluateInWebPage: function(expr, context, targetWindow)
{
var win = targetWindow || context.window;
var element = Dom.addScript(win.document, "_firebugInWebPage", expr);
if (!element)
return;
setTimeout(function delayRemoveScriptTag()
{
// we don't need the script element, result is in DOM object
if (element.parentNode)
element.parentNode.removeChild(element);
});
return "true";
},
// isSandbox(context) true, => context.global is a Sandbox
evaluateInSandbox: function(expr, context, thisValue, targetWindow, successConsoleFunction,
exceptionFunction)
{
var result,
scriptToEval = expr;
try
{
result = Components.utils.evalInSandbox(scriptToEval, context.global);
if (FBTrace.DBG_COMMANDLINE)
FBTrace.sysout("commandLine.evaluateInSandbox success for sandbox ", scriptToEval);
successConsoleFunction(result, context);
}
catch (e)
{
if (FBTrace.DBG_ERRORS && FBTrace.DBG_COMMANDLINE)
FBTrace.sysout("commandLine.evaluateInSandbox FAILED in "+context.getName()+
" because "+e, e);
exceptionFunction(e, context);
result = new FirebugReps.ErrorMessageObj("commandLine.evaluateInSandbox FAILED: " + e,
Url.getDataURLForContent(scriptToEval, "FirebugCommandLineEvaluate"),
e.lineNumber, 0, "js", context, null);
}
return result;
},
isSandbox: function (context)
{
return (context.global && context.global+"" === "[object Sandbox]");
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
enter: function(context, command)
{
var expr = command ? command : this.getExpression(context);
if (expr == "")
return;
var mozJSEnabled = Firebug.Options.getPref("javascript", "enabled");
if (!mozJSEnabled)
{
Firebug.Console.log(Locale.$STR("console.JSDisabledInFirefoxPrefs"), context, "info");
return;
}
if (!Firebug.commandEditor || context.panelName != "console")
{
this.clear(context);
Firebug.Console.log(commandPrefix + " " + expr, context, "command", FirebugReps.Text);
}
else
{
var shortExpr = Str.cropString(Str.stripNewLines(expr), 100);
Firebug.Console.log(commandPrefix + " " + shortExpr, context, "command",
FirebugReps.Text);
}
this.commandHistory.appendToHistory(expr);
var noscript = getNoScript();
if (noscript)
{
var currentURI = Firefox.getCurrentURI();
var noScriptURI = currentURI ? noscript.getSite(currentURI.spec) : null;
if (noScriptURI)
noScriptURI = (noscript.jsEnabled || noscript.isJSEnabled(noScriptURI)) ?
null : noScriptURI;
}
if (noscript && noScriptURI)
noscript.setJSEnabled(noScriptURI, true);
var goodOrBad = Obj.bind(Firebug.Console.log, Firebug.Console);
this.evaluate(expr, context, null, null, goodOrBad, goodOrBad);
if (noscript && noScriptURI)
noscript.setJSEnabled(noScriptURI, false);
},
enterInspect: function(context)
{
var expr = this.getCommandLine(context).value;
if (expr == "")
return;
this.clear(context);
this.commandHistory.appendToHistory(expr);
this.evaluate(expr, context, null, null, function(result, context)
{
if (typeof(result) != undefined)
Firebug.chrome.select(result);
});
},
reenter: function(context)
{
var command = this.commandHistory.getLastCommand();
this.enter(context, command);
},
copyBookmarklet: function(context)
{
// XXXsilin: This needs escaping, and stripNewLines is exactly the
// wrong thing to do when it comes to JavaScript.
var commandLine = this.getCommandLine(context);
var expr = "javascript: " + Str.stripNewLines(commandLine.value);
System.copyToClipboard(expr);
},
focus: function(context)
{
if (Firebug.isDetached())
Firebug.chrome.focus();
else
Firebug.toggleBar(true);
var commandLine = this.getCommandLine(context);
if (!context.panelName)
{
Firebug.chrome.selectPanel("console");
}
else if (context.panelName != "console")
{
this.Popup.toggle(Firebug.currentContext);
setTimeout(function() { commandLine.select(); });
}
else
{
// We are already on the console, if the command line has also
// the focus, toggle back. But only if the UI has been already
// opened.
if (commandLine.getAttribute("focused") != "true")
setTimeout(function() { commandLine.select(); });
}
},
clear: function(context)
{
var commandLine = this.getCommandLine(context);
if (commandLine.value)
{
commandLine.value = "";
this.autoCompleter.hide();
this.update(context);
return true;
}
return false;
},
cancel: function(context)
{
return this.clear(context);
},
update: function(context)
{
var commandLine = this.getCommandLine(context);
context.commandLineText = commandLine.value;
},
// xxxsz: setMultiLine should just be called when switching between Command Line
// and Command Editor
// xxxHonza: it is called for me when switching between the Command Line and
// Command Editor
setMultiLine: function(multiLine, chrome, saveMultiLine)
{
var context = Firebug.currentContext;
if (FBTrace.DBG_COMMANDLINE)
{
FBTrace.sysout("commandLine.setMultiline; multiLine: " + multiLine + " for: " +
(context ? context.getName() : "no contet"));
}
if (context && context.panelName != "console")
return;
Dom.collapse(chrome.$("fbCommandBox"), multiLine);
Dom.collapse(chrome.$("fbPanelSplitter"), !multiLine);
Dom.collapse(chrome.$("fbSidePanelDeck"), !multiLine);
if (multiLine)
chrome.$("fbSidePanelDeck").selectedPanel = chrome.$("fbCommandEditorBox");
var commandLine = this.getSingleRowCommandLine();
var commandEditor = this.getCommandEditor();
if (saveMultiLine) // we are just closing the view
{
commandLine.value = commandEditor.value;
return;
}
if (context)
{
var text = context.commandLineText || "";
context.commandLineText = text;
if (multiLine)
commandEditor.value = Str.cleanIndentation(text);
else
commandLine.value = Str.stripNewLines(text);
this.setAutoCompleter();
}
// else we may be hiding a panel while turning Firebug off
},
toggleMultiLine: function(forceCommandEditor)
{
var showCommandEditor = forceCommandEditor || !Firebug.commandEditor;
if (showCommandEditor != Firebug.commandEditor)
Firebug.Options.set("commandEditor", showCommandEditor);
},
checkOverflow: function(context)
{
if (!context)
return;
var commandLine = this.getCommandLine(context);
if (commandLine.value.indexOf("\n") >= 0)
{
setTimeout(Obj.bindFixed(function()
{
Firebug.Options.set("commandEditor", true);
// Switch to the Console panel, where the multiline command line
// is actually displayed. This should be improved see issue 5146
Firebug.chrome.selectPanel("console");
}, this));
}
},
onCommandLineOverflow: function(event)
{
this.checkOverflow(Firebug.currentContext);
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// extends Module
initialize: function()
{
Firebug.Module.initialize.apply(this, arguments);
this.autoCompleter = new Firebug.EmptyJSAutoCompleter();
this.commandHistory = new Firebug.CommandHistory();
if (Firebug.commandEditor)
this.setMultiLine(true, Firebug.chrome);
},
setAutoCompleter: function()
{
var context = Firebug.currentContext;
// xxxHonza: see http://code.google.com/p/fbug/issues/detail?id=4901#c21
if (this.autoCompleter)
this.autoCompleter.shutdown();
// Set the auto-completer even if the command-editor is currently displayed
// in the Console panel. This is to make the auto-completion work even in
// the small-command-line available on other panels (see issue 5006).
if (!context/* || Firebug.commandEditor*/)
{
this.autoCompleter = new Firebug.EmptyJSAutoCompleter();
}
else
{
// Always create the auto-completer for the single command line.
var commandLine = this.getSingleRowCommandLine();
var completionBox = this.getCompletionBox();
var options = {
showCompletionPopup: Firebug.Options.get("commandLineShowCompleterPopup"),
completionPopup: Firebug.chrome.$("fbCommandLineCompletionList"),
tabWarnings: true,
includeCurrentScope: true
};
this.autoCompleter = new Firebug.JSAutoCompleter(commandLine,
completionBox, options);
}
},
initializeUI: function()
{
this.onCommandLineFocus = Obj.bind(this.onCommandLineFocus, this);
this.onCommandLineInput = Obj.bind(this.onCommandLineInput, this);
this.onCommandLineBlur = Obj.bind(this.onCommandLineBlur, this);
this.onCommandLineKeyUp = Obj.bind(this.onCommandLineKeyUp, this);
this.onCommandLineKeyDown = Obj.bind(this.onCommandLineKeyDown, this);
this.onCommandLineKeyPress = Obj.bind(this.onCommandLineKeyPress, this);
this.onCommandLineOverflow = Obj.bind(this.onCommandLineOverflow, this);
this.attachListeners();
},
attachListeners: function()
{
var commandLine = this.getSingleRowCommandLine();
Events.addEventListener(commandLine, "focus", this.onCommandLineFocus, true);
Events.addEventListener(commandLine, "input", this.onCommandLineInput, true);
Events.addEventListener(commandLine, "overflow", this.onCommandLineOverflow, true);
Events.addEventListener(commandLine, "keyup", this.onCommandLineKeyUp, true);
Events.addEventListener(commandLine, "keydown", this.onCommandLineKeyDown, true);
Events.addEventListener(commandLine, "keypress", this.onCommandLineKeyPress, true);
Events.addEventListener(commandLine, "blur", this.onCommandLineBlur, true);
Firebug.Console.addListener(this); // to get onConsoleInjected
},
shutdown: function()
{
var commandLine = this.getSingleRowCommandLine();
// Make sure all listeners registered by the auto completer are removed.
if (this.autoCompleter)
this.autoCompleter.shutdown();
if (this.commandHistory)
this.commandHistory.detachListeners();
Events.removeEventListener(commandLine, "focus", this.onCommandLineFocus, true);
Events.removeEventListener(commandLine, "input", this.onCommandLineInput, true);
Events.removeEventListener(commandLine, "overflow", this.onCommandLineOverflow, true);
Events.removeEventListener(commandLine, "keyup", this.onCommandLineKeyUp, true);
Events.removeEventListener(commandLine, "keydown", this.onCommandLineKeyDown, true);
Events.removeEventListener(commandLine, "keypress", this.onCommandLineKeyPress, true);
Events.removeEventListener(commandLine, "blur", this.onCommandLineBlur, true);
},
destroyContext: function(context, persistedState)
{
var panelState = Persist.getPersistedState(this, "console");
panelState.commandLineText = context.commandLineText;
var commandLine = this.getCommandLine(context);
commandLine.value = "";
this.autoCompleter.hide();
Persist.persistObjects(this, panelState);
// more of our work is done in the Console
// All command line handlers should be removed at this moment.
for (var handler in context.activeCommandLineHandlers)
{
FBTrace.sysout("commandLine.destroyContext; ERROR active commandlinehandler for: " +
context.getName());
}
},
showPanel: function(browser, panel)
{
if (!Firebug.currentContext)
return;
var chrome = Firebug.chrome;
var panelState = Persist.getPersistedState(this, "console");
if (panelState.commandLineText)
{
var value = panelState.commandLineText;
var commandLine = this.getCommandLine(browser);
Firebug.currentContext.commandLineText = value;
commandLine.value = value;
// We don't need the persistent value in this session/context any more. The showPanel
// method is called every time the panel is selected and the text could have been
// changed in this session/context already.
delete panelState.commandLineText;
}
this.autoCompleter.hide();
},
updateOption: function(name, value)
{
if (name == "commandEditor")
this.setMultiLine(value, Firebug.chrome);
else if (name == "commandLineShowCompleterPopup")
this.setAutoCompleter();
},
// called by users of command line, currently:
// 1) Console on focus command line,
// 2) Watch onfocus, and
// 3) debugger loadedContext if watches exist
isReadyElsePreparing: function(context, win)
{
if (FBTrace.DBG_COMMANDLINE)
{
FBTrace.sysout("commandLine.isReadyElsePreparing " + context.getName() + " win: " +
(win ? win.location : "not given"), context);
}
if (this.isSandbox(context))
return;
if (Xml.isXMLPrettyPrint(context, win))
return false;
if (win)
{
Firebug.CommandLine.injector.attachCommandLine(context, win);
}
else
{
Firebug.CommandLine.injector.attachCommandLine(context, context.window);
for (var i=0; i<context.windows.length; i++)
Firebug.CommandLine.injector.attachCommandLine(context, context.windows[i]);
}
var contentView = Wrapper.getContentView(context.window);
if (!contentView)
{
if (FBTrace.DBG_ERRORS)
FBTrace.sysout("CommandLine ERROR context.window invalid", context.window);
return false;
}
// the attach is asynchronous, we can report when it is complete:
return contentView._FirebugCommandLine;
},
onCommandLineKeyUp: function(event)
{
},
onCommandLineKeyDown: function(event)
{
var context = Firebug.currentContext;
this.autoCompleter.handleKeyDown(event, context);
if (event.keyCode === KeyEvent.DOM_VK_H && Events.isControl(event))
{
event.preventDefault();
this.autoCompleter.hide();
this.commandHistory.show(Firebug.chrome.$("fbCommandLineHistoryButton"));
return true;
}
// Parts of the code moved into key-press handler due to bug 613752
},
onCommandLineKeyPress: function(event)
{
var context = Firebug.currentContext;
if (!this.autoCompleter.handleKeyPress(event, context))
{
this.handleKeyPress(event);
}
},
handleKeyPress: function(event)
{
switch (event.keyCode)
{
case KeyEvent.DOM_VK_RETURN:
case KeyEvent.DOM_VK_ENTER:
event.preventDefault();
if (!event.metaKey && !event.shiftKey)
{
Firebug.CommandLine.enter(Firebug.currentContext);
this.commandHistory.hide();
return true;
}
else if(!event.metaKey && event.shiftKey)
{
Firebug.CommandLine.enterInspect(Firebug.currentContext);
this.commandHistory.hide();
return true;
}
break;
case KeyEvent.DOM_VK_UP:
event.preventDefault();
this.commandHistory.cycleCommands(Firebug.currentContext, -1);
return true;
case KeyEvent.DOM_VK_DOWN:
event.preventDefault();
this.commandHistory.cycleCommands(Firebug.currentContext, 1);
return true;
case KeyEvent.DOM_VK_ESCAPE:
event.preventDefault();
if (Firebug.CommandLine.cancel(Firebug.currentContext))
Events.cancelEvent(event);
this.commandHistory.hide();
return true;
}
return false;
},
onCommandLineInput: function(event)
{
var context = Firebug.currentContext;
if (!this.commandHistory.isShown())
{
this.autoCompleter.complete(context);
}
// Always update the buffer in context, even if command line is empty.
this.update(context);
},
onCommandLineBlur: function(event)
{
},
onCommandLineFocus: function(event)
{
if (FBTrace.DBG_COMMANDLINE)
{
var context = Firebug.currentContext;
FBTrace.sysout("commandLine.onCommandLineFocus; for: " +
(context ? context.getName() : "no context"));
}
if (this.autoCompleter.empty)
this.setAutoCompleter();
},
isAttached: function(context, win)
{
if (!context)
return false;
return Firebug.CommandLine.injector.isAttached(win ? win : context.window);
},
onPanelEnable: function(panelName)
{
Dom.collapse(Firebug.chrome.$("fbCommandBox"), true);
Dom.collapse(Firebug.chrome.$("fbPanelSplitter"), true);
Dom.collapse(Firebug.chrome.$("fbSidePanelDeck"), true);
this.setMultiLine(Firebug.commandEditor, Firebug.chrome);
},
onPanelDisable: function(panelName)
{
if (panelName != "console") // we don't care about other panels
return;
Dom.collapse(Firebug.chrome.$("fbCommandBox"), true);
Dom.collapse(Firebug.chrome.$("fbPanelSplitter"), true);
Dom.collapse(Firebug.chrome.$("fbSidePanelDeck"), true);
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Firebug.Console listener
onConsoleInjected: function(context, win)
{
// for some reason the console has been injected. If the user had focus in the command
// line they want it added in the page also. If the user has the cursor in the command
// line and reloads, the focus will already be there. issue 1339
var isFocused = Firebug.CommandEditor.hasFocus();
isFocused = isFocused || (this.getSingleRowCommandLine().getAttribute("focused") == "true");
if (isFocused)
setTimeout(this.onCommandLineFocus);
},
getCommandLine: function(context)
{
return (!this.isInOtherPanel(context) && Firebug.commandEditor) ?
this.getCommandEditor():
this.getSingleRowCommandLine();
},
isInOtherPanel: function(context)
{
// Command line on other panels is never multiline.
var visible = Firebug.CommandLine.Popup.isVisible();
return visible && context.panelName != "console";
},
getExpression: function(context)
{
return (!this.isInOtherPanel(context) && Firebug.commandEditor) ?
this.getCommandEditor().getExpression() :
this.getSingleRowCommandLine().value;
},
getCompletionBox: function()
{
return Firebug.chrome.$("fbCommandLineCompletion");
},
getSingleRowCommandLine: function()
{
return Firebug.chrome.$("fbCommandLine");
},
getCommandEditor: function()
{
return Firebug.CommandEditor;
}
});
// ********************************************************************************************* //
// Shared Helpers
Firebug.CommandLine.CommandHandler = Obj.extend(Object,
{
handle: function(event, api, win)
{
var element = event.target;
var methodName = win.document.getUserData("firebug-methodName");
// We create this array in the page using JS, so we need to look on the