Bug 1385157 - Don't focus jsterm if the output region wasn't clicked;r=nchevobbe draft
authorBrian Grinstead <bgrinstead@mozilla.com>
Mon, 31 Jul 2017 11:57:47 -0700
changeset 618579 efbb9bcb6090517f80775326a65d955aba79b2fb
parent 618502 1be0c1da06076f85c69cd8a9d244e0164ec544d9
child 640118 d2df9333a9764c2559150bbfc2c7b17c42ba72b7
push id71385
push userbgrinstead@mozilla.com
push dateMon, 31 Jul 2017 18:57:57 +0000
reviewersnchevobbe
bugs1385157
milestone56.0a1
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();
   });
 }