All files / roosterjs-content-model-dom/lib/domUtils/selection getSelectionRootNode.ts

100% Statements 2/2
87.5% Branches 7/8
100% Functions 1/1
100% Lines 2/2

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 16x                    
import type { DOMSelection } from 'roosterjs-content-model-types';
 
/**
 * Get root node of a given DOM selection
 * For table selection, root node is the selected table
 * For image selection, root node is the selected image
 * For range selection, root node is the common ancestor container node of the selection range
 * @param selection The selection to get root node from
 */
export function getSelectionRootNode(selection: DOMSelection | undefined): Node | undefined {
    return !selection
        ? undefined
        : selection.type == 'range'
        ? selection.range.commonAncestorContainer
        : selection.type == 'table'
        ? selection.table
        : selection.type == 'image'
        ? selection.image
        : undefined;
}