Skip to content

Commit

Permalink
Merge pull request #20 from fflorent/issue_5535
Browse files Browse the repository at this point in the history
Execute selection in Command Editor (issue 5535)
  • Loading branch information
janodvarko committed Jul 10, 2012
2 parents 9a61e8f + 36cdb68 commit 50f785d
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 58 deletions.
94 changes: 53 additions & 41 deletions extension/content/firebug/console/commandEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ Firebug.CommandEditor = Obj.extend(Firebug.Module,
if (!this.editor)
return;

try
{
this.editor.removeEventListener("keypress", this.onKeyPress);
}
catch (err)
{
}

this.editor.removeEventListener(CONTEXT_MENU, this.onContextMenu);
this.editor.removeEventListener(TEXT_CHANGED, this.onTextChanged);

Expand All @@ -111,18 +103,6 @@ Firebug.CommandEditor = Obj.extend(Firebug.Module,
*/
onEditorLoad: function()
{
try
{
// This event is not supported in Fx11, so catch the exception
// which is thrown.
this.editor.addEventListener("keypress", this.onKeyPress);
}
catch (err)
{
if (FBTrace.DBG_ERROR)
FBTrace.sysout("commandEditor.onEditorLoad; EXCEPTION " + err, err);
}

// xxxHonza: Context menu support is going to change in SourceEditor
this.editor.addEventListener(CONTEXT_MENU, this.onContextMenu);
this.editor.addEventListener(TEXT_CHANGED, this.onTextChanged);
Expand All @@ -138,24 +118,6 @@ Firebug.CommandEditor = Obj.extend(Firebug.Module,
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Keyboard shortcuts

onKeyPress: function(event)
{
Firebug.CommandLine.update(Firebug.currentContext);

switch (event.keyCode)
{
case KeyEvent.DOM_VK_RETURN:
if (Events.isControl(event))
this.onExecute();
break;

case KeyEvent.DOM_VK_ESCAPE:
this.onEscape();
event.preventDefault();
break;
}
},

onExecute: function()
{
var context = Firebug.currentContext;
Expand Down Expand Up @@ -185,7 +147,7 @@ Firebug.CommandEditor = Obj.extend(Firebug.Module,
},

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Contex Menu
// Context Menu

onContextMenu: function(event)
{
Expand Down Expand Up @@ -257,6 +219,29 @@ Firebug.CommandEditor = Obj.extend(Firebug.Module,
// TODO xxxHonza
},

// returns the applicable commands
getExpression: function()
{
if (this.editor)
{
if (this.isCollapsed())
return this.getText();
else
return this.editor.getSelectedText();
}
},

isCollapsed: function()
{
var selection;
if (this.editor)
{
selection = this.editor.getSelection();
return selection.start === selection.end;
}
return true;
},

hasFocus: function()
{
try
Expand All @@ -269,6 +254,12 @@ Firebug.CommandEditor = Obj.extend(Firebug.Module,
}
},

focus: function()
{
if (this.editor)
this.editor.focus();
},

fontSizeAdjust: function(adjust)
{
if (!this.editor || !this.editor._view)
Expand Down Expand Up @@ -369,13 +360,34 @@ TextEditor.prototype =

setSelection: function(start, end)
{
this.textBox.setSelection(start, end);
this.textBox.setSelectionRange(start, end);
},

getSelection: function()
{
return {
start: this.textBox.selectionStart,
end: this.textBox.selectionEnd
};
},

hasFocus: function()
{
return this.textBox.getAttribute("focused") == "true";
}
},

focus: function()
{
this.textBox.focus();
},

getSelectedText: function()
{
var start = this.textBox.selectionStart;
var end = this.textBox.selectionEnd;

return this.textBox.value.substring(start, end);
}
}

// ********************************************************************************************* //
Expand Down
22 changes: 16 additions & 6 deletions extension/content/firebug/console/commandLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ Firebug.CommandLine = Obj.extend(Firebug.Module,

enter: function(context, command)
{
var expr = command ? command : this.getCommandLine(context).value;
var expr = command ? command : this.getExpression(context);
if (expr == "")
return;

Expand Down Expand Up @@ -950,15 +950,24 @@ Firebug.CommandLine = Obj.extend(Firebug.Module,
},

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();
if (visible && context.panelName != "console")
return this.getSingleRowCommandLine();
return visible && context.panelName != "console";
},

return Firebug.commandEditor
? this.getCommandEditor()
: this.getSingleRowCommandLine();
getExpression: function(context)
{
return (!this.isInOtherPanel(context) && Firebug.commandEditor) ?
this.getCommandEditor().getExpression() :
this.getSingleRowCommandLine().value;
},

getCompletionBox: function()
Expand All @@ -975,6 +984,7 @@ Firebug.CommandLine = Obj.extend(Firebug.Module,
{
return Firebug.CommandEditor;
}

});

// ********************************************************************************************* //
Expand Down
47 changes: 36 additions & 11 deletions tests/FBTest/content/FBTestFirebug.js
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,19 @@ this.getPref = function(pref)
// ********************************************************************************************* //
// Command Line

function getCommandLine(useCommandEditor)
{
return useCommandEditor ?
FW.Firebug.CommandEditor :
FW.Firebug.CommandLine.getSingleRowCommandLine();
}

/**
* executes an expression inside the Command Line
* @param {String} the command to execute
* @param {Object} the Firebug.chrome object
* @param {Boolean} if set to true, type in the CommandEditor, or in the CommandLine otherwise
*/
this.executeCommand = function(expr, chrome, useCommandEditor)
{
this.clearAndTypeCommand(expr, useCommandEditor);
Expand All @@ -1148,39 +1161,51 @@ this.executeCommand = function(expr, chrome, useCommandEditor)
FBTest.sendKey("RETURN", "fbCommandLine");
};

this.clearCommand = function(useCommandEditor)
/**
* clears the Command Line or the Command Editor
*/
this.clearCommand = function()
{
var doc = FW.Firebug.chrome.window.document;
var cmdLine = doc.getElementById(useCommandEditor ? "fbCommandEditor": "fbCommandLine");
cmdLine.value = "";
FW.Firebug.CommandLine.clear(FW.Firebug.currentContext);
};


/**
* clears and types a command into the Command Line or the Command Editor
* @param {String} the command to type
* @param {Boolean} if set to true, type in the CommandEditor, or in the CommandLine otherwise
*
*/
this.clearAndTypeCommand = function(string, useCommandEditor)
{
FBTest.clearCommand(useCommandEditor);
FBTest.clearCommand();
FBTest.typeCommand(string, useCommandEditor);
};

/**
* types a command into the Command Line or the Command Editor
* @param {String} the command to type
* @param {Boolean} if set to true, type in the CommandEditor, or in the CommandLine otherwise
*
*/
this.typeCommand = function(string, useCommandEditor)
{
var doc = FW.Firebug.chrome.window.document;
var cmdLine = doc.getElementById(useCommandEditor ? "fbCommandEditor": "fbCommandLine");
var panelBar1 = doc.getElementById("fbPanelBar1");
var cmdLine = getCommandLine(useCommandEditor);
var win = panelBar1.browser.contentWindow;

if (useCommandEditor)
FBTest.setPref("largeCommandLine", useCommandEditor);
FBTest.setPref("commandEditor", useCommandEditor);

FW.Firebug.chrome.window.focus();
panelBar1.browser.contentWindow.focus();
FBTest.focus(cmdLine);
cmdLine.focus();

FBTest.sysout("typing "+string+" in to "+cmdLine+" focused on "+
FW.FBL.getElementCSSSelector(doc.commandDispatcher.focusedElement)+
" win "+panelBar1.browser.contentWindow);

for (var i=0; i<string.length; ++i)
FBTest.synthesizeKey(string.charAt(i), null, win);
this.sendString(string, doc.commandDispatcher.focusedElement);
};

/**
Expand Down
54 changes: 54 additions & 0 deletions tests/content/console/executeSelection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head>
<title>Execute Command Selection</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="https://getfirebug.com/tests/head/_common/testcase.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript">
window.addEventListener("load", function(){
var email = document.getElementById("email");
email.textContent = email.textContent
.replace("__AT__", String.fromCharCode(64))
.replace("__DOT__", ".");
}, false);
</script>
</head>
<body>
<header>
<h1><a href="http://code.google.com/p/fbug/issues/detail?id=5535">Issue 5535</a>: Execute Command Selection</h1>
</header>
<div>
<section id="description">
<p>This test case is intended for <em>command editor</em></p>
<ol>
<li>Open Firebug</li>
<li>Enable and switch to the <em>Console</em> panel</li>
<li>Switch to the editor mode</li>
<li>Type the following commands : <br/>
<code style="color:green; white-space: normal;">var a = "no selection";<br/>
var b = window.a || "selection";<br/>
console.log(b);<br/></code></li>
<li>Select the 2<sup>nd</sup> and the 3<sup>rd</sup> line</li>
<li>Click on "Run".</li>
<li>The console panel should display : <br/>
<code style="color: red"> selection</code>
</li>
<li>Collapse the selection</li>
<li>Click on "Run"</li>
<li>The Console panel should display : <br/>
<code style="color: red"> no selection</code>
</li>
<li>Switch back to Command Line</li>
<li>Select <code style="color: green">var b = a || "selection"; console.log(b);</code></li>
<li>Press <i>Enter</i></li>
<li>The Console panel should display : <br/>
<code style="color: red"> no selection</code>
</li>
</ol>
</section>
<footer>
Florent FAYOLLE, &lt;<span id="email">florent.fayolle69__AT__gmail__DOT__com</span>&gt;
</footer>
</div>
</body>
</html>
Loading

0 comments on commit 50f785d

Please sign in to comment.