Bug 1328410 - Only restore focus if the user did not explicitly place focus elsewhere. r=jaws, a=jcristau
authorNeil Deakin <neil@mozilla.com>
Wed, 04 Jan 2017 23:01:34 +0000
changeset 353491 9f3854382eba019383b78322885ff2c38dab633a
parent 353490 4244045e6f4d3b97eb1b90bc7a27d0982c39f190
child 353492 838e1c92ded6fff1e1366c81faab1ed3c41add60
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws, jcristau
bugs1328410
milestone52.0a2
Bug 1328410 - Only restore focus if the user did not explicitly place focus elsewhere. r=jaws, a=jcristau MozReview-Commit-ID: CdL2gN7EgbF
toolkit/content/widgets/popup.xml
--- a/toolkit/content/widgets/popup.xml
+++ b/toolkit/content/widgets/popup.xml
@@ -326,16 +326,29 @@
           } catch (e) {
             prevFocus.focus();
           }
         }
         var currentFocus = this._currentFocus;
         var prevFocus = this._prevFocus ? this._prevFocus.get() : null;
         this._currentFocus = null;
         this._prevFocus = null;
+
+        // Avoid changing focus if focus changed while we hide the popup
+        // (This can happen e.g. if the popup is hiding as a result of a
+        // click/keypress that focused something)
+        let nowFocus;
+        try {
+          nowFocus = document.commandDispatcher.focusedElement;
+        } catch (e) {
+          nowFocus = document.activeElement;
+        }
+        if (nowFocus && nowFocus != currentFocus)
+          return;
+
         if (prevFocus && this.getAttribute("norestorefocus") != "true") {
           // Try to restore focus
           try {
             if (document.commandDispatcher.focusedWindow != window)
               return; // Focus has already been set to a window outside of this panel
           } catch (ex) {}
 
           if (!currentFocus) {