Bug 1521629 - Port search() to UrlbarInput.jsm r=dao
authorRicky Rosario <rickyrosario@gmail.com>
Sat, 02 Feb 2019 16:33:57 +0000
changeset 456564 5b4294b589042588942714434fe614b93bb20ae1
parent 456563 5e295b971fd8c53e94525fe31f54fa0d6e9ceb27
child 456565 c858a0fe0feab0168823813cece7c5bcedc25461
push id35490
push useraiakab@mozilla.com
push dateSun, 03 Feb 2019 09:50:26 +0000
treeherdermozilla-central@12b2328eedd1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdao
bugs1521629
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1521629 - Port search() to UrlbarInput.jsm r=dao MozReview-Commit-ID: 70RV8IdoAKc Differential Revision: https://phabricator.services.mozilla.com/D18381
browser/base/content/browser-allTabsMenu.js
browser/base/content/browser-places.js
browser/base/content/browser.js
browser/base/content/urlbarBindings.xml
browser/components/urlbar/UrlbarInput.jsm
browser/components/urlbar/tests/browser/browser.ini
browser/components/urlbar/tests/browser/browser_UrlbarInput_search.js
--- a/browser/base/content/browser-allTabsMenu.js
+++ b/browser/base/content/browser-allTabsMenu.js
@@ -129,11 +129,11 @@ var gTabsPanel = {
     }
     this.allTabsView.addEventListener("ViewShown", (e) => {
       PanelUI.showSubView(this.kElements.hiddenTabsView, this.hiddenTabsButton);
     }, {once: true});
     this.showAllTabsPanel();
   },
 
   searchTabs() {
-    gURLBar.typeRestrictToken(UrlbarTokenizer.RESTRICT.OPENPAGE);
+    gURLBar.search(UrlbarTokenizer.RESTRICT.OPENPAGE);
   },
 };
--- a/browser/base/content/browser-places.js
+++ b/browser/base/content/browser-places.js
@@ -537,17 +537,17 @@ var PlacesCommandHook = {
                  "", "chrome,toolbar=yes,dialog=no,resizable", item);
     } else {
       organizer.PlacesOrganizer.selectLeftPaneContainerByHierarchy(item);
       organizer.focus();
     }
   },
 
   searchBookmarks() {
-    gURLBar.typeRestrictToken(UrlbarTokenizer.RESTRICT.BOOKMARK);
+    gURLBar.search(UrlbarTokenizer.RESTRICT.BOOKMARK);
   },
 };
 
 ChromeUtils.defineModuleGetter(this, "RecentlyClosedTabsAndWindowsMenuUtils",
   "resource:///modules/sessionstore/RecentlyClosedTabsAndWindowsMenuUtils.jsm");
 
 // View for the history menu.
 function HistoryMenu(aPopupShowingEvent) {
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -4026,17 +4026,17 @@ const BrowserSearch = {
         Services.obs.addObserver(observer, "browser-delayed-startup-finished");
       }
       return;
     }
 
     let focusUrlBarIfSearchFieldIsNotActive = function(aSearchBar) {
       if (!aSearchBar || document.activeElement != aSearchBar.textbox.inputField) {
         // Limit the results to search suggestions, like the search bar.
-        gURLBar.typeRestrictToken(UrlbarTokenizer.RESTRICT.SEARCH);
+        gURLBar.search(UrlbarTokenizer.RESTRICT.SEARCH);
       }
     };
 
     let searchBar = this.searchBar;
     let placement = CustomizableUI.getPlacementOfWidget("search-container");
     let focusSearchBar = () => {
       searchBar = this.searchBar;
       searchBar.select();
--- a/browser/base/content/urlbarBindings.xml
+++ b/browser/base/content/urlbarBindings.xml
@@ -1664,18 +1664,24 @@ file, You can obtain one at http://mozil
 
           // We want the value to be treated as text that the user typed.  It
           // should go through the controller.handleText() path in onInput() so
           // that gBrowser.userTypedValue, this.valueIsTyped, etc. are set and
           // nsAutoCompleteController::HandleText() is called.  Set this.value
           // and fire an input event to do that.  (If we set this.textValue we'd
           // get an input event for free, but it would also set mIgnoreInput,
           // skipping all of the above requirements.)
-          this.focus();
-          this.value = value;
+          focusAndSelectUrlBar();
+
+          // If the value is a restricted token, append a space.
+          if (Object.values(UrlbarTokenizer.RESTRICT).includes(value)) {
+            this.inputField.value = value + " ";
+          } else {
+            this.inputField.value = value;
+          }
 
           // Avoid selecting the text if this method is called twice in a row.
           this.selectionStart = -1;
 
           let event = document.createEvent("Events");
           event.initEvent("input", true, true);
           this.inputField.dispatchEvent(event);
 
@@ -1685,29 +1691,16 @@ file, You can obtain one at http://mozil
           // new search here.
           if (!this._onInputHandledText) {
             this.gotResultForCurrentQuery = false;
             this.controller.startSearch(value);
           }
         ]]></body>
       </method>
 
-      <method name="typeRestrictToken">
-        <parameter name="char"/>
-        <body><![CDATA[
-          focusAndSelectUrlBar();
-
-          this.inputField.value = char + " ";
-
-          let event = this.document.createEvent("UIEvents");
-          event.initUIEvent("input", true, false, window, 0);
-          this.inputField.dispatchEvent(event);
-        ]]></body>
-      </method>
-
       <method name="removeHiddenFocus">
         <body><![CDATA[
           this.classList.remove("hidden-focus");
         ]]></body>
       </method>
 
       <method name="setHiddenFocus">
         <body><![CDATA[
--- a/browser/components/urlbar/UrlbarInput.jsm
+++ b/browser/components/urlbar/UrlbarInput.jsm
@@ -12,16 +12,17 @@ XPCOMUtils.defineLazyModuleGetters(this,
   AppConstants: "resource://gre/modules/AppConstants.jsm",
   ExtensionSearchHandler: "resource://gre/modules/ExtensionSearchHandler.jsm",
   PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
   ReaderMode: "resource://gre/modules/ReaderMode.jsm",
   Services: "resource://gre/modules/Services.jsm",
   UrlbarController: "resource:///modules/UrlbarController.jsm",
   UrlbarPrefs: "resource:///modules/UrlbarPrefs.jsm",
   UrlbarQueryContext: "resource:///modules/UrlbarUtils.jsm",
+  UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.jsm",
   UrlbarUtils: "resource:///modules/UrlbarUtils.jsm",
   UrlbarValueFormatter: "resource:///modules/UrlbarValueFormatter.jsm",
   UrlbarView: "resource:///modules/UrlbarView.jsm",
 });
 
 XPCOMUtils.defineLazyServiceGetter(this, "ClipboardHelper",
                                    "@mozilla.org/widget/clipboardhelper;1",
                                    "nsIClipboardHelper");
@@ -432,20 +433,35 @@ class UrlbarInput {
       lastKey,
       maxResults: UrlbarPrefs.get("maxRichResults"),
       muxer: "UnifiedComplete",
       providers: ["UnifiedComplete"],
       searchString,
     }));
   }
 
-  typeRestrictToken(char) {
+  /**
+   * Sets the input's value, starts a search, and opens the popup.
+   *
+   * @param {string} value
+   *   The input's value will be set to this value, and the search will
+   *   use it as its query.
+   */
+  search(value) {
     this.window.focusAndSelectUrlBar();
 
-    this.inputField.value = char + " ";
+    // If the value is a restricted token, append a space.
+    if (Object.values(UrlbarTokenizer.RESTRICT).includes(value)) {
+      this.inputField.value = value + " ";
+    } else {
+      this.inputField.value = value;
+    }
+
+    // Avoid selecting the text if this method is called twice in a row.
+    this.selectionStart = -1;
 
     let event = this.document.createEvent("UIEvents");
     event.initUIEvent("input", true, false, this.window, 0);
     this.inputField.dispatchEvent(event);
   }
 
   /**
    * Focus without the focus styles.
--- a/browser/components/urlbar/tests/browser/browser.ini
+++ b/browser/components/urlbar/tests/browser/browser.ini
@@ -47,16 +47,17 @@ support-files =
 [browser_urlbarCutting.js]
 [browser_urlbarEnter.js]
 [browser_urlbarFocusedCmdK.js]
 [browser_urlbarHashChangeProxyState.js]
 [browser_UrlbarInput_autofill.js]
 [browser_UrlbarInput_formatValue.js]
 [browser_UrlbarInput_hiddenFocus.js]
 [browser_UrlbarInput_overflow.js]
+[browser_UrlbarInput_search.js]
 [browser_UrlbarInput_tooltip.js]
 [browser_UrlbarInput_trimURLs.js]
 subsuite = clipboard
 [browser_UrlbarInput_unit.js]
 support-files = empty.xul
 [browser_UrlbarLoadRace.js]
 [browser_urlbarPlaceholder.js]
 support-files =
new file mode 100644
--- /dev/null
+++ b/browser/components/urlbar/tests/browser/browser_UrlbarInput_search.js
@@ -0,0 +1,22 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+add_task(async function() {
+  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
+
+  registerCleanupFunction(async function() {
+    BrowserTestUtils.removeTab(tab);
+  });
+
+  // Test with a search shortcut.
+  gURLBar.blur();
+  gURLBar.search("@google ");
+  ok(gURLBar.hasAttribute("focused"), "url bar is focused");
+  is(gURLBar.value, "@google ", "url bar has correct value");
+
+  // Test with a restricted token.
+  gURLBar.blur();
+  gURLBar.search("?");
+  ok(gURLBar.hasAttribute("focused"), "url bar is focused");
+  is(gURLBar.value, "? ", "url bar has correct value");
+});