Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(copy): fix unescaped special characters such as `<>` in copied HTML (#6850)
wpxp1234567ba9b6aafa63
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.tsindex 13339158bd..4645defc39 100644--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts@@ -93,7 +93,7 @@ import {} from '../../commands/commands/clipboard.command';import { SetScrollOperation } from '../../commands/operations/scroll.operation';import { SHEETS_UI_PLUGIN_CONFIG_KEY } from '../../config/config';-import { ISheetClipboardService, PREDEFINED_HOOK_NAME_COPY, PREDEFINED_HOOK_NAME_PASTE } from '../../services/clipboard/clipboard.service';+import { escapeSpecialCode, ISheetClipboardService, PREDEFINED_HOOK_NAME_COPY, PREDEFINED_HOOK_NAME_PASTE } from '../../services/clipboard/clipboard.service';import { SheetSkeletonManagerService } from '../../services/sheet-skeleton-manager.service';import { ClipboardPopupMenu } from '../../views/clipboard/ClipboardPopupMenu';import { whenSheetEditorFocused } from '../shortcuts/utils';@@ -243,11 +243,19 @@ export class SheetClipboardController extends RxDisposable {},onCopyCellContent(row: number, col: number): string {const cell = currentSheet!.getCell(row, col);++ let content = '';if (cell?.p?.body?.paragraphs || cell?.p?.body?.textRuns) {- return convertBodyToHtml(cell.p);+ content = convertBodyToHtml(cell.p);+ } else if (cell) {+ content = extractPureTextFromCell(cell);}- const content = cell ? extractPureTextFromCell(cell) : '';- return content;++ /**+ * Used for generating the copied HTML, so we need to escape special code to avoid breaking the HTML structure.+ * For example, if the cell value contains <, > or &, it would break the HTML structure and cause the copied content to be incorrect.+ */+ return escapeSpecialCode(content);},onCopyCellStyle: (row: number, col: number, rowSpan?: number, colSpan?: number) => {const properties: IClipboardPropertyItem = {};diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-copy-with-escape-special-code.spec.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-copy-with-escape-special-code.spec.tsnew file mode 100644index 0000000000..59fa1f0e34--- /dev/null+++ b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-copy-with-escape-special-code.spec.ts@@ -0,0 +1,93 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { Injector, Univer } from '@univerjs/core';+import type { ISelectionWithStyle, ISetSelectionsOperationParams } from '@univerjs/sheets';+import { ICommandService, LocaleType } from '@univerjs/core';+import { SetRangeValuesMutation, SetSelectionsOperation } from '@univerjs/sheets';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { SheetCopyCommand } from '../../../commands/commands/clipboard.command';+import { ISheetClipboardService } from '../clipboard.service';+import { clipboardTestBed } from './clipboard-test-bed';++describe('Test clipboard', () => {+ let univer: Univer;+ let get: Injector['get'];+ let commandService: ICommandService;+ let sheetClipboardService: ISheetClipboardService;++ beforeEach(async () => {+ const testBed = clipboardTestBed({+ id: 'test',+ appVersion: '3.0.0-alpha',+ sheets: {+ sheet1: {+ id: 'sheet1',+ cellData: {+ 0: {+ 0: {+ v: 'List<int>_C',+ t: 1,+ },+ },+ },+ },+ },+ locale: LocaleType.EN_US,+ name: '',+ sheetOrder: [],+ styles: {},+ });++ univer = testBed.univer;+ get = testBed.get;++ commandService = get(ICommandService);+ commandService.registerCommand(SetRangeValuesMutation);+ commandService.registerCommand(SetSelectionsOperation);++ sheetClipboardService = get(ISheetClipboardService);+ });++ afterEach(() => {+ univer?.dispose();+ });++ describe('Copy cell content with escape special code', () => {+ it('cell need escape special code', async () => {+ const selection: ISelectionWithStyle = {+ range: { startRow: 0, startColumn: 0, endRow: 0, endColumn: 0 },+ primary: null,+ style: null,+ };+ expect(await commandService.executeCommand<ISetSelectionsOperationParams>(SetSelectionsOperation.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ selections: [selection],+ })).toBeTruthy();++ expect(await commandService.executeCommand(SheetCopyCommand.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ })).toBeTruthy();++ const { html, copyId } = sheetClipboardService.generateCopyContent('test', 'sheet1', selection.range)!;+ const result = `<google-sheets-html-origin><table data-copy-id="${copyId}" xmlns="http://www.w3.org/1999/xhtml" cellspacing="0" cellpadding="0" dir="ltr" style="table-layout:fixed;font-size:10pt;font-family:Arial;width:0px;border-collapse:collapse;border:none"><colgroup><col width="88" /></colgroup>+<tbody><tr style="height: 24px;"><td>List<int>_C</td></tr></tbody></table>`;+ expect(html).toBe(result);+ });+ });+});