Bug 1576563 - Evaluate selected text if a selection exists in console input. r=Honza.
authorNicolas Chevobbe <nchevobbe@mozilla.com>
Mon, 26 Aug 2019 14:28:38 +0000
changeset 553635 0690321bae187ea4f67eb59acef2484c3ccf8b46
parent 553634 025d1028313e06933dba9394650dc0f32c68e5e1
child 553636 da5201d67339cf79bf625b4e1765783335bd83f5
push id2165
push userffxbld-merge
push dateMon, 14 Oct 2019 16:30:58 +0000
treeherdermozilla-release@0eae18af659f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersHonza
bugs1576563
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1576563 - Evaluate selected text if a selection exists in console input. r=Honza. This is something Scratchpad allowed and that people told me they were relying on. With Scratchpad being deprecated, it makes sense to implement it in the WebConsole as it's quite easy to do so. A test is added to ensure this works as expected. Differential Revision: https://phabricator.services.mozilla.com/D43394
devtools/client/webconsole/components/Input/JSTerm.js
devtools/client/webconsole/test/browser/browser.ini
devtools/client/webconsole/test/browser/browser_jsterm_editor_execute_selection.js
--- a/devtools/client/webconsole/components/Input/JSTerm.js
+++ b/devtools/client/webconsole/components/Input/JSTerm.js
@@ -553,17 +553,17 @@ class JSTerm extends Component {
       focusableEl.focus();
     }
   }
 
   /**
    * Execute a string. Execution happens asynchronously in the content process.
    */
   _execute() {
-    const executeString = this._getValue();
+    const executeString = this.getSelectedText() || this._getValue();
     if (!executeString) {
       return;
     }
 
     if (!this.props.editorMode) {
       this._setValue("");
     }
     this.clearCompletion();
--- a/devtools/client/webconsole/test/browser/browser.ini
+++ b/devtools/client/webconsole/test/browser/browser.ini
@@ -228,16 +228,17 @@ skip-if = (os == "win" && processor == "
 [browser_jsterm_copy_command.js]
 [browser_jsterm_ctrl_a_select_all.js]
 [browser_jsterm_ctrl_key_nav.js]
 skip-if = os != 'mac' # The tested ctrl+key shortcuts are OSX only
 [browser_jsterm_document_no_xray.js]
 [browser_jsterm_editor.js]
 [browser_jsterm_editor_disabled_history_nav_with_keyboard.js]
 [browser_jsterm_editor_enter.js]
+[browser_jsterm_editor_execute_selection.js]
 [browser_jsterm_editor_execute.js]
 [browser_jsterm_editor_gutter.js]
 [browser_jsterm_editor_toggle_keyboard_shortcut.js]
 [browser_jsterm_editor_resize.js]
 [browser_jsterm_editor_toolbar.js]
 [browser_jsterm_error_docs.js]
 [browser_jsterm_error_outside_valid_range.js]
 [browser_jsterm_focus_reload.js]
new file mode 100644
--- /dev/null
+++ b/devtools/client/webconsole/test/browser/browser_jsterm_editor_execute_selection.js
@@ -0,0 +1,58 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Test that the user can execute only the code that is selected in the input, in editor
+// mode.
+// See https://bugzilla.mozilla.org/show_bug.cgi?id=1576563
+
+"use strict";
+
+const TEST_URI =
+  "data:text/html;charset=utf-8,Web Console test for executing input selection";
+
+add_task(async function() {
+  await pushPref("devtools.webconsole.features.editor", true);
+  await pushPref("devtools.webconsole.input.editor", true);
+  const hud = await openNewTabAndConsole(TEST_URI);
+
+  const expression = `x = 10;x;
+    x = 20;x;`;
+
+  info("Evaluate the whole expression");
+  setInputValue(hud, expression);
+
+  let onResultMessage = waitForMessage(hud, "20", ".result");
+  synthesizeEvaluation();
+  await onResultMessage;
+  ok(true, "The whole expression is evaluated when there's no selection");
+
+  info("Select the first line and evaluate");
+  hud.ui.jsterm.editor.setSelection(
+    { line: 0, ch: 0 },
+    { line: 0, ch: expression.split("\n")[0].length }
+  );
+  onResultMessage = waitForMessage(hud, "10", ".result");
+  synthesizeEvaluation();
+  await onResultMessage;
+  ok(true, "Only the expression on the first line was evaluated");
+
+  info("Check that this works in inline mode as well");
+  await toggleLayout(hud);
+  hud.ui.jsterm.editor.setSelection(
+    { line: 0, ch: 0 },
+    { line: 0, ch: expression.split("\n")[0].length }
+  );
+  onResultMessage = waitForMessage(hud, "10", ".result");
+  synthesizeEvaluation();
+  await onResultMessage;
+  ok(
+    true,
+    "Only the expression on the first line was evaluated in inline mode"
+  );
+});
+
+function synthesizeEvaluation() {
+  EventUtils.synthesizeKey("KEY_Enter", {
+    [Services.appinfo.OS === "Darwin" ? "metaKey" : "ctrlKey"]: true,
+  });
+}