Bug 350079 - Esc in search box now selects all and reverts. r=mak
authorAntonin LOUBIERE <pyjacpp@laposte.net>
Sun, 26 Sep 2021 09:10:02 +0000
changeset 593177 9237deee110beeaa7605195528a273b2bbc77352
parent 593176 4f7e9fe53b44e13edc2813523abefcb0b75e01f0
child 593178 60f70fb44696a20e969920a12ef34942eee14bd4
push id150336
push usermak77@bonardo.net
push dateSun, 26 Sep 2021 09:12:24 +0000
treeherderautoland@9237deee110b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs350079
milestone94.0a1
first release with
nightly linux32
9237deee110b / 94.0a1 / 20210926210158 / files
nightly linux64
9237deee110b / 94.0a1 / 20210926210158 / files
nightly mac
9237deee110b / 94.0a1 / 20210926210158 / files
nightly win32
9237deee110b / 94.0a1 / 20210926210158 / files
nightly win64
9237deee110b / 94.0a1 / 20210926210158 / files
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 350079 - Esc in search box now selects all and reverts. r=mak Fix bug, now escape in the search box (if the popup is disimissed) will select all the content and will revert to the last search. Differential Revision: https://phabricator.services.mozilla.com/D120137
browser/components/search/content/searchbar.js
browser/components/search/test/browser/browser_searchbar_default.js
--- a/browser/components/search/content/searchbar.js
+++ b/browser/components/search/content/searchbar.js
@@ -691,24 +691,32 @@
           if (!this.textbox.openSearch()) {
             aEvent.preventDefault();
             aEvent.stopPropagation();
             return true;
           }
         }
 
         let popup = this.textbox.popup;
-        if (!popup.popupOpen) {
-          return false;
+        if (popup.popupOpen) {
+          let suggestionsHidden =
+            popup.richlistbox.getAttribute("collapsed") == "true";
+          let numItems = suggestionsHidden ? 0 : popup.matchCount;
+          return popup.oneOffButtons.handleKeyDown(aEvent, numItems, true);
+        } else if (aEvent.keyCode == KeyEvent.DOM_VK_ESCAPE) {
+          let undoCount = this.textbox.editor.transactionManager
+            .numberOfUndoItems;
+          if (undoCount) {
+            this.textbox.editor.undo(undoCount);
+          } else {
+            this.textbox.select();
+          }
+          return true;
         }
-
-        let suggestionsHidden =
-          popup.richlistbox.getAttribute("collapsed") == "true";
-        let numItems = suggestionsHidden ? 0 : popup.matchCount;
-        return popup.oneOffButtons.handleKeyDown(aEvent, numItems, true);
+        return false;
       };
 
       // This method overrides the autocomplete binding's openPopup (essentially
       // duplicating the logic from the autocomplete popup binding's
       // openAutocompletePopup method), modifying it so that the popup is aligned with
       // the inner textbox, but sized to not extend beyond the search bar border.
       this.textbox.openPopup = () => {
         // Entering customization mode after the search bar had focus causes
@@ -778,16 +786,18 @@
         }
         // Otherwise, "call super": do what the autocomplete binding's
         // handleEnter implementation does.
         return this.textbox.mController.handleEnter(false, event || null);
       };
 
       // override |onTextEntered| in autocomplete.xml
       this.textbox.onTextEntered = event => {
+        this.textbox.editor.transactionManager.clearUndoStack();
+
         let engine;
         let oneOff = this.textbox.selectedButton;
         if (oneOff) {
           if (!oneOff.engine) {
             oneOff.doCommand();
             return;
           }
           engine = oneOff.engine;
--- a/browser/components/search/test/browser/browser_searchbar_default.js
+++ b/browser/components/search/test/browser/browser_searchbar_default.js
@@ -9,16 +9,18 @@
 
 const { SearchSuggestionController } = ChromeUtils.import(
   "resource://gre/modules/SearchSuggestionController.jsm"
 );
 
 const templateNormal = "https://example.com/?q=";
 const templatePrivate = "https://example.com/?query=";
 
+const searchPopup = document.getElementById("PopupSearchAutoComplete");
+
 add_task(async function setup() {
   await gCUITestUtils.addSearchBar();
 
   await SpecialPowers.pushPrefEnv({
     set: [["browser.search.separatePrivateDefault", false]],
   });
 
   // Create two new search engines. Mark one as the default engine, so
@@ -152,8 +154,58 @@ add_task(async function test_form_histor
   entries = (await FormHistoryTestUtils.search("searchbar-history")).map(
     entry => entry.value
   );
   Assert.deepEqual(entries, [], "Should not find form history");
 
   await FormHistoryTestUtils.clear("searchbar-history");
   BrowserTestUtils.removeTab(tab);
 });
+
+add_task(async function test_searchbar_revert() {
+  const tab = await BrowserTestUtils.openNewForegroundTab(
+    gBrowser,
+    "about:blank"
+  );
+
+  await doSearch(window, tab, "MozSearch1", templateNormal, "testQuery");
+
+  let searchbar = window.BrowserSearch.searchBar;
+  is(
+    searchbar.value,
+    "testQuery",
+    "Search value should be the the last search"
+  );
+
+  // focus search bar
+  let promise = promiseEvent(searchPopup, "popupshown");
+  info("Opening search panel");
+  searchbar.focus();
+  await promise;
+
+  searchbar.value = "aQuery";
+  searchbar.value = "anotherQuery";
+
+  // close the panel using the escape key.
+  promise = promiseEvent(searchPopup, "popuphidden");
+  EventUtils.synthesizeKey("KEY_Escape");
+  await promise;
+
+  is(searchbar.value, "anotherQuery", "The search value should be the same");
+  // revert the search bar value
+  EventUtils.synthesizeKey("KEY_Escape");
+  is(
+    searchbar.value,
+    "testQuery",
+    "The search value should have been reverted"
+  );
+
+  EventUtils.synthesizeKey("KEY_Escape");
+  is(searchbar.value, "testQuery", "The search value should be the same");
+
+  await doSearch(window, tab, "MozSearch1", templateNormal, "query");
+
+  is(searchbar.value, "query", "The search value should be query");
+  EventUtils.synthesizeKey("KEY_Escape");
+  is(searchbar.value, "query", "The search value should be the same");
+
+  BrowserTestUtils.removeTab(tab);
+});