Use quotes in search query only when neccessary

Link for topic 'test'
before change: /q/topic:"test"
after change: /q/topic:test

Quotes are added only when there is whitespace in topic.
Same for hashtags.

Change-Id: I67cc8c8d0f977dcc99e0c583eba36b41ffbbc3e6
diff --git a/polygerrit-ui/app/elements/core/gr-router/gr-router.ts b/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
index 582d96b..8a23004 100644
--- a/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
+++ b/polygerrit-ui/app/elements/core/gr-router/gr-router.ts
@@ -66,6 +66,7 @@
 import {LocationChangeEventDetail} from '../../../types/events';
 import {GerritView, updateState} from '../../../services/router/router-model';
 import {firePageError} from '../../../utils/event-util';
+import {addQuotesWhen} from '../../../utils/string-util';
 
 const RoutePattern = {
   ROOT: '/',
@@ -485,11 +486,18 @@
       operators.push('branch:' + encodeURL(params.branch, false));
     }
     if (params.topic) {
-      operators.push('topic:"' + encodeURL(params.topic, false) + '"');
+      operators.push(
+        'topic:' +
+          addQuotesWhen(encodeURL(params.topic, false), /\s/.test(params.topic))
+      );
     }
     if (params.hashtag) {
       operators.push(
-        'hashtag:"' + encodeURL(params.hashtag.toLowerCase(), false) + '"'
+        'hashtag:' +
+          addQuotesWhen(
+            encodeURL(params.hashtag.toLowerCase(), false),
+            /\s/.test(params.hashtag)
+          )
       );
     }
     if (params.statuses) {
diff --git a/polygerrit-ui/app/elements/core/gr-router/gr-router_test.js b/polygerrit-ui/app/elements/core/gr-router/gr-router_test.js
index ec122ec..f7fe091 100644
--- a/polygerrit-ui/app/elements/core/gr-router/gr-router_test.js
+++ b/polygerrit-ui/app/elements/core/gr-router/gr-router_test.js
@@ -269,12 +269,12 @@
       };
       assert.equal(element._generateUrl(params),
           '/q/owner:a%2525b+project:c%2525d+branch:e%2525f+' +
-          'topic:"g%2525h"+status:op%2525en');
+          'topic:g%2525h+status:op%2525en');
 
       params.offset = 100;
       assert.equal(element._generateUrl(params),
           '/q/owner:a%2525b+project:c%2525d+branch:e%2525f+' +
-          'topic:"g%2525h"+status:op%2525en,100');
+          'topic:g%2525h+status:op%2525en,100');
       delete params.offset;
 
       // The presence of the query param overrides other params.
@@ -290,6 +290,19 @@
       };
       assert.equal(element._generateUrl(params),
           '/q/(status:a OR status:b OR status:c)');
+
+      params = {
+        view: GerritNav.View.SEARCH,
+        topic: 'test',
+      };
+      assert.equal(element._generateUrl(params),
+          '/q/topic:test');
+      params = {
+        view: GerritNav.View.SEARCH,
+        topic: 'test test',
+      };
+      assert.equal(element._generateUrl(params),
+          '/q/topic:"test+test"');
     });
 
     test('change', () => {
diff --git a/polygerrit-ui/app/utils/string-util.ts b/polygerrit-ui/app/utils/string-util.ts
index 36050ca..1b400cf 100644
--- a/polygerrit-ui/app/utils/string-util.ts
+++ b/polygerrit-ui/app/utils/string-util.ts
@@ -22,3 +22,7 @@
   if (count === 0) return '';
   return `${count} ${noun}` + (count > 1 ? 's' : '');
 }
+
+export function addQuotesWhen(string: string, cond: boolean): string {
+  return cond ? `"${string}"` : string;
+}