Skip to content

Commit

Permalink
fix for missing table cells in incomplete tables
Browse files Browse the repository at this point in the history
  • Loading branch information
KillyMXI committed Feb 10, 2023
1 parent 6336d9b commit c049ac6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module.exports = {
'max-lines-per-function': ['error', 80],
'max-nested-callbacks': ['error', 4],
'max-params': ['error', 6],
'max-statements': ['error', 35],
'max-statements': ['error', 41],
'max-statements-per-line': ['error', { 'max': 2 }],
'multiline-ternary': ['error', 'always-multiline'],
'newline-per-chained-call': ['error', { 'ignoreChainWithDepth': 3 }],
Expand Down
49 changes: 33 additions & 16 deletions packages/html-to-text/src/table-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ function transposeInPlace (matrix, maxSize) {
const rowI = getRow(matrix, i);
for (let j = 0; j < i; j++) {
const rowJ = getRow(matrix, j);
const temp = rowI[j];
rowI[j] = rowJ[i];
rowJ[i] = temp;
if (rowI[j] || rowJ[i]) {
const temp = rowI[j];
rowI[j] = rowJ[i];
rowJ[i] = temp;
}
}
}
}
Expand All @@ -34,10 +36,17 @@ function putCellIntoLayout (cell, layout, baseRow, baseCol) {
}
}

function getOrInitOffset (offsets, index) {
if (offsets[index] === undefined) {
offsets[index] = (index === 0) ? 0 : 1 + getOrInitOffset(offsets, index - 1);
}
return offsets[index];
}

function updateOffset (offsets, base, span, value) {
offsets[base + span] = Math.max(
offsets[base + span] || 0,
offsets[base] + value
getOrInitOffset(offsets, base + span),
getOrInitOffset(offsets, base) + value
);
}

Expand Down Expand Up @@ -82,19 +91,27 @@ function tableToString (tableRows, rowSpacing, colSpacing) {
for (let x = 0; x < colNumber; x++) {
let y = 0;
let cell;
while (y < rowNumber && (cell = layout[x][y])) {
if (!cell.rendered) {
let cellWidth = 0;
for (let j = 0; j < cell.lines.length; j++) {
const line = cell.lines[j];
const lineOffset = rowOffsets[y] + j;
outputLines[lineOffset] = (outputLines[lineOffset] || '').padEnd(colOffsets[x]) + line;
cellWidth = (line.length > cellWidth) ? line.length : cellWidth;
const rowsInThisColumn = Math.min(rowNumber, layout[x].length);
while (y < rowsInThisColumn) {
cell = layout[x][y];
if (cell) {
if (!cell.rendered) {
let cellWidth = 0;
for (let j = 0; j < cell.lines.length; j++) {
const line = cell.lines[j];
const lineOffset = rowOffsets[y] + j;
outputLines[lineOffset] = (outputLines[lineOffset] || '').padEnd(colOffsets[x]) + line;
cellWidth = (line.length > cellWidth) ? line.length : cellWidth;
}
updateOffset(colOffsets, x, cell.colspan, cellWidth + colSpacing);
cell.rendered = true;
}
updateOffset(colOffsets, x, cell.colspan, cellWidth + colSpacing);
cell.rendered = true;
y += cell.rowspan;
} else {
const lineOffset = rowOffsets[y];
outputLines[lineOffset] = (outputLines[lineOffset] || '');
y++;
}
y += cell.rowspan;
}
}

Expand Down
17 changes: 17 additions & 0 deletions packages/html-to-text/test/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,23 @@ describe('tags', function () {
expect(htmlToText(html, options)).to.equal(expected);
});

it('should not miss content in tables with variable number of cells per row', function () {
const html = `
<table>
<tr><td>a</td></tr>
<tr><td>b</td><td>c</td></tr>
<tr></tr>
<tr><td>d</td></tr>
</table>`;
const expected = 'a\nb c\n\nd';
const options = {
selectors: [
{ selector: 'table', format: 'dataTable' }
]
};
expect(htmlToText(html, options)).to.equal(expected);
});

});

describe('custom formatting', function () {
Expand Down

0 comments on commit c049ac6

Please sign in to comment.