/** * The borders `border-collapse: collapse` would merge, for a table that has * to be drawn with `separate` instead (a grid, or cells with rounded corners, * which a collapsed table never rounds). * * Separate draws both borders where two cells meet. Of each such pair only * one is kept: the wider, or the top/left cell's on a tie, the way collapse * resolves them. Where a merged cell meets several smaller ones, the merged * cell's border covers the whole edge, so the smaller cells give way. A cell * on the table's edge gives way to the table's own border there. * * Mirrored by renderer/Table/CollapsedBorders.php; keep the two in step. */ export type BorderSide = "top" | "right" | "bottom" | "left"; export type BorderWidths = Record; export interface BorderCell { id: string; /** 0-based row and column the cell starts at. */ row: number; col: number; rowSpan: number; colSpan: number; widths: BorderWidths; } export const NO_BORDER_WIDTHS: BorderWidths = { top: 0, right: 0, bottom: 0, left: 0, }; /** The sides to leave out, by cell id. */ export function resolveCollapsedBorders( cells: BorderCell[], rows: number, cols: number, tableWidths: BorderWidths = NO_BORDER_WIDTHS ): Map> { const hidden = new Map>(); const hide = (cell: BorderCell, side: BorderSide) => { const sides = hidden.get(cell.id) ?? new Set(); sides.add(side); hidden.set(cell.id, sides); }; const at: Array> = Array.from( { length: rows }, () => new Array(cols).fill(undefined) ); cells.forEach(cell => { for (let r = cell.row; r < cell.row + cell.rowSpan && r < rows; r++) { for ( let c = cell.col; c < cell.col + cell.colSpan && c < cols; c++ ) { at[r][c] = cell; } } }); // `first` is the top/left cell of the pair; the ranges are the rows (or // columns) along which the two meet. const settle = ( first: BorderCell, firstSide: BorderSide, firstRange: [number, number], second: BorderCell, secondSide: BorderSide, secondRange: [number, number] ) => { const firstWidth = first.widths[firstSide]; const secondWidth = second.widths[secondSide]; if (firstWidth <= 0 || secondWidth <= 0) { return; } const sameEdge = firstRange[0] === secondRange[0] && firstRange[1] === secondRange[1]; const secondCoversFirst = secondRange[0] <= firstRange[0] && secondRange[1] >= firstRange[1]; if (sameEdge) { if (firstWidth >= secondWidth) { hide(second, secondSide); } else { hide(first, firstSide); } } else if (secondCoversFirst) { hide(first, firstSide); } else { hide(second, secondSide); } }; cells.forEach(cell => { const rowRange: [number, number] = [ cell.row, cell.row + cell.rowSpan - 1, ]; const colRange: [number, number] = [ cell.col, cell.col + cell.colSpan - 1, ]; const rightCol = cell.col + cell.colSpan; if (rightCol < cols) { const seen = new Set(); for (let r = rowRange[0]; r <= rowRange[1] && r < rows; r++) { const right = at[r][rightCol]; if (right && !seen.has(right.id)) { seen.add(right.id); settle(cell, "right", rowRange, right, "left", [ right.row, right.row + right.rowSpan - 1, ]); } } } const belowRow = cell.row + cell.rowSpan; if (belowRow < rows) { const seen = new Set(); for (let c = colRange[0]; c <= colRange[1] && c < cols; c++) { const below = at[belowRow][c]; if (below && !seen.has(below.id)) { seen.add(below.id); settle(cell, "bottom", colRange, below, "top", [ below.col, below.col + below.colSpan - 1, ]); } } } if (cell.row === 0 && tableWidths.top > 0) { hide(cell, "top"); } if (cell.row + cell.rowSpan >= rows && tableWidths.bottom > 0) { hide(cell, "bottom"); } if (cell.col === 0 && tableWidths.left > 0) { hide(cell, "left"); } if (cell.col + cell.colSpan >= cols && tableWidths.right > 0) { hide(cell, "right"); } }); return hidden; } /** * The width of a CSS border shorthand like `2px solid #000`, in pixels; * 0 for none. Ems count as 16px; only the order of widths matters here. */ export function borderWidthOf(border: string | undefined | null): number { const value = (border ?? "").trim().toLowerCase(); if (value === "" || /\b(none|hidden)\b/.test(value)) { return 0; } const length = value.match(/(^|\s)(\d*\.?\d+)(px|r?em)?(?=\s|$)/); if (length) { const amount = parseFloat(length[2]); return length[3] === "em" || length[3] === "rem" ? amount * 16 : amount; } const keyword = value.match(/\b(thin|medium|thick)\b/); if (keyword) { return { thin: 1, medium: 3, thick: 5 }[keyword[1] as "thin"]; } // A style with no width draws the default, medium. return 3; }