fix(core): escape cmdk value (#4947)

Co-authored-by: LongYinan <lynweklm@gmail.com>
This commit is contained in:
JimmFly
2023-11-20 10:49:32 +08:00
committed by GitHub
parent 9bab1b5dff
commit 4ef1f4c046
2 changed files with 31 additions and 3 deletions

View File

@@ -24,7 +24,7 @@ import {
PreconditionStrategy, PreconditionStrategy,
} from '@toeverything/infra/command'; } from '@toeverything/infra/command';
import { atom, useAtomValue } from 'jotai'; import { atom, useAtomValue } from 'jotai';
import { groupBy } from 'lodash-es'; import { escape, groupBy } from 'lodash-es';
import { useCallback, useMemo } from 'react'; import { useCallback, useMemo } from 'react';
import { import {
@@ -168,6 +168,7 @@ export const pageToCommand = (
const commandLabel = label || { const commandLabel = label || {
title: title, title: title,
}; };
const escapedTitle = escape(title);
return { return {
id: page.id, id: page.id,
@@ -175,8 +176,13 @@ export const pageToCommand = (
// hack: when comparing, the part between >>> and <<< will be ignored // hack: when comparing, the part between >>> and <<< will be ignored
// adding this patch so that CMDK will not complain about duplicated commands // adding this patch so that CMDK will not complain about duplicated commands
value: value:
title + valueWrapperStart + page.id + '.' + category + valueWrapperEnd, escapedTitle +
originalValue: title, valueWrapperStart +
page.id +
'.' +
category +
valueWrapperEnd,
originalValue: escapedTitle,
category: category, category: category,
run: () => { run: () => {
if (!currentWorkspaceId) { if (!currentWorkspaceId) {

View File

@@ -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'); const selectionElement = page.locator('affine-block-selection');
await expect(selectionElement).toBeVisible(); 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);
});