Skip to content

Commit 734a5fc

Browse files
Add ability to register colspans in table headers (#1)
* fixup of html-to-text#144 * update integration test to cover new colspan functionality * update unit tests to cover new colspan functionality
1 parent b642ec7 commit 734a5fc

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

lib/formatter.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,21 +221,22 @@ function formatTable(elem, fn, options) {
221221
switch (elem.name.toLowerCase()) {
222222
case 'th':
223223
tokens = formatHeading(elem, fn, options).split('\n');
224-
rows.push(compact(tokens));
225224
break;
226225

227226
case 'td':
228227
tokens = fn(elem.children, options).split('\n');
229-
rows.push(compact(tokens));
230-
// Fill colspans with empty values
231-
if (elem.attribs && elem.attribs.colspan) {
232-
count = elem.attribs.colspan - 1 || 0;
233-
times(count, function() {
234-
rows.push(['']);
235-
});
236-
}
237228
break;
238229
}
230+
if (tokens) {
231+
rows.push(compact(tokens));
232+
// Fill colspans with empty values
233+
if (elem.attribs && elem.attribs.colspan) {
234+
count = elem.attribs.colspan - 1 || 0;
235+
times(count, function() {
236+
rows.push(['']);
237+
});
238+
}
239+
}
239240
}
240241
});
241242
rows = helper.arrayZip(rows);

test/html-to-text.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,31 @@ describe('html-to-text', function() {
192192
var result = htmlToText.fromString(html, { tables: true });
193193
expect(result).to.equal(resultExpected);
194194
});
195+
it('does handle colspan on th elements correctly', function () {
196+
var html = '\
197+
<table> \
198+
<tr> \
199+
<th>header column 1</th> \
200+
<th colspan="2">header columns 2 and 3</th> \
201+
<th>header column 4</th> \
202+
</tr> \
203+
<tbody> \
204+
<tr> \
205+
<td>column 1</td> \
206+
<td>column 2</td> \
207+
<td>column 3</td> \
208+
<td>column 4</td> \
209+
</tr> \
210+
</center> \
211+
</tbody> \
212+
</table> \
213+
';
214+
var resultExpected = ' HEADER COLUMN 1 HEADER COLUMNS 2 AND 3 HEADER COLUMN 4 \n\
215+
column 1 column 2 column 3 column 4';
216+
var result = htmlToText.fromString(html, { tables: true });
217+
expect(result).to.equal(resultExpected);
218+
});
219+
195220
});
196221

197222
describe('a', function () {

test/test.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ <h2>Pretty printed table</h2>
3030
<thead>
3131
<tr>
3232
<th>Article</th>
33+
<th colspan="2">Notes</th>
3334
<th>Price</th>
3435
<th>Taxes</th>
3536
<th>Amount</th>
@@ -44,24 +45,30 @@ <h2>Pretty printed table</h2>
4445
<span style="font-size:0.8em">Contains: 1x Product 1</span>
4546
</p>
4647
</td>
48+
<td align="right" valign="top">one</td>
49+
<td align="right" valign="top">two</td>
4750
<td align="right" valign="top">6,99&euro;</td>
4851
<td align="right" valign="top">7%</td>
4952
<td align="right" valign="top">1</td>
5053
<td align="right" valign="top">6,99€</td>
5154
</tr>
5255
<tr>
5356
<td>Shipment costs</td>
57+
<td align="right" valign="top"></td>
58+
<td align="right" valign="top">two</td>
5459
<td align="right">3,25€</td>
5560
<td align="right">7%</td>
5661
<td align="right">1</td>
5762
<td align="right">3,25€</td>
5863
</tr>
5964
<tr>
65+
<td>&nbsp;</td>
6066
<td>&nbsp;</td>
6167
<td>&nbsp;</td>
6268
<td colspan="3">to pay: 10,24€</td>
6369
</tr>
6470
<tr>
71+
<td></td>
6572
<td></td>
6673
<td></td>
6774
<td colspan="3">Taxes 7%: 0,72€</td>

test/test.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ takimata sanctus est Lorem ipsum dolor sit amet.
1717
--------------------------------------------------------------------------------
1818

1919
PRETTY PRINTED TABLE
20-
ARTICLE PRICE TAXES AMOUNT TOTAL
21-
Product 1 6,99€ 7% 1 6,99€
22-
Contains: 1x Product 1
23-
Shipment costs 3,25€ 7% 1 3,25€
24-
to pay: 10,24€
25-
Taxes 7%: 0,72€
20+
ARTICLE NOTES PRICE TAXES AMOUNT TOTAL
21+
Product 1 one two 6,99€ 7% 1 6,99€
22+
Contains: 1x Product 1
23+
Shipment costs two 3,25€ 7% 1 3,25€
24+
to pay: 10,24€
25+
Taxes 7%: 0,72€
2626

2727

2828
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)