Merge "Replace nbsp with space in queries." into main
diff --git a/ui/src/base/string_utils.ts b/ui/src/base/string_utils.ts
index bc33b70..07b1419 100644
--- a/ui/src/base/string_utils.ts
+++ b/ui/src/base/string_utils.ts
@@ -115,3 +115,11 @@
 export function sqliteString(str: string): string {
   return `'${str.replace(/'/g, '\'\'')}'`;
 }
+
+// Chat apps (including G Chat) sometimes replace ASCII characters with similar
+// looking unicode characters that break code snippets.
+// This function attempts to undo these replacements.
+export function undoCommonChatAppReplacements(str: string): string {
+  // Replace non-breaking spaces with normal spaces.
+  return str.replace(/\u00A0/g, ' ');
+}
diff --git a/ui/src/frontend/app.ts b/ui/src/frontend/app.ts
index d623531..c8f80b8 100644
--- a/ui/src/frontend/app.ts
+++ b/ui/src/frontend/app.ts
@@ -18,6 +18,7 @@
 import {findRef} from '../base/dom_utils';
 import {FuzzyFinder} from '../base/fuzzy';
 import {assertExists} from '../base/logging';
+import {undoCommonChatAppReplacements} from '../base/string_utils';
 import {Actions} from '../common/actions';
 import {setTimestampFormat, TimestampFormat} from '../common/time';
 import {raf} from '../core/raf_scheduler';
@@ -423,7 +424,7 @@
       },
       onSubmit: (value, alt) => {
         runQueryInNewTab(
-            value,
+            undoCommonChatAppReplacements(value),
             alt ? 'Pinned query' : 'Omnibox query',
             alt ? undefined : 'omnibox_query');
       },
diff --git a/ui/src/frontend/query_page.ts b/ui/src/frontend/query_page.ts
index d914f98..9ae3043 100644
--- a/ui/src/frontend/query_page.ts
+++ b/ui/src/frontend/query_page.ts
@@ -17,6 +17,7 @@
 
 import {Disposable} from '../base/disposable';
 import {SimpleResizeObserver} from '../base/resize_observer';
+import {undoCommonChatAppReplacements} from '../base/string_utils';
 import {EngineProxy} from '../common/engine';
 import {QueryResponse, runQuery} from '../common/queries';
 import {raf} from '../core/raf_scheduler';
@@ -47,23 +48,25 @@
   state.queryResult = undefined;
   const engine = getEngine();
   if (engine) {
-    runQuery(query, engine).then((resp: QueryResponse) => {
-      addTab({
-        kind: QueryResultTab.kind,
-        tag: 'analyze_page_query',
-        config: {
-          query: query,
-          title: 'Standalone Query',
-          prefetchedResponse: resp,
-        },
-      });
-      // We might have started to execute another query. Ignore it in that case.
-      if (state.executedQuery !== query) {
-        return;
-      }
-      state.queryResult = resp;
-      raf.scheduleFullRedraw();
-    });
+    runQuery(undoCommonChatAppReplacements(query), engine)
+        .then((resp: QueryResponse) => {
+          addTab({
+            kind: QueryResultTab.kind,
+            tag: 'analyze_page_query',
+            config: {
+              query: query,
+              title: 'Standalone Query',
+              prefetchedResponse: resp,
+            },
+          });
+          // We might have started to execute another query. Ignore it in that
+          // case.
+          if (state.executedQuery !== query) {
+            return;
+          }
+          state.queryResult = resp;
+          raf.scheduleFullRedraw();
+        });
   }
   raf.scheduleDelayedFullRedraw();
 }