Bug 1256074 - Always present a 'search' option when the location bar search is "fixed" to an URL. r=mak draft
authorEvan Tseng <evan@tseng.io>
Wed, 05 Oct 2016 17:39:08 +0800
changeset 426778 fdf4fdbdd265369ddbad3da84cf5885cac16af98
parent 426358 64b5c3fc34ede1862d6f597ed3483a6fbb067d98
child 534261 a5b107f922c91c322e8b330a0a1358721b722ba0
push id32794
push userbmo:evan@tseng.io
push dateWed, 19 Oct 2016 03:06:24 +0000
reviewersmak
bugs1256074
milestone52.0a1
Bug 1256074 - Always present a 'search' option when the location bar search is "fixed" to an URL. r=mak MozReview-Commit-ID: 7olZPhnmjTH
toolkit/components/places/UnifiedComplete.js
toolkit/components/places/tests/unifiedcomplete/test_searchSuggestions.js
toolkit/components/places/tests/unifiedcomplete/test_tabmatches.js
toolkit/components/places/tests/unifiedcomplete/test_visiturl.js
--- a/toolkit/components/places/UnifiedComplete.js
+++ b/toolkit/components/places/UnifiedComplete.js
@@ -612,20 +612,21 @@ function makeKeyForURL(actionUrl) {
       // the |placeId| that's also used to remove duplicate entries.
   }
   return stripHttpAndTrim(actionUrl);
 }
 
 /**
  * Returns whether the passed in string looks like a url.
  */
-function looksLikeUrl(str) {
+function looksLikeUrl(str, ignoreAlphanumericHosts = false) {
   // Single word not including special chars.
   return !REGEXP_SPACES.test(str) &&
-         ["/", "@", ":", "."].some(c => str.includes(c));
+         (["/", "@", ":", "["].some(c => str.includes(c)) ||
+          (ignoreAlphanumericHosts ? /(.*\..*){3,}/.test(str) : str.includes(".")));
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 
 /**
  * Manages a single instance of an autocomplete search.
  *
  * The first three parameters all originate from the similarly named parameters
@@ -991,16 +992,28 @@ Search.prototype = {
       // we use a fallback for things we don't know about.
 
       // We may not have auto-filled, but this may still look like a URL.
       // However, even if the input is a valid URL, we may not want to use
       // it as such. This can happen if the host would require whitelisting,
       // but isn't in the whitelist.
       let matched = yield this._matchUnknownUrl();
       if (matched) {
+        // Since we can't tell if this is a real URL and
+        // whether the user wants to visit or search for it,
+        // we always provide an alternative searchengine match.
+        try {
+          new URL(this._originalSearchString);
+        } catch (ex) {
+          if (Prefs.keywordEnabled && !looksLikeUrl(this._originalSearchString, true)) {
+            this._addingHeuristicFirstMatch = false;
+            yield this._matchCurrentSearchEngine();
+            this._addingHeuristicFirstMatch = true;
+          }
+        }
         return true;
       }
     }
 
     if (this.pending && this._enableActions && this._originalSearchString) {
       // When all else fails, and the search string is non-empty, we search
       // using the current search engine.
       let matched = yield this._matchCurrentSearchEngine();
--- a/toolkit/components/places/tests/unifiedcomplete/test_searchSuggestions.js
+++ b/toolkit/components/places/tests/unifiedcomplete/test_searchSuggestions.js
@@ -474,16 +474,17 @@ add_task(function* prohibit_suggestions(
   do_register_cleanup(() => {
     Services.prefs.clearUserPref("browser.fixup.domainwhitelist.localhost");
   });
   yield check_autocomplete({
     search: "localhost",
     searchParam: "enable-actions",
     matches: [
       makeVisitMatch("localhost", "http://localhost/", { heuristic: true }),
+      makeSearchMatch("localhost", { engineName: ENGINE_NAME, heuristic: false })
     ],
   });
 
   // When using multiple words, we should still get suggestions:
   yield check_autocomplete({
     search: "localhost other",
     searchParam: "enable-actions",
     matches: [
@@ -521,24 +522,26 @@ add_task(function* prohibit_suggestions(
     Services.prefs.clearUserPref("browser.fixup.dns_first_for_single_words");
   });
 
   yield check_autocomplete({
     search: "localhost",
     searchParam: "enable-actions",
     matches: [
       makeVisitMatch("localhost", "http://localhost/", { heuristic: true }),
+      makeSearchMatch("localhost", { engineName: ENGINE_NAME, heuristic: false })
     ],
   });
 
   yield check_autocomplete({
     search: "somethingelse",
     searchParam: "enable-actions",
     matches: [
       makeVisitMatch("somethingelse", "http://somethingelse/", { heuristic: true }),
+      makeSearchMatch("somethingelse", { engineName: ENGINE_NAME, heuristic: false })
     ],
   });
 
   // When using multiple words, we should still get suggestions:
   yield check_autocomplete({
     search: "localhost other",
     searchParam: "enable-actions",
     matches: [
--- a/toolkit/components/places/tests/unifiedcomplete/test_tabmatches.js
+++ b/toolkit/components/places/tests/unifiedcomplete/test_tabmatches.js
@@ -20,17 +20,18 @@ add_task(function* test_tab_matches() {
   addOpenPages(uri3, 1);
   addOpenPages(uri4, 1);
 
   do_print("two results, normal result is a tab match");
   yield check_autocomplete({
     search: "abc.com",
     searchParam: "enable-actions",
     matches: [ makeVisitMatch("abc.com", "http://abc.com/", { heuristic: true }),
-               makeSwitchToTabMatch("http://abc.com/", { title: "ABC rocks" }) ]
+               makeSwitchToTabMatch("http://abc.com/", { title: "ABC rocks" }),
+               makeSearchMatch("abc.com", { heuristic: false }) ]
   });
 
   do_print("three results, one tab match");
   yield check_autocomplete({
     search: "abc",
     searchParam: "enable-actions",
     matches: [ makeSearchMatch("abc", { heuristic: true }),
                makeSwitchToTabMatch("http://abc.com/", { title: "ABC rocks" }),
--- a/toolkit/components/places/tests/unifiedcomplete/test_visiturl.js
+++ b/toolkit/components/places/tests/unifiedcomplete/test_visiturl.js
@@ -1,14 +1,48 @@
 add_task(function*() {
   do_print("visit url, no protocol");
   yield check_autocomplete({
     search: "mozilla.org",
     searchParam: "enable-actions",
-    matches: [ { uri: makeActionURI("visiturl", {url: "http://mozilla.org/", input: "mozilla.org"}), title: "http://mozilla.org/", style: [ "action", "visiturl", "heuristic" ] } ]
+    matches: [
+      { uri: makeActionURI("visiturl", {url: "http://mozilla.org/", input: "mozilla.org"}), title: "http://mozilla.org/", style: [ "action", "visiturl", "heuristic" ] },
+      { uri: makeActionURI("searchengine", {engineName: "MozSearch", input: "mozilla.org", searchQuery: "mozilla.org"}), title: "MozSearch", style: ["action", "searchengine"] }
+    ]
+  });
+
+  do_print("visit url, no protocol but with 2 dots");
+  yield check_autocomplete({
+    search: "www.mozilla.org",
+    searchParam: "enable-actions",
+    matches: [
+      { uri: makeActionURI("visiturl", {url: "http://www.mozilla.org/", input: "www.mozilla.org"}), title: "http://www.mozilla.org/", style: [ "action", "visiturl", "heuristic" ] },
+      { uri: makeActionURI("searchengine", {engineName: "MozSearch", input: "www.mozilla.org", searchQuery: "www.mozilla.org"}), title: "MozSearch", style: ["action", "searchengine"] }
+    ]
+  });
+
+  do_print("visit url, no protocol but with 3 dots");
+  yield check_autocomplete({
+    search: "www.mozilla.org.tw",
+    searchParam: "enable-actions",
+    matches: [ { uri: makeActionURI("visiturl", {url: "http://www.mozilla.org.tw/", input: "www.mozilla.org.tw"}), title: "http://www.mozilla.org.tw/", style: [ "action", "visiturl", "heuristic" ] } ]
+  });
+
+  do_print("visit url, with protocol but with 2 dots");
+  yield check_autocomplete({
+    search: "https://www.mozilla.org",
+    searchParam: "enable-actions",
+    matches: [ { uri: makeActionURI("visiturl", {url: "https://www.mozilla.org/", input: "https://www.mozilla.org"}), title: "https://www.mozilla.org/", style: [ "action", "visiturl", "heuristic" ] } ]
+  });
+
+  do_print("visit url, with protocol but with 3 dots");
+  yield check_autocomplete({
+    search: "https://www.mozilla.org.tw",
+    searchParam: "enable-actions",
+    matches: [ { uri: makeActionURI("visiturl", {url: "https://www.mozilla.org.tw/", input: "https://www.mozilla.org.tw"}), title: "https://www.mozilla.org.tw/", style: [ "action", "visiturl", "heuristic" ] } ]
   });
 
   do_print("visit url, with protocol");
   yield check_autocomplete({
     search: "https://mozilla.org",
     searchParam: "enable-actions",
     matches: [ { uri: makeActionURI("visiturl", {url: "https://mozilla.org/", input: "https://mozilla.org"}), title: "https://mozilla.org/", style: [ "action", "visiturl", "heuristic" ] } ]
   });
@@ -51,17 +85,20 @@ add_task(function*() {
   do_register_cleanup(() => {
     Services.prefs.clearUserPref("browser.fixup.domainwhitelist.firefox");
   });
 
   do_print("whitelisted host");
   yield check_autocomplete({
     search: "firefox",
     searchParam: "enable-actions",
-    matches: [ makeVisitMatch("firefox", "http://firefox/", { heuristic: true }) ]
+    matches: [
+      makeVisitMatch("firefox", "http://firefox/", { heuristic: true }),
+      makeSearchMatch("firefox", { heuristic: false })
+    ]
   });
 
   do_print("url with whitelisted host");
   yield check_autocomplete({
     search: "firefox/get",
     searchParam: "enable-actions",
     matches: [ makeVisitMatch("firefox/get", "http://firefox/get", { heuristic: true }) ]
   });