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

100% Statements 11/11
89.29% Branches 25/28
100% Functions 1/1
100% Lines 11/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 28 29 30 31 32 33 34 35                  1x       100x   50x           41x 41x   41x 5x 36x 5x   31x     19x    
import type { ParsedTable, TableCellCoordinate } from 'roosterjs-content-model-types';
 
/**
 * @internal
 * Try to find the last logic cell of a merged table cell
 * @param parsedTable The parsed table
 * @param row Row index
 * @param col Column index
 */
export function findLastedCoInMergedCell(
    parsedTable: ParsedTable,
    coordinate: TableCellCoordinate
): TableCellCoordinate | null {
    let { row, col } = coordinate;
 
    while (
        row >= 0 &&
        col >= 0 &&
        row < parsedTable.length &&
        col < (parsedTable[row]?.length ?? 0)
    ) {
        const right = parsedTable[row]?.[col + 1];
        const below = parsedTable[row + 1]?.[col];
 
        if (right == 'spanLeft' || right == 'spanBoth') {
            col++;
        } else if (below == 'spanTop' || below == 'spanBoth') {
            row++;
        } else {
            return { row, col };
        }
    }
    return null;
}