All files / roosterjs-content-model-core/lib/coreApi/setDOMSelection addRangeToSelection.ts

83.33% Statements 10/12
75% Branches 12/16
100% Functions 1/1
81.82% Lines 9/11

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 281x         62x 59x   59x 59x 59x     59x   59x 59x                      
import { areSameRanges } from '../../corePlugin/cache/areSameSelections';
 
/**
 * @internal
 */
export function addRangeToSelection(doc: Document, range: Range, isReverted: boolean = false) {
    const selection = doc.defaultView?.getSelection();
 
    Eif (selection) {
        const currentRange = selection.rangeCount > 0 && selection.getRangeAt(0);
        Iif (currentRange && areSameRanges(currentRange, range)) {
            return;
        }
        selection.removeAllRanges();
 
        Eif (!isReverted) {
            selection.addRange(range);
        } else {
            selection.setBaseAndExtent(
                range.endContainer,
                range.endOffset,
                range.startContainer,
                range.startOffset
            );
        }
    }
}