Bug 1174471: Override autocomplete's default handling of left, right and home key presses. r=markh
authorDave Townsend <dtownsend@oxymoronical.com>
Thu, 18 Jun 2015 11:02:11 -0700
changeset 268201 3606409b170fbadec766916c5c8ccfcada8bc179
parent 268200 732640286a2082c6ed685879ca9c1cda35c3d9ed
child 268202 2555d8ffbd6b9d511e98110520dceaf3fa012dc9
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-esr52@6dd5a4f5f745 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmarkh
bugs1174471
milestone41.0a1
Bug 1174471: Override autocomplete's default handling of left, right and home key presses. r=markh
browser/base/content/test/general/browser.ini
browser/base/content/test/general/browser_autocomplete_cursor.js
browser/base/content/urlbarBindings.xml
toolkit/content/widgets/autocomplete.xml
--- a/browser/base/content/test/general/browser.ini
+++ b/browser/base/content/test/general/browser.ini
@@ -126,16 +126,17 @@ skip-if = e10s # Bug 1093153 - no about:
 [browser_action_keyword_override.js]
 [browser_action_searchengine.js]
 [browser_action_searchengine_alias.js]
 [browser_addKeywordSearch.js]
 [browser_search_favicon.js]
 [browser_alltabslistener.js]
 [browser_autocomplete_a11y_label.js]
 skip-if = e10s # Bug 1101993 - times out for unknown reasons when run in the dir (works on its own)
+[browser_autocomplete_cursor.js]
 [browser_autocomplete_enter_race.js]
 [browser_autocomplete_no_title.js]
 [browser_autocomplete_autoselect.js]
 [browser_autocomplete_oldschool_wrap.js]
 [browser_autocomplete_tag_star_visibility.js]
 [browser_backButtonFitts.js]
 skip-if = os == "mac" # The Fitt's Law back button is not supported on OS X
 [browser_beforeunload_duplicate_dialogs.js]
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/general/browser_autocomplete_cursor.js
@@ -0,0 +1,25 @@
+add_task(function*() {
+  // This test is only relevant if UnifiedComplete is enabled.
+  let ucpref = Services.prefs.getBoolPref("browser.urlbar.unifiedcomplete");
+  Services.prefs.setBoolPref("browser.urlbar.unifiedcomplete", true);
+  registerCleanupFunction(() => {
+    Services.prefs.setBoolPref("browser.urlbar.unifiedcomplete", ucpref);
+  });
+
+  let tab = gBrowser.selectedTab = gBrowser.addTab("about:mozilla", {animate: false});
+  yield promiseTabLoaded(tab);
+  yield promiseAutocompleteResultPopup("www.mozilla.org");
+
+  gURLBar.selectTextRange(4, 4);
+
+  is(gURLBar.popup.state, "open", "Popup should be open");
+  is(gURLBar.popup.richlistbox.selectedIndex, 0, "Should have selected something");
+
+  EventUtils.synthesizeKey("VK_RIGHT", {});
+  yield promisePopupHidden(gURLBar.popup);
+
+  is(gURLBar.selectionStart, 5, "Should have moved the cursor");
+  is(gURLBar.selectionEnd, 5, "And not selected anything");
+
+  gBrowser.removeTab(tab);
+});
--- a/browser/base/content/urlbarBindings.xml
+++ b/browser/base/content/urlbarBindings.xml
@@ -179,16 +179,32 @@ file, You can obtain one at http://mozil
             this.setAttribute("actiontype", action.type);
           } else {
             this.removeAttribute("actiontype");
           }
           return returnValue;
         ]]></body>
       </method>
 
+      <method name="onKeyPress">
+        <parameter name="aEvent"/>
+        <body><![CDATA[
+          switch (aEvent.keyCode) {
+            case KeyEvent.DOM_VK_LEFT:
+            case KeyEvent.DOM_VK_RIGHT:
+            case KeyEvent.DOM_VK_HOME:
+              this.popup.hidePopup();
+              return;
+              break;
+          }
+
+          return this.handleKeyPress(aEvent);
+        ]]></body>
+      </method>
+
       <field name="_mayTrimURLs">true</field>
       <method name="trimValue">
         <parameter name="aURL"/>
         <body><![CDATA[
           // This method must not modify the given URL such that calling
           // nsIURIFixup::createFixupURI with the result will produce a different URI.
           return this._mayTrimURLs ? trimURL(aURL) : aURL;
         ]]></body>
--- a/toolkit/content/widgets/autocomplete.xml
+++ b/toolkit/content/widgets/autocomplete.xml
@@ -407,16 +407,23 @@
       </method>
 
       <!-- ::::::::::::: key handling ::::::::::::: -->
 
       <field name="_selectionDetails">null</field>
       <method name="onKeyPress">
         <parameter name="aEvent"/>
         <body><![CDATA[
+          return this.handleKeyPress(aEvent);
+        ]]></body>
+      </method>
+
+      <method name="handleKeyPress">
+        <parameter name="aEvent"/>
+        <body><![CDATA[
           if (aEvent.target.localName != "textbox")
             return true; // Let child buttons of autocomplete take input
 
           //XXXpch this is so bogus...
           if (aEvent.defaultPrevented)
             return false;
 
           var cancel = false;