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

91.67% Statements 11/12
83.33% Branches 20/24
100% Functions 1/1
91.67% Lines 11/12

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 43 44                                  1x       118x   59x           52x   52x   52x 42x 10x 5x   5x     17x    
import type { ParsedTable, TableCellCoordinate } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export interface TableCellCoordinateWithCell extends TableCellCoordinate {
    cell: HTMLTableCellElement;
}
 
/**
 * @internal
 * Try to find a TD/TH element from the given row and col number from the given parsed table
 * @param parsedTable The parsed table
 * @param row Row index
 * @param col Column index
 * @param findLast True to find last merged cell instead of the first cell
 */
export function findTableCellElement(
    parsedTable: ParsedTable,
    coordinate: TableCellCoordinate
): TableCellCoordinateWithCell | null {
    let { row, col } = coordinate;
 
    while (
        row >= 0 &&
        col >= 0 &&
        row < parsedTable.length &&
        col < (parsedTable[row]?.length ?? 0)
    ) {
        const cell = parsedTable[row]?.[col];
 
        Iif (!cell) {
            break;
        } else if (typeof cell == 'object') {
            return { cell, row, col };
        } else if (cell == 'spanLeft' || cell == 'spanBoth') {
            col--;
        } else {
            row--;
        }
    }
    return null;
}