All files / roosterjs-content-model-core/lib/coreApi/restoreUndoSnapshot getPositionFromPath.ts

100% Statements 10/10
100% Branches 6/6
100% Functions 1/1
100% Lines 9/9

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 311x               1x     12x   12x 27x   27x           17x   10x       12x    
import { isNodeOfType } from 'roosterjs-content-model-dom';
import type { DOMInsertPoint } from 'roosterjs-content-model-types';
 
/**
 * @internal
 *
 * Use with paths generated by `getPath`.
 */
export function getPositionFromPath(node: Node, path: number[]): DOMInsertPoint {
    // Iterate with a for loop to avoid mutating the passed in element path stack
    // or needing to copy it.
    let offset: number = 0;
 
    for (let i = 0; i < path.length; i++) {
        offset = path[i];
 
        if (
            i < path.length - 1 &&
            node &&
            isNodeOfType(node, 'ELEMENT_NODE') &&
            node.childNodes.length > offset
        ) {
            node = node.childNodes[offset];
        } else {
            break;
        }
    }
 
    return { node, offset };
}