Bug 1360524 - Remove search field focus after pressing enter. draft
authorEvan Tseng <evan@tseng.io>
Tue, 16 May 2017 16:08:41 +0800
changeset 578608 0d9c1272993ad7c8dadf06c735ab39ea2e070899
parent 578175 3e166b6838931b3933ca274331f9e0e115af5cc0
child 628768 db54e0cf5e12cdfbb1f6c486774ea9364693309d
push id58982
push userbmo:evan@tseng.io
push dateTue, 16 May 2017 08:08:59 +0000
bugs1360524
milestone55.0a1
Bug 1360524 - Remove search field focus after pressing enter. MozReview-Commit-ID: J6oeaCpxvYF
browser/components/preferences/in-content/findInPage.js
browser/components/preferences/in-content/tests/browser_search_within_preferences.js
--- a/browser/components/preferences/in-content/findInPage.js
+++ b/browser/components/preferences/in-content/findInPage.js
@@ -15,24 +15,27 @@ var gSearchResultsPane = {
     this.findSelection.setColors("currentColor", "#ffe900", "currentColor", "#540ead");
     this.searchResultsCategory = document.getElementById("category-search-results");
 
     this.searchInput = document.getElementById("searchInput");
     this.searchInput.hidden = !Services.prefs.getBoolPref("browser.preferences.search");
     if (!this.searchInput.hidden) {
       this.searchInput.addEventListener("command", this);
       this.searchInput.addEventListener("focus", this);
+      this.searchInput.addEventListener("keypress", this);
     }
   },
 
   handleEvent(event) {
     if (event.type === "command") {
       this.searchFunction(event);
     } else if (event.type === "focus") {
       this.initializeCategories();
+    } else if (event.type === "keypress") {
+      this.handleKeypressEvent(event);
     }
   },
 
   /**
    * Check that the passed string matches the filter arguments.
    *
    * @param String str
    *    to search for filter words in.
@@ -64,16 +67,27 @@ var gSearchResultsPane = {
         if (!category.inited) {
           category.init();
         }
       }
     }
   },
 
   /**
+   * Keypress event handler
+   *
+   * @param String event keypress event
+   */
+  handleKeypressEvent(event) {
+    if (event.keyCode === KeyEvent.DOM_VK_RETURN) {
+      this.searchInput.blur();
+    }
+  },
+
+  /**
    * Finds and returns text nodes within node and all descendants
    * Iterates through all the sibilings of the node object and adds the sibilings
    * to an array if sibiling is a TEXT_NODE else checks the text nodes with in current node
    * Source - http://stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page
    *
    * @param Node nodeObject
    *    DOM element
    * @returns array of text nodes
--- a/browser/components/preferences/in-content/tests/browser_search_within_preferences.js
+++ b/browser/components/preferences/in-content/tests/browser_search_within_preferences.js
@@ -230,8 +230,34 @@ add_task(async function() {
       is(child.selected, true, "Privacy panel should be selected");
     } else if (child.id) {
       is(child.selected, false, "No other panel should be selected");
     }
   }
 
   await BrowserTestUtils.removeTab(gBrowser.selectedTab);
 });
+
+/**
+ * Test for pressing enter on search field after doing search
+ */
+add_task(async function() {
+  await openPreferencesViaOpenPreferencesAPI("paneGeneral", {leaveOpen: true});
+
+  // Performs search
+  let doc = gBrowser.contentDocument;
+  let searchInput = doc.getElementById("searchInput");
+  searchInput.focus();
+  searchInput.value = "password";
+  searchInput.doCommand();
+  is(searchInput.getAttribute("focused"), "true",
+    "Search input should be the active element before pressing Return");
+  is(doc.activeElement, doc.getAnonymousNodes(searchInput)[0].querySelector("input"),
+    "Search input should be the active element before pressing Return");
+
+  EventUtils.synthesizeKey("VK_RETURN", {});
+  is(searchInput.getAttribute("focused"), "",
+    "Search input should not be focused after pressing Return");
+  isnot(doc.activeElement, doc.getAnonymousNodes(searchInput)[0].querySelector("input"),
+    "Search input should not be focused after pressing Return");
+
+  await BrowserTestUtils.removeTab(gBrowser.selectedTab);
+});