Bug 1636961 - Require a space in front of trailing non-search restriction tokens. r=adw
authorHarry Twyford <htwyford@mozilla.com>
Tue, 02 Jun 2020 18:23:47 +0000
changeset 533592 a2941bad7532ff314f94ae8e56cbec0be23815fb
parent 533591 f6fd4b85734e3ac604f91637a8636bfe10145831
child 533593 e9c7a59d4bc5a2bb46d36df9ab6f7b7b23a8f769
push id37474
push userabutkovits@mozilla.com
push dateWed, 03 Jun 2020 09:29:05 +0000
treeherdermozilla-central@bf162b065e1f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1636961
milestone79.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 1636961 - Require a space in front of trailing non-search restriction tokens. r=adw Differential Revision: https://phabricator.services.mozilla.com/D77853
browser/components/urlbar/UrlbarTokenizer.jsm
browser/components/urlbar/tests/unit/test_tokenizer.js
--- a/browser/components/urlbar/UrlbarTokenizer.jsm
+++ b/browser/components/urlbar/UrlbarTokenizer.jsm
@@ -279,29 +279,31 @@ function splitString(searchString) {
     // a token, if it's the first one and it includes a leading restriction char
     // or it's the last one and it includes a trailing restriction char.
     // This allows to not require the user to add artificial whitespaces to
     // enforce restrictions, for example typing questions would restrict to
     // search results.
     let token = tokens[i];
     if (!hasRestrictionToken && token.length > 1) {
       // Check for an unambiguous restriction char at the beginning of the
-      // first token, or at the end of the last token.
+      // first token, or at the end of the last token. We only count trailing
+      // restriction chars if they are the search restriction char, which is
+      // "?". This is to allow for a typed question to yield only search results.
       if (
         i == 0 &&
         chars.includes(token[0]) &&
         !UrlbarTokenizer.REGEXP_PERCENT_ENCODED_START.test(token)
       ) {
         hasRestrictionToken = true;
         accumulator.push(token[0]);
         accumulator.push(token.slice(1));
         continue;
       } else if (
         i == tokens.length - 1 &&
-        chars.includes(token[token.length - 1]) &&
+        token[token.length - 1] == UrlbarTokenizer.RESTRICT.SEARCH &&
         !UrlbarTokenizer.looksLikeUrl(token, { requirePath: true })
       ) {
         hasRestrictionToken = true;
         accumulator.push(token.slice(0, token.length - 1));
         accumulator.push(token[token.length - 1]);
         continue;
       }
     }
--- a/browser/components/urlbar/tests/unit/test_tokenizer.js
+++ b/browser/components/urlbar/tests/unit/test_tokenizer.js
@@ -41,20 +41,30 @@ add_task(async function test_tokenizer()
           type: UrlbarTokenizer.TYPE.RESTRICT_BOOKMARK,
         },
       ],
     },
     {
       desc: "boundary restriction char at end",
       searchString: `test${UrlbarTokenizer.RESTRICT.BOOKMARK}`,
       expectedTokens: [
+        {
+          value: `test${UrlbarTokenizer.RESTRICT.BOOKMARK}`,
+          type: UrlbarTokenizer.TYPE.TEXT,
+        },
+      ],
+    },
+    {
+      desc: "boundary search restriction char at end",
+      searchString: `test${UrlbarTokenizer.RESTRICT.SEARCH}`,
+      expectedTokens: [
         { value: "test", type: UrlbarTokenizer.TYPE.TEXT },
         {
-          value: UrlbarTokenizer.RESTRICT.BOOKMARK,
-          type: UrlbarTokenizer.TYPE.RESTRICT_BOOKMARK,
+          value: UrlbarTokenizer.RESTRICT.SEARCH,
+          type: UrlbarTokenizer.TYPE.RESTRICT_SEARCH,
         },
       ],
     },
     {
       desc: "separate restriction char in the middle",
       searchString: `test ${UrlbarTokenizer.RESTRICT.BOOKMARK} test`,
       expectedTokens: [
         { value: "test", type: UrlbarTokenizer.TYPE.TEXT },