Skip to content

Commit

Permalink
Fix #236416 (#2166)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong authored Oct 25, 2023
1 parent dd3d691 commit 4123318
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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<Node> = (group, element, context) => {
let index = 0;
const [nodeStartOffset, nodeEndOffset] = getRegularSelectionOffsets(context, element);
export const delimiterProcessor: ElementProcessor<Node> = (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;
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('delimiterProcessor', () => {
blockGroupType: 'Document',
blocks: [],
});
expect(delimiterProcessorFile.handleRegularSelection).toHaveBeenCalledTimes(3);
});

it('Delimiter with selection', () => {
Expand Down Expand Up @@ -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();
});
});

0 comments on commit 4123318

Please sign in to comment.