All files / roosterjs-content-model-dom/lib/domToModel/utils getRegularSelectionOffsets.ts

100% Statements 5/5
100% Branches 18/18
100% Functions 1/1
100% Lines 5/5

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                  1x       4455x   4455x 4455x   4455x    
import type { DomToModelContext } from 'roosterjs-content-model-types';
 
/**
 * Get offset numbers of a regular (range based) selection.
 * If the selection start/end position is not in the given node, it will return -1 for the related value
 * @param context DOM to Content Model context used for retrieve the selection
 * @param currentContainer The container node to check
 * @returns a tuple of start and end offsets. -1 means selection is not directly under the given node
 */
export function getRegularSelectionOffsets(
    context: DomToModelContext,
    currentContainer: Node
): [number, number] {
    const range = context.selection?.type == 'range' ? context.selection.range : null;
 
    const startOffset = range?.startContainer == currentContainer ? range.startOffset : -1;
    const endOffset = range?.endContainer == currentContainer ? range.endOffset! : -1;
 
    return [startOffset, endOffset];
}