From 0cbc37049746ad13b637c5a28698922a9ddcfb35 Mon Sep 17 00:00:00 2001 From: WEI ZHANG Date: Mon, 24 Nov 2025 15:05:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20handle=20empty=20sections=20in=20`parseD?= =?UTF-8?q?ataStreamToTree`=20by=20ensuring=20an=20=E2=80=A6=20(#6188)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/document-view-model.spec.ts | 50 +++++++++++++++++++ .../docs/view-model/document-view-model.ts | 6 +++ 2 files changed, 56 insertions(+) create mode 100644 packages/engine-render/src/components/docs/view-model/__tests__/document-view-model.spec.ts diff --git a/packages/engine-render/src/components/docs/view-model/__tests__/document-view-model.spec.ts b/packages/engine-render/src/components/docs/view-model/__tests__/document-view-model.spec.ts new file mode 100644 index 0000000000..01f567ce9a --- /dev/null +++ b/packages/engine-render/src/components/docs/view-model/__tests__/document-view-model.spec.ts @@ -0,0 +1,50 @@ +/** + * 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 { DataStreamTreeNodeType, DataStreamTreeTokenType } from '@univerjs/core'; +import { describe, expect, it } from 'vitest'; +import { parseDataStreamToTree } from '../document-view-model'; + +describe('DocumentViewModel', () => { + describe('parseDataStreamToTree', () => { + it('should handle empty section correctly', () => { + const dataStream = DataStreamTreeTokenType.SECTION_BREAK; + const { sectionList } = parseDataStreamToTree(dataStream); + + expect(sectionList.length).toBe(1); + const section = sectionList[0]; + expect(section.nodeType).toBe(DataStreamTreeNodeType.SECTION_BREAK); + expect(section.children.length).toBe(1); + expect(section.children[0].nodeType).toBe(DataStreamTreeNodeType.PARAGRAPH); + expect(section.children[0].content).toBe(''); + }); + + it('should handle consecutive section breaks correctly', () => { + const dataStream = `Hello${DataStreamTreeTokenType.PARAGRAPH}${DataStreamTreeTokenType.SECTION_BREAK}${DataStreamTreeTokenType.SECTION_BREAK}`; + const { sectionList } = parseDataStreamToTree(dataStream); + + expect(sectionList.length).toBe(2); + + // First section + expect(sectionList[0].children.length).toBe(1); + expect(sectionList[0].children[0].content).toBe(`Hello${DataStreamTreeTokenType.PARAGRAPH}${DataStreamTreeTokenType.SECTION_BREAK}`); + + // Second section (empty) + expect(sectionList[1].children.length).toBe(1); + expect(sectionList[1].children[0].content).toBe(''); + }); + }); +}); diff --git a/packages/engine-render/src/components/docs/view-model/document-view-model.ts b/packages/engine-render/src/components/docs/view-model/document-view-model.ts index aeb9173010..6fcba6b648 100644 --- a/packages/engine-render/src/components/docs/view-model/document-view-model.ts +++ b/packages/engine-render/src/components/docs/view-model/document-view-model.ts @@ -111,6 +111,12 @@ export function parseDataStreamToTree(dataStream: string, tables?: ICustomTable[ const sectionNode = DataStreamTreeNode.create(DataStreamTreeNodeType.SECTION_BREAK); const tempParagraphList = tableCellList.length > 0 ? cellParagraphList : paragraphList; + if (tempParagraphList.length === 0) { + const emptyParagraph = DataStreamTreeNode.create(DataStreamTreeNodeType.PARAGRAPH, ''); + emptyParagraph.setIndexRange(i, i - 1); + tempParagraphList.push(emptyParagraph); + } + batchParent(sectionNode, tempParagraphList); const lastNode = tempParagraphList[tempParagraphList.length - 1];