Skip to content

Instantly share code, notes, and snippets.

@eirikb
Last active January 10, 2020 20:16
Show Gist options
  • Save eirikb/9d7aea3c792018c35fcc218882adf93d to your computer and use it in GitHub Desktop.
Save eirikb/9d7aea3c792018c35fcc218882adf93d to your computer and use it in GitHub Desktop.
DOM normailzation of table rows - creates a matrix with duplicate cells based on rowspan colspan
module.exports = table => {
const res = [];
table.querySelectorAll('tbody tr').forEach((row, y) =>
row.querySelectorAll('td').forEach((cell, x) => {
const rowspan = Number(cell.getAttribute('rowspan') || 1);
const colspan = Number(cell.getAttribute('colspan') || 1);
while (res[y] && res[y][x]) x++;
for (let yy = y; yy < y + rowspan; yy++) {
const resRow = res[yy] = res[yy] || [];
for (let j = 0; j < colspan; j++) {
resRow.row = row;
resRow[x + j] = cell;
}
}
})
);
return res.filter(row => row.length > 0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment