All files / roosterjs-content-model-dom/lib/domToModel/processors textWithSelectionProcessor.ts

100% Statements 24/24
100% Branches 11/11
100% Functions 1/1
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431x 1x 1x           1x 1281x 1281x 1281x 1281x   1281x 24x 24x 24x   24x   24x 24x     1281x 25x 25x   25x       13x     25x 25x     1281x    
import { addSelectionMarker } from '../utils/addSelectionMarker';
import { addTextSegment } from '../../modelApi/common/addTextSegment';
import { getRegularSelectionOffsets } from '../utils/getRegularSelectionOffsets';
import type { ElementProcessor } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export const textWithSelectionProcessor: ElementProcessor<Text> = (group, textNode, context) => {
    let txt = textNode.nodeValue || '';
    const offsets = getRegularSelectionOffsets(context, textNode);
    const txtStartOffset = offsets[0];
    let txtEndOffset = offsets[1];
 
    if (txtStartOffset >= 0) {
        const subText = txt.substring(0, txtStartOffset);
        addTextSegment(group, subText, context);
        context.isInSelection = true;
 
        addSelectionMarker(group, context, textNode, txtStartOffset);
 
        txt = txt.substring(txtStartOffset);
        txtEndOffset -= txtStartOffset;
    }
 
    if (txtEndOffset >= 0) {
        const subText = txt.substring(0, txtEndOffset);
        addTextSegment(group, subText, context);
 
        if (
            context.selection &&
            (context.selection.type != 'range' || !context.selection.range.collapsed)
        ) {
            addSelectionMarker(group, context, textNode, offsets[1]); // Must use offsets[1] here as the unchanged offset value, cannot use txtEndOffset since it has been modified
        }
 
        context.isInSelection = false;
        txt = txt.substring(txtEndOffset);
    }
 
    addTextSegment(group, txt, context);
};