From 232fdd195a3df44a258d65eaf84d06f2ab36e988 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 10 Feb 2022 15:57:56 +0100 Subject: [PATCH] Unescape regex string (#45137) (#45244) (cherry picked from commit ec48b81388ccc5c139449944a4ec9e8f32da03e9) Co-authored-by: Ashley Harrison --- public/app/features/panel/state/util.ts | 6 +++--- public/app/features/panel/state/utils.test.ts | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/public/app/features/panel/state/util.ts b/public/app/features/panel/state/util.ts index 5db12bc1534..a12a748400a 100644 --- a/public/app/features/panel/state/util.ts +++ b/public/app/features/panel/state/util.ts @@ -1,4 +1,4 @@ -import { PanelPluginMeta, PluginState } from '@grafana/data'; +import { PanelPluginMeta, PluginState, unEscapeStringFromRegex } from '@grafana/data'; import { config } from 'app/core/config'; export function getAllPanelPluginMeta(): PanelPluginMeta[] { @@ -12,7 +12,7 @@ export function getAllPanelPluginMeta(): PanelPluginMeta[] { export function filterPluginList( pluginsList: PanelPluginMeta[], - searchQuery: string, + searchQuery: string, // Note: this will be an escaped regex string as it comes from `FilterInput` current: PanelPluginMeta ): PanelPluginMeta[] { if (!searchQuery.length) { @@ -24,7 +24,7 @@ export function filterPluginList( }); } - const query = searchQuery.toLowerCase(); + const query = unEscapeStringFromRegex(searchQuery).toLowerCase(); const first: PanelPluginMeta[] = []; const match: PanelPluginMeta[] = []; const isGraphQuery = 'graph'.startsWith(query); diff --git a/public/app/features/panel/state/utils.test.ts b/public/app/features/panel/state/utils.test.ts index bc664e0f572..6735aeb9a98 100644 --- a/public/app/features/panel/state/utils.test.ts +++ b/public/app/features/panel/state/utils.test.ts @@ -1,4 +1,4 @@ -import { PanelPluginMeta } from '@grafana/data'; +import { PanelPluginMeta, escapeStringForRegex } from '@grafana/data'; import { filterPluginList } from './util'; describe('panel state utils', () => { @@ -8,7 +8,18 @@ describe('panel state utils', () => { { id: 'timeseries', name: 'Graph (old)' }, { id: 'timeline', name: 'Timeline' }, ]; - const found = filterPluginList(pluginsList, 'gra', { id: 'xyz' } as any); + const found = filterPluginList(pluginsList, escapeStringForRegex('gra'), { id: 'xyz' } as any); expect(found.map((v) => v.id)).toEqual(['graph', 'timeseries']); }); + + it('should handle escaped regex characters in the search query (e.g. -)', async () => { + const pluginsList: PanelPluginMeta[] = [ + { id: 'graph', name: 'Graph (old)' } as any, + { id: 'timeseries', name: 'Graph (old)' }, + { id: 'timeline', name: 'Timeline' }, + { id: 'panelwithdashes', name: 'Panel-With-Dashes' }, + ]; + const found = filterPluginList(pluginsList, escapeStringForRegex('panel-'), { id: 'xyz' } as any); + expect(found.map((v) => v.id)).toEqual(['panelwithdashes']); + }); });