Bug 603407 - Quick search not done upon entering '[keyword] text' Return [r=mbrubeck]
authorMark Finkle <mfinkle@mozilla.com>
Mon, 01 Nov 2010 19:36:07 -0400
changeset 66932 6ceb14c0eb002218d987bdce0c20d0dbe3273777
parent 66931 ea8e43b21f3d8f938abf8cb74ae411dfb1a1463e
child 66933 74458f5c71bb3f96d7cfcbec497ed9eaf5258f63
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmbrubeck
bugs603407
Bug 603407 - Quick search not done upon entering '[keyword] text' Return [r=mbrubeck]
mobile/chrome/content/browser-ui.js
mobile/chrome/content/browser.js
--- a/mobile/chrome/content/browser-ui.js
+++ b/mobile/chrome/content/browser-ui.js
@@ -584,17 +584,19 @@ var BrowserUI = {
     Util.forceOnline();
 
     // Give the new page lots of room
     Browser.hideSidebars();
     this.closeAutoComplete();
 
     this._edit.value = aURI;
 
-    Browser.loadURI(aURI, { flags: Ci.nsIWebNavigation.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP });
+    let postData = {};
+    aURI = Browser.getShortcutOrURI(aURI, postData);
+    Browser.loadURI(aURI, { flags: Ci.nsIWebNavigation.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP, postData: postData });
 
     // Delay doing the fixup so the raw URI is passed to loadURIWithFlags
     // and the proper third-party fixup can be done
     let fixupFlags = Ci.nsIURIFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
     let uri = gURIFixup.createFixupURI(aURI, fixupFlags);
     gHistSvc.markPageAsTyped(uri);
   },
 
--- a/mobile/chrome/content/browser.js
+++ b/mobile/chrome/content/browser.js
@@ -524,16 +524,90 @@ var Browser = {
     else {
       let params = aParams || {};
       let flags = params.flags || Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
       browser.loadURIWithFlags(aURI, flags, params.referrerURI, params.charset, params.postData);
     }
   },
 
   /**
+   * Determine if the given URL is a shortcut/keyword and, if so, expand it
+   * @param aURL String
+   * @param aPostDataRef Out param contains any required post data for a search
+   * @returns the expanded shortcut, or the original URL if not a shortcut
+   */
+  getShortcutOrURI: function getShortcutOrURI(aURL, aPostDataRef) {
+    let shortcutURL = null;
+    let keyword = aURL;
+    let param = "";
+
+    let offset = aURL.indexOf(" ");
+    if (offset > 0) {
+      keyword = aURL.substr(0, offset);
+      param = aURL.substr(offset + 1);
+    }
+  
+    if (!aPostDataRef)
+      aPostDataRef = {};
+  
+    let engine = Services.search.getEngineByAlias(keyword);
+    if (engine) {
+      let submission = engine.getSubmission(param);
+      aPostDataRef.value = submission.postData;
+      return submission.uri.spec;
+    }
+
+    try {
+      [shortcutURL, aPostDataRef.value] = PlacesUtils.getURLAndPostDataForKeyword(keyword);
+    } catch (e) {}
+
+    if (!shortcutURL)
+      return aURL;
+
+    let postData = "";
+    if (aPostDataRef.value)
+      postData = unescape(aPostDataRef.value);
+
+    if (/%s/i.test(shortcutURL) || /%s/i.test(postData)) {
+      let charset = "";
+      const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/;
+      let matches = shortcutURL.match(re);
+      if (matches)
+        [, shortcutURL, charset] = matches;
+      else {
+        // Try to get the saved character-set.
+        try {
+          // makeURI throws if URI is invalid.
+          // Will return an empty string if character-set is not found.
+          charset = PlacesUtils.history.getCharsetForURI(Util.makeURI(shortcutURL));
+        } catch (e) { dump("--- error " + e + "\n"); }
+      }
+
+      let encodedParam = "";
+      if (charset)
+        encodedParam = escape(convertFromUnicode(charset, param));
+      else // Default charset is UTF-8
+        encodedParam = encodeURIComponent(param);
+
+      shortcutURL = shortcutURL.replace(/%s/g, encodedParam).replace(/%S/g, param);
+
+      if (/%s/i.test(postData)) // POST keyword
+        aPostDataRef.value = getPostDataStream(postData, param, encodedParam, "application/x-www-form-urlencoded");
+    } else if (param) {
+      // This keyword doesn't take a parameter, but one was provided. Just return
+      // the original URL.
+      aPostDataRef.value = null;
+
+      return aURL;
+    }
+
+    return shortcutURL;
+  },
+
+  /**
    * Return the currently active <browser> object
    */
   get selectedBrowser() {
     return this._selectedTab.browser;
   },
 
   get tabs() {
     return this._tabs;