Last active
January 10, 2020 20:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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