Skip to content

Commit

Permalink
move mapTable, fixTable out, separate tests for xml
Browse files Browse the repository at this point in the history
  • Loading branch information
goose-life committed Aug 18, 2024
1 parent 894e75d commit ab0ba4e
Show file tree
Hide file tree
Showing 3 changed files with 596 additions and 77 deletions.
88 changes: 47 additions & 41 deletions src/xml.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
/**
* Fixes all tables in a list of Akoma Ntoso XML elements.
* Returns a map of a table, taking all colspans and rowspans into account.
*/
export function fixTables (elementList) {
function fixTable(table) {
export function mapTable(table) {
let matrix = {},
rows = table.children;

for (let y = 0; y < rows.length; y++) {
// set default
if (!matrix[y]) {
matrix[y] = {};
}
let row = rows[y];
let cells = row.children;
for (let x = 0; x < cells.length; x++) {
// stash 'x' so we don't end the loop prematurely if it's incremented below
let xPos = x;
let cell = cells[x];
// set default; increment xPos if needed (skip already occupied cells in current row)
while (matrix[y][xPos]) {
xPos += 1;
}
matrix[y][xPos] = {};
// mark matrix elements occupied by current cell with true
let colSpanTotal = xPos + Number(cell.getAttribute('colspan') || 1);
let rowSpanTotal = y + Number(cell.getAttribute('rowspan') || 1);

for (let xx = xPos; xx < colSpanTotal; xx++) {
for (let yy = y; yy < rowSpanTotal; yy++) {
if (!matrix[yy]) {
matrix[yy] = {};
}
matrix[yy][xx] = true;
}
}
}
}
return matrix;
}

/**
* Fixes a table by inserting missing rows and cells to match the table's matrix.
*/
export function fixTable(table) {
let xmlns = table.namespaceURI,
tableMap = mapTable(table),
nMappedRows = Object.keys(tableMap).length;

// add missing rows
let nMissingRows = nMappedRows - table.childNodes.length;
let nMissingRows = nMappedRows - table.children.length;
for (let y = 0; y < nMissingRows; y++) {
console.log("adding a missing row to table ", table.getAttribute('eId'));
table.appendChild(document.createElementNS(xmlns, 'tr'));
Expand All @@ -28,43 +67,10 @@ export function fixTables (elementList) {
}
}

function mapTable(table) {
let matrix = {},
rows = table.children;

for (let y = 0; y < rows.length; y++) {
// set default
if (!matrix[y]) {
matrix[y] = {};
}
let row = rows[y];
let cells = row.children;
for (let x = 0; x < cells.length; x++) {
// stash 'x' so we don't end the loop prematurely if it's incremented below
let xPos = x;
let cell = cells[x];
// set default; increment xPos if needed (skip already occupied cells in current row)
while (matrix[y][xPos]) {
xPos += 1;
}
matrix[y][xPos] = {};
// mark matrix elements occupied by current cell with true
let colSpanTotal = xPos + Number(cell.getAttribute('colspan') || 1);
let rowSpanTotal = y + Number(cell.getAttribute('rowspan') || 1);

for (let xx = xPos; xx < colSpanTotal; xx++) {
for (let yy = y; yy < rowSpanTotal; yy++) {
if (!matrix[yy]) {
matrix[yy] = {};
}
matrix[yy][xx] = true;
}
}
}
}
return matrix;
}

/**
* Fixes all tables in a list of Akoma Ntoso XML elements.
*/
export function fixTables (elementList) {
for (let i = 0; i < elementList.length; i++) {
let tableList = elementList[i].querySelectorAll("table");
for (let t = 0; t < tableList.length; t++) {
Expand Down
36 changes: 0 additions & 36 deletions tests/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,40 +189,4 @@ describe('htmlToAkn', () => {
const akn = convert(`<div><div>first</div><div>second</div></div>`, true);
expect(akn).to.eql(`<p xmlns="http://docs.oasis-open.org/legaldocml/ns/akn/3.0"><p>first</p><p>second</p></p>`);
});

it('should insert missing rows and cells as required to get a clean table matrix', () => {
const akn = convert(`<table id="hcontainer_1__table_1" data-eid="hcontainer_1__table_1">
<tbody><tr>
<th><span id="hcontainer_1__table_1__p_1" data-eid="hcontainer_1__table_1__p_1">Heading 1</span></th>
<th><span id="hcontainer_1__table_1__p_2" data-eid="hcontainer_1__table_1__p_2">Heading 2</span></th>
</tr>
<tr>
<td class="akn-td" rowspan="5" colspan="1"><span id="hcontainer_1__table_1__p_3" data-eid="hcontainer_1__table_1__p_3">&nbsp;</span></td>
<td class="akn-td" rowspan="2" colspan="1"><span id="hcontainer_1__table_1__p_4" data-eid="hcontainer_1__table_1__p_4">&nbsp;</span></td>
</tr>
<tr><td class="akn-td"><span id="hcontainer_1__table_1__p_5" data-eid="hcontainer_1__table_1__p_5">&nbsp;</span></td></tr>
<tr>
<td class="akn-td"><span id="hcontainer_1__table_1__p_6" data-eid="hcontainer_1__table_1__p_6">Content 1</span></td>
<td class="akn-td"><span id="hcontainer_1__table_1__p_7" data-eid="hcontainer_1__table_1__p_7">Content 2</span></td>
</tr>
</tbody></table>`);
expect(akn).to.eql(`<table xmlns="http://docs.oasis-open.org/legaldocml/ns/akn/3.0" eId="hcontainer_1__table_1">
<tr> <th><p>Heading 1</p></th> <th><p>Heading 2</p></th> <td/></tr>
<tr> <td class="akn-td" rowspan="5" colspan="1"><p/></td> <td class="akn-td" rowspan="2" colspan="1"><p/></td> <td/></tr>
<tr>
<td class="akn-td">
<p/>
</td>
</tr>
<tr> <td class="akn-td"><p>Content 1</p></td> <td class="akn-td"><p>Content 2</p></td> </tr>
<tr>
<td/>
<td/>
</tr>
<tr>
<td/>
<td/>
</tr>
</table>`);
});
});
Loading

0 comments on commit ab0ba4e

Please sign in to comment.