Skip to content

Commit

Permalink
Merge pull request #105 from tawk/fix-malformed-td
Browse files Browse the repository at this point in the history
Treat non-numeric colspans as zero and handle them gracefully
  • Loading branch information
Malte Legenhausen committed Aug 17, 2016
2 parents 15ed4c9 + 64ae3e7 commit b73cc33
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function formatTable(elem, fn, options) {
rows.push(_.compact(tokens));
// Fill colspans with empty values
if (elem.attribs && elem.attribs.colspan) {
times = elem.attribs.colspan - 1;
times = elem.attribs.colspan - 1 || 0;
_.times(times, function() {
rows.push(['']);
});
Expand Down
17 changes: 17 additions & 0 deletions test/html-to-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ describe('html-to-text', function() {
var result = htmlToText.fromString(html, { tables: true });
expect(result).to.equal(resultExpected);
});

it('does handle non-integer colspan on td element gracefully', function () {
var html = 'Good morning Jacob, \
<TABLE> \
<CENTER> \
<TBODY> \
<TR> \
<TD colspan="abc">Lorem ipsum dolor sit amet.</TD> \
</TR> \
</CENTER> \
</TBODY> \
</TABLE> \
';
var resultExpected = 'Good morning Jacob, Lorem ipsum dolor sit amet.';
var result = htmlToText.fromString(html, { tables: true });
expect(result).to.equal(resultExpected);
});
});

describe('lists', function() {
Expand Down

0 comments on commit b73cc33

Please sign in to comment.