* Fills the `grid` and `posMap` of a `tableGrid` by traversing a given `TableNode` row. * * This function iterates through a linked list of `TableNode`s, which represents a single row in the * conceptual table. For each node in the row, it calculates the final `x` position and width, then * adds
(tableGrid: TableGrid, node: TableNode, startRow: number)
| 46 | * @returns An object containing the final `x` coordinate and the total `sumWidth` of the processed row. |
| 47 | */ |
| 48 | function fillGrid(tableGrid: TableGrid, node: TableNode, startRow: number) { |
| 49 | let x = node.x; |
| 50 | let sumWidth = 0; |
| 51 | |
| 52 | // The parent node may be wider than the sum of its child nodes, |
| 53 | // so the width difference needs to be allocated to the rightmost child node. |
| 54 | const adjustWidth = (nd: TableNode) => { |
| 55 | if (nd.parent && nd.borderFlags["right"]) { |
| 56 | nd.width = nd.parent.x + nd.parent.width - x; |
| 57 | } |
| 58 | nd.x = x; |
| 59 | }; |
| 60 | |
| 61 | const accWidth = (w: number) => { |
| 62 | x += w; |
| 63 | sumWidth += w; |
| 64 | }; |
| 65 | |
| 66 | for (let current: TableNode | undefined = node; current; current = current.right) { |
| 67 | if (isTableType(current.type)) { |
| 68 | adjustWidth(current); |
| 69 | const head = findHead(current.heads, startRow); |
| 70 | |
| 71 | if (head) { |
| 72 | head.x = x; |
| 73 | const sub = fillGrid(tableGrid, head, startRow); |
| 74 | accWidth(sub.sumWidth); |
| 75 | } |
| 76 | } else { |
| 77 | const nd = current.row === startRow ? current : newDummyNode(current, startRow); |
| 78 | adjustWidth(nd); |
| 79 | accWidth(nd.width); |
| 80 | addCell(tableGrid, nd, startRow); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return { x, sumWidth }; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * A helper function to add a cell to the grid and update the position map. |
no test coverage detected