fix: handle empty sections in parseDataStreamToTree by ensuring an … (#6188)

This commit is contained in:
WEI ZHANG
2025-11-24 15:05:00 +08:00
committed by GitHub
parent 3990117529
commit 0cbc370497
2 changed files with 56 additions and 0 deletions
@@ -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('');
});
});
});
@@ -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];