diff --git a/browser/devtools/webconsole/test/browser_console_click_focus.js b/browser/devtools/webconsole/test/browser_console_click_focus.js index 994f421afbf..774c52b0314 100644 --- a/browser/devtools/webconsole/test/browser_console_click_focus.js +++ b/browser/devtools/webconsole/test/browser_console_click_focus.js @@ -16,24 +16,45 @@ function testInputFocus() { browser.removeEventListener("DOMContentLoaded", testInputFocus, false); openConsole().then((hud) => { - let inputNode = hud.jsterm.inputNode; - ok(inputNode.getAttribute("focused"), "input node is focused"); + waitForMessages({ + webconsole: hud, + messages: [{ + text: "Dolske Digs Bacon", + category: CATEGORY_WEBDEV, + severity: SEVERITY_LOG, + }], + }).then(([result]) => { + let msg = [...result.matched][0]; + let outputItem = msg.querySelector(".body"); + ok(outputItem, "found a logged message"); + let inputNode = hud.jsterm.inputNode; + ok(inputNode.getAttribute("focused"), "input node is focused, first"); - let lostFocus = () => { - inputNode.removeEventListener("blur", lostFocus); - info("input node lost focus"); - } + let lostFocus = () => { + inputNode.removeEventListener("blur", lostFocus); + info("input node lost focus"); + } - inputNode.addEventListener("blur", lostFocus); + inputNode.addEventListener("blur", lostFocus); - browser.ownerDocument.getElementById("urlbar").click(); + browser.ownerDocument.getElementById("urlbar").click(); - ok(!inputNode.getAttribute("focused"), "input node is not focused"); + ok(!inputNode.getAttribute("focused"), "input node is not focused"); - hud.outputNode.click(); + EventUtils.sendMouseEvent({type: "click"}, hud.outputNode); - ok(inputNode.getAttribute("focused"), "input node is focused"); + ok(inputNode.getAttribute("focused"), "input node is focused, second time") - finishTest(); + // test click-drags are not focusing the input element. + EventUtils.sendMouseEvent({type: "mousedown", clientX: 3, clientY: 4}, + outputItem); + EventUtils.sendMouseEvent({type: "click", clientX: 15, clientY: 5}, + outputItem); + + executeSoon(() => { + todo(!inputNode.getAttribute("focused"), "input node is not focused after drag"); + finishTest(); + }); + }); }); } diff --git a/browser/devtools/webconsole/webconsole.js b/browser/devtools/webconsole/webconsole.js index 1334560ad8b..a921dee70c9 100644 --- a/browser/devtools/webconsole/webconsole.js +++ b/browser/devtools/webconsole/webconsole.js @@ -571,15 +571,13 @@ WebConsoleFrame.prototype = { /* * Focus input line whenever the output area is clicked. - * Only focus when the target node (or parent, as in source links) is - * not an anchor. + * Reusing _addMEssageLinkCallback since it correctly filters + * drag and select events. */ - this.outputNode.addEventListener("click", (e) => { - if ((e.button == 0) && - (e.target.nodeName.toLowerCase() != "a") && - (e.target.parentNode.nodeName.toLowerCase() != "a")) { + this._addFocusCallback(this.outputNode, (evt) => { + if ((evt.target.nodeName.toLowerCase() != "a") && + (evt.target.parentNode.nodeName.toLowerCase() != "a")) this.jsterm.inputNode.focus(); - } }); // Toggle the timestamp on preference change @@ -2644,13 +2642,13 @@ WebConsoleFrame.prototype = { */ _addMessageLinkCallback: function WCF__addMessageLinkCallback(aNode, aCallback) { - aNode.addEventListener("mousedown", function(aEvent) { + aNode.addEventListener("mousedown", (aEvent) => { this._mousedown = true; this._startX = aEvent.clientX; this._startY = aEvent.clientY; }, false); - aNode.addEventListener("click", function(aEvent) { + aNode.addEventListener("click", (aEvent) => { let mousedown = this._mousedown; this._mousedown = false; @@ -2663,11 +2661,53 @@ WebConsoleFrame.prototype = { // If this event started with a mousedown event and it ends at a different // location, we consider this text selection. - if (mousedown && this._startX != aEvent.clientX && - this._startY != aEvent.clientY) { + // Add a fuzz modifier of two pixels in any direction to account for sloppy + // clicking. + if (mousedown && + (this._startX != aEvent.clientX) && + (this._startY != aEvent.clientY)) + { + this._startX = this._startY = undefined; return; } + this._startX = this._startY = undefined; + + aCallback.call(this, aEvent); + }, false); + }, + + _addFocusCallback: function WCF__addFocusCallback(aNode, aCallback) + { + aNode.addEventListener("mousedown", (aEvent) => { + this._mousedown = true; + this._startX = aEvent.clientX; + this._startY = aEvent.clientY; + }, false); + + aNode.addEventListener("click", (aEvent) => { + let mousedown = this._mousedown; + this._mousedown = false; + + // Do not allow middle/right-click or 2+ clicks. + if (aEvent.detail != 1 || aEvent.button != 0) { + return; + } + + // If this event started with a mousedown event and it ends at a different + // location, we consider this text selection. + // Add a fuzz modifier of two pixels in any direction to account for sloppy + // clicking. + if (mousedown && + (Math.abs(aEvent.clientX - this._startX) >= 2) && + (Math.abs(aEvent.clientY - this._startY) >= 1)) + { + this._startX = this._startY = undefined; + return; + } + + this._startX = this._startY = undefined; + aCallback.call(this, aEvent); }, false); },