Skip to content

Commit 3e69791

Browse files
committed
Correction a table creation with rowspan and colspan
1 parent 8496679 commit 3e69791

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

index.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -254,19 +254,13 @@ function htmlToPdfMake(htmlText, options) {
254254
var header = ret.table.body[0];
255255
if (Array.isArray(header)) {
256256
// determine the number of columns
257-
var columnCount = header.some(function(cell) {
258-
return cell.colSpan > 0;
259-
})
260-
? header.reduce(function(partialCount, cell) {
261-
return partialCount + (cell.colSpan ? cell.colSpan : 0)
262-
}, 0)
263-
: header.length;
257+
var columnsCount = header.length;
264258
// determine the number of rows
265-
var rowCount = ret.table.body.length;
259+
var rowsCount = ret.table.body.length;
266260

267261
// for each column
268-
for (var columnInd=0; columnInd<columnCount; columnInd++) {
269-
for (var rowInd=0; rowInd<rowCount; rowInd++) {
262+
for (var columnInd=0; columnInd<columnsCount; columnInd++) {
263+
for (var rowInd=0; rowInd<rowsCount; rowInd++) {
270264
var row = ret.table.body[rowInd];
271265
if (Array.isArray(row)) {
272266
var cell = row[columnInd];

test/unit.js

+60
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,66 @@ test("unit tests", function(t) {
572572
t.finish();
573573
})
574574

575+
t.test("table (rowspan/colspan) with thead and tbody", function(t) {
576+
var html = `<table>
577+
<thead>
578+
<tr>
579+
<th rowspan="2">Col A</th>
580+
<th colspan="2">Col B & C</th>
581+
<th rowspan="2">Col D</th>
582+
</tr>
583+
<tr>
584+
<th>Col B</th>
585+
<th>Col C</th>
586+
</tr>
587+
</thead>
588+
<tbody>
589+
<tr>
590+
<td rowspan="2">Cell A1 & A2</td>
591+
<td>Cell B1</td>
592+
<td rowspan="2">Cell C1 & C2</td>
593+
<td>Cell D1</td>
594+
</tr>
595+
<tr>
596+
<td>Cell B2</td>
597+
<td>Cell D2</td>
598+
</tr>
599+
</tbody>
600+
</table>`;
601+
var ret = htmlToPdfMake(html, {window:window});
602+
if (debug) console.log(JSON.stringify(ret));
603+
t.check(Array.isArray(ret) && ret.length===1, "return is OK");
604+
ret = ret[0];
605+
606+
t.check(
607+
ret.table &&
608+
Array.isArray(ret.table.body) &&
609+
ret.table.body.length === 4, "base");
610+
t.check(
611+
ret.table.body[0].length === 4 &&
612+
ret.table.body[0][0].text === "Col A" &&
613+
ret.table.body[0][1].text === "Col B & C" &&
614+
ret.table.body[0][3].text === "Col D" &&
615+
ret.table.body[1].length === 4 &&
616+
ret.table.body[1][1].text === "Col B" &&
617+
ret.table.body[1][2].text === "Col C", "header");
618+
t.check(
619+
ret.table.body[2].length === 4 &&
620+
ret.table.body[2][0].text === "Cell A1 & A2" &&
621+
ret.table.body[2][1].text === "Cell B1" &&
622+
ret.table.body[2][2].text === "Cell C1 & C2" &&
623+
ret.table.body[2][3].text === "Cell D1", "row 1");
624+
t.check(
625+
ret.table.body[3].length === 4 &&
626+
ret.table.body[3][1].text === "Cell B2" &&
627+
ret.table.body[3][3].text === "Cell D2", "row 2");
628+
t.check(
629+
Array.isArray(ret.style) &&
630+
ret.style[0] === 'html-table', "table style");
631+
632+
t.finish();
633+
})
634+
575635
t.test("img",function(t) {
576636
var ret = htmlToPdfMake('<img width="10" style="height:10px" src="data:image/jpeg;base64,...encodedContent...">', {window:window});
577637
if (debug) console.log(JSON.stringify(ret));

0 commit comments

Comments
 (0)