diff --git a/packages-content-model/roosterjs-content-model-dom/lib/domToModel/processors/delimiterProcessor.ts b/packages-content-model/roosterjs-content-model-dom/lib/domToModel/processors/delimiterProcessor.ts index fd6052e09b3..5f330cccb2a 100644 --- a/packages-content-model/roosterjs-content-model-dom/lib/domToModel/processors/delimiterProcessor.ts +++ b/packages-content-model/roosterjs-content-model-dom/lib/domToModel/processors/delimiterProcessor.ts @@ -1,23 +1,28 @@ -import { getRegularSelectionOffsets } from '../utils/getRegularSelectionOffsets'; -import { handleRegularSelection } from './childProcessor'; +import { addSelectionMarker } from '../utils/addSelectionMarker'; import type { ElementProcessor } from 'roosterjs-content-model-types'; /** * @internal * @param group - * @param element + * @param node * @param context */ -export const delimiterProcessor: ElementProcessor = (group, element, context) => { - let index = 0; - const [nodeStartOffset, nodeEndOffset] = getRegularSelectionOffsets(context, element); +export const delimiterProcessor: ElementProcessor = (group, node, context) => { + const range = context.selection?.type == 'range' ? context.selection.range : null; - for (let child = element.firstChild; child; child = child.nextSibling) { - handleRegularSelection(index, context, group, nodeStartOffset, nodeEndOffset); + if (range) { + if (node.contains(range.startContainer)) { + context.isInSelection = true; - delimiterProcessor(group, child, context); - index++; - } + addSelectionMarker(group, context); + } + + if (context.selection?.type == 'range' && node.contains(range.endContainer)) { + if (!context.selection.range.collapsed) { + addSelectionMarker(group, context); + } - handleRegularSelection(index, context, group, nodeStartOffset, nodeEndOffset); + context.isInSelection = false; + } + } }; diff --git a/packages-content-model/roosterjs-content-model-dom/test/domToModel/processors/delimiterProcessorTest.ts b/packages-content-model/roosterjs-content-model-dom/test/domToModel/processors/delimiterProcessorTest.ts index d63a35989fd..e2312476e6f 100644 --- a/packages-content-model/roosterjs-content-model-dom/test/domToModel/processors/delimiterProcessorTest.ts +++ b/packages-content-model/roosterjs-content-model-dom/test/domToModel/processors/delimiterProcessorTest.ts @@ -24,7 +24,6 @@ describe('delimiterProcessor', () => { blockGroupType: 'Document', blocks: [], }); - expect(delimiterProcessorFile.handleRegularSelection).toHaveBeenCalledTimes(3); }); it('Delimiter with selection', () => { @@ -65,4 +64,45 @@ describe('delimiterProcessor', () => { }); expect(context.isInSelection).toBeTrue(); }); + + it('Delimiter with selection end', () => { + const doc = createContentModelDocument(); + const text1 = document.createTextNode('test1'); + const text2 = document.createTextNode('test2'); + const span = document.createElement('span'); + const span2 = document.createElement('span'); + const div = document.createElement('div'); + + span.appendChild(text2); + + div.appendChild(text1); + div.appendChild(span); + div.appendChild(span2); + + context.selection = { + type: 'range', + range: createRange(text1, 2, text2, 3), + }; + + delimiterProcessor(doc, span, context); + + expect(doc).toEqual({ + blockGroupType: 'Document', + blocks: [ + { + blockType: 'Paragraph', + isImplicit: true, + format: {}, + segments: [ + { + segmentType: 'SelectionMarker', + isSelected: true, + format: {}, + }, + ], + }, + ], + }); + expect(context.isInSelection).toBeFalse(); + }); });