All files / roosterjs-content-model-dom/lib/domUtils/table parseTableCells.ts

100% Statements 21/21
80% Branches 8/10
100% Functions 3/3
100% Lines 15/15

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 431x               1x 80x 158x   80x 158x   299x   299x   299x 318x 341x 341x                         158x 341x       80x    
import { toArray } from '../toArray';
import type { ParsedTable } from 'roosterjs-content-model-types';
 
/**
 * Parse a table into a two dimensions array of TD elements. For those merged cells, the value will be null.
 * @param table Input HTML Table element
 * @returns Array of TD elements
 */
export function parseTableCells(table: HTMLTableElement): ParsedTable {
    const trs = toArray(table.rows);
    const cells: ParsedTable = trs.map(row => []);
 
    trs.forEach((tr, rowIndex) => {
        for (let sourceCol = 0, targetCol = 0; sourceCol < tr.cells.length; sourceCol++) {
            // Skip the cells which already initialized
            for (; cells[rowIndex][targetCol] !== undefined; targetCol++) {}
 
            const td = tr.cells[sourceCol];
 
            for (let colSpan = 0; colSpan < td.colSpan; colSpan++, targetCol++) {
                for (let rowSpan = 0; rowSpan < td.rowSpan; rowSpan++) {
                    Eif (cells[rowIndex + rowSpan]) {
                        cells[rowIndex + rowSpan][targetCol] =
                            colSpan == 0
                                ? rowSpan == 0
                                    ? td
                                    : 'spanTop'
                                : rowSpan == 0
                                ? 'spanLeft'
                                : 'spanBoth';
                    }
                }
            }
        }
 
        for (let col = 0; col < cells[rowIndex].length; col++) {
            cells[rowIndex][col] = cells[rowIndex][col] || null;
        }
    });
 
    return cells;
}