Bug 1385157 - Don't focus jsterm if the output region wasn't clicked;r=nchevobbe
authorBrian Grinstead <bgrinstead@mozilla.com>
Mon, 31 Jul 2017 11:57:47 -0700
changeset 423159 29ffe5a88b7b3b113ee9ee4db80885a1470ab0b9
parent 423158 c92dcf2ce2368b49a78c55c827d404f4d98797bd
child 423160 80647273e2f06e3968f4bba052be4a516a2b0f61
push id1517
push userjlorenzo@mozilla.com
push dateThu, 14 Sep 2017 16:50:54 +0000
treeherdermozilla-release@3b41fd564418 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnchevobbe
bugs1385157
milestone56.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 1385157 - Don't focus jsterm if the output region wasn't clicked;r=nchevobbe MozReview-Commit-ID: Gv60PSFKErD
devtools/client/webconsole/new-console-output/new-console-output-wrapper.js
devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_input_focus.js
--- a/devtools/client/webconsole/new-console-output/new-console-output-wrapper.js
+++ b/devtools/client/webconsole/new-console-output/new-console-output-wrapper.js
@@ -41,28 +41,32 @@ NewConsoleOutputWrapper.prototype = {
     };
     // Focus the input line whenever the output area is clicked.
     this.parentNode.addEventListener("click", (event) => {
       // Do not focus on middle/right-click or 2+ clicks.
       if (event.detail !== 1 || event.button !== 0) {
         return;
       }
 
+      // Do not focus if a link was clicked
+      if (event.originalTarget.closest("a")) {
+        return;
+      }
+
+      // Do not focus if something other than the output region was clicked
+      if (!event.originalTarget.closest(".webconsole-output")) {
+        return;
+      }
+
       // Do not focus if something is selected
       let selection = this.document.defaultView.getSelection();
       if (selection && !selection.isCollapsed) {
         return;
       }
 
-      // Do not focus if a link was clicked
-      if (event.target.nodeName.toLowerCase() === "a" ||
-          event.target.parentNode.nodeName.toLowerCase() === "a") {
-        return;
-      }
-
       this.jsterm.focus();
     });
 
     const serviceContainer = {
       attachRefToHud,
       emitNewMessage: (node, messageId) => {
         this.jsterm.hud.emit("new-messages", new Set([{
           node,
--- a/devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_input_focus.js
+++ b/devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_input_focus.js
@@ -10,43 +10,51 @@
 const TEST_URI =
   `data:text/html;charset=utf-8,Test input focused
   <script>
     console.log("console message 1");
   </script>`;
 
 add_task(function* () {
   let hud = yield openNewTabAndConsole(TEST_URI);
+
   hud.jsterm.clearOutput();
-
   let inputNode = hud.jsterm.inputNode;
   ok(inputNode.getAttribute("focused"), "input node is focused after output is cleared");
 
+  info("Focus during message logging");
   ContentTask.spawn(gBrowser.selectedBrowser, {}, function* () {
     content.wrappedJSObject.console.log("console message 2");
   });
   let msg = yield waitFor(() => findMessage(hud, "console message 2"));
-  let outputItem = msg.querySelector(".message-body");
-  inputNode = hud.jsterm.inputNode;
-  ok(inputNode.getAttribute("focused"), "input node is focused, first");
-  yield waitForBlurredInput(inputNode);
-  EventUtils.sendMouseEvent({type: "click"}, hud.outputNode);
+  ok(inputNode.getAttribute("focused"), "input node is focused, first time");
 
+  info("Focus after clicking in the output area");
+  yield waitForBlurredInput(hud);
+  EventUtils.sendMouseEvent({type: "click"}, msg);
   ok(inputNode.getAttribute("focused"), "input node is focused, second time");
-  yield waitForBlurredInput(inputNode);
+
   info("Setting a text selection and making sure a click does not re-focus");
+  yield waitForBlurredInput(hud);
   let selection = hud.iframeWindow.getSelection();
-  selection.selectAllChildren(outputItem);
-  EventUtils.sendMouseEvent({type: "click"}, hud.outputNode);
+  selection.selectAllChildren(msg.querySelector(".message-body"));
+  EventUtils.sendMouseEvent({type: "click"}, msg);
   ok(!inputNode.getAttribute("focused"),
-    "input node focused after text is selected");
+    "input node not focused after text is selected");
 });
 
-function waitForBlurredInput(inputNode) {
+function waitForBlurredInput(hud) {
+  let inputNode = hud.jsterm.inputNode;
   return new Promise(resolve => {
     let lostFocus = () => {
       ok(!inputNode.getAttribute("focused"), "input node is not focused");
       resolve();
     };
     inputNode.addEventListener("blur", lostFocus, { once: true });
+
+    // Clicking on a DOM Node outside of the webconsole document. The 'blur' event fires
+    // if we click on something in this document (like the filter box), but the 'focus'
+    // event won't re-fire on the textbox XBL binding when it's clicked on again.
+    // Bug 1304328 is tracking removal of XUL for jsterm, we should be able to click on
+    // the filter textbox instead of the url bar after that.
     document.getElementById("urlbar").click();
   });
 }