From 4ef1f4c04647a07ef5dd969c878280ad92e22812 Mon Sep 17 00:00:00 2001 From: JimmFly Date: Mon, 20 Nov 2023 10:49:32 +0800 Subject: [PATCH] fix(core): escape cmdk value (#4947) Co-authored-by: LongYinan --- .../core/src/components/pure/cmdk/data.tsx | 12 +++++++--- tests/affine-local/e2e/quick-search.spec.ts | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/frontend/core/src/components/pure/cmdk/data.tsx b/packages/frontend/core/src/components/pure/cmdk/data.tsx index 7c2bb695b6..143e74cc52 100644 --- a/packages/frontend/core/src/components/pure/cmdk/data.tsx +++ b/packages/frontend/core/src/components/pure/cmdk/data.tsx @@ -24,7 +24,7 @@ import { PreconditionStrategy, } from '@toeverything/infra/command'; import { atom, useAtomValue } from 'jotai'; -import { groupBy } from 'lodash-es'; +import { escape, groupBy } from 'lodash-es'; import { useCallback, useMemo } from 'react'; import { @@ -168,6 +168,7 @@ export const pageToCommand = ( const commandLabel = label || { title: title, }; + const escapedTitle = escape(title); return { id: page.id, @@ -175,8 +176,13 @@ export const pageToCommand = ( // hack: when comparing, the part between >>> and <<< will be ignored // adding this patch so that CMDK will not complain about duplicated commands value: - title + valueWrapperStart + page.id + '.' + category + valueWrapperEnd, - originalValue: title, + escapedTitle + + valueWrapperStart + + page.id + + '.' + + category + + valueWrapperEnd, + originalValue: escapedTitle, category: category, run: () => { if (!currentWorkspaceId) { diff --git a/tests/affine-local/e2e/quick-search.spec.ts b/tests/affine-local/e2e/quick-search.spec.ts index d86dcda8ef..236cb93d53 100644 --- a/tests/affine-local/e2e/quick-search.spec.ts +++ b/tests/affine-local/e2e/quick-search.spec.ts @@ -406,3 +406,25 @@ test('can use cmdk to search page content and scroll to it, then the block will const selectionElement = page.locator('affine-block-selection'); await expect(selectionElement).toBeVisible(); }); + +test('Create a new page with special characters in the title and search for this page', async ({ + page, +}) => { + const specialTitle = '"test123456"'; + + await openHomePage(page); + await waitForEditorLoad(page); + + await clickNewPageButton(page); + await getBlockSuiteEditorTitle(page).click(); + await getBlockSuiteEditorTitle(page).fill(specialTitle); + await openQuickSearchByShortcut(page); + + await page.keyboard.insertText(specialTitle); + await page.waitForTimeout(300); + + await assertResultList(page, [specialTitle, specialTitle]); + await page.keyboard.press('Enter'); + await page.waitForTimeout(300); + await assertTitle(page, specialTitle); +});