Bug 1237350 - don't steal focus for the search box if another piece of non-button/non-input UI has focus, r?jaws,giorgios draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Mon, 11 Jan 2016 11:57:36 +0000
changeset 320821 59490d14e7e9de043645f89231ee7a818f3d7ed8
parent 320820 eae9f44f7444407a28ab3b876032eceebe19310c
child 512827 1287e2e766e66341a4f9ee60d63d87300c4797b6
push id9299
push usergijskruitbosch@gmail.com
push dateTue, 12 Jan 2016 14:47:29 +0000
reviewersjaws, giorgios
bugs1237350
milestone46.0a1
Bug 1237350 - don't steal focus for the search box if another piece of non-button/non-input UI has focus, r?jaws,giorgios
browser/base/content/abouthome/aboutHome.js
--- a/browser/base/content/abouthome/aboutHome.js
+++ b/browser/base/content/abouthome/aboutHome.js
@@ -51,19 +51,31 @@ window.addEventListener("pageshow", func
 });
 
 window.addEventListener("pagehide", function() {
   window.gObserver.disconnect();
   window.removeEventListener("resize", fitToWidth);
 });
 
 window.addEventListener("keypress", ev => {
-  // focus the search-box on keypress
-  if (document.activeElement.id == "searchText") // unless already focussed
+  if (ev.defaultPrevented) {
     return;
+  }
+
+  // don't focus the search-box on keypress if something other than the
+  // body or document element has focus - don't want to steal input from other elements
+  // Make an exception for <a> and <button> elements (and input[type=button|submit])
+  // which don't usefully take keypresses anyway.
+  // (except space, which is handled below)
+  if (document.activeElement && document.activeElement != document.body &&
+      document.activeElement != document.documentElement &&
+      !["a", "button"].includes(document.activeElement.localName) &&
+      !document.activeElement.matches("input:-moz-any([type=button],[type=submit])")) {
+    return;
+  }
 
   let modifiers = ev.ctrlKey + ev.altKey + ev.metaKey;
   // ignore Ctrl/Cmd/Alt, but not Shift
   // also ignore Tab, Insert, PageUp, etc., and Space
   if (modifiers != 0 || ev.charCode == 0 || ev.charCode == 32)
     return;
 
   searchText.focus();