All files / roosterjs-content-model-core/lib/corePlugin/selection findCoordinate.ts

100% Statements 14/14
91.67% Branches 11/12
100% Functions 4/4
100% Lines 14/14

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    1x           1x         58x 58x     58x 53x 65x   65x         58x 6x 6x 7x     6x       58x    
import type { DOMHelper, ParsedTable, TableCellCoordinate } from 'roosterjs-content-model-types';
 
const TableCellSelector = 'TH,TD';
 
/**
 * @internal
 * Find coordinate of a given element from a parsed table
 */
export function findCoordinate(
    parsedTable: ParsedTable,
    element: Node,
    domHelper: DOMHelper
): TableCellCoordinate | null {
    const td = domHelper.findClosestElementAncestor(element, TableCellSelector);
    let result: TableCellCoordinate | null = null;
 
    // Try to do a fast check if both TD are in the given TABLE
    if (td) {
        parsedTable.some((row, rowIndex) => {
            const colIndex = td ? row.indexOf(td as HTMLTableCellElement) : -1;
 
            return (result = colIndex >= 0 ? { row: rowIndex, col: colIndex } : null);
        });
    }
 
    // For nested table scenario, try to find the outer TAble cells
    if (!result) {
        parsedTable.some((row, rowIndex) => {
            const colIndex = row.findIndex(
                cell => typeof cell == 'object' && cell.contains(element)
            );
 
            return (result = colIndex >= 0 ? { row: rowIndex, col: colIndex } : null);
        });
    }
 
    return result;
}