Skip to content

Commit 2ec3338

Browse files
committed
handle un-parsable colspan values
fixes matthewwithanm#126
1 parent c1672ae commit 2ec3338

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

markdownify/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,13 @@ def convert_figcaption(self, el, text, convert_as_inline):
383383

384384
def convert_td(self, el, text, convert_as_inline):
385385
colspan = 1
386-
if 'colspan' in el.attrs:
386+
if 'colspan' in el.attrs and el['colspan'].isdigit():
387387
colspan = int(el['colspan'])
388388
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan
389389

390390
def convert_th(self, el, text, convert_as_inline):
391391
colspan = 1
392-
if 'colspan' in el.attrs:
392+
if 'colspan' in el.attrs and el['colspan'].isdigit():
393393
colspan = int(el['colspan'])
394394
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan
395395

@@ -406,7 +406,7 @@ def convert_tr(self, el, text, convert_as_inline):
406406
# first row and is headline: print headline underline
407407
full_colspan = 0
408408
for cell in cells:
409-
if "colspan" in cell.attrs:
409+
if 'colspan' in cell.attrs and cell['colspan'].isdigit():
410410
full_colspan += int(cell["colspan"])
411411
else:
412412
full_colspan += 1

tests/test_tables.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
<th>Age</th>
216216
</tr>
217217
<tr>
218-
<td>Jill</td>
218+
<td colspan="1">Jill</td>
219219
<td>Smith</td>
220220
<td>50</td>
221221
</tr>
@@ -226,6 +226,17 @@
226226
</tr>
227227
</table>"""
228228

229+
table_with_undefined_colspan = """<table>
230+
<tr>
231+
<th colspan="undefined">Name</th>
232+
<th>Age</th>
233+
</tr>
234+
<tr>
235+
<td colspan="-1">Jill</td>
236+
<td>Smith</td>
237+
</tr>
238+
</table>"""
239+
229240

230241
def test_table():
231242
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
@@ -240,3 +251,4 @@ def test_table():
240251
assert md(table_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
241252
assert md(table_with_caption) == 'TEXT\n\nCaption\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n\n'
242253
assert md(table_with_colspan) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
254+
assert md(table_with_undefined_colspan) == '\n\n| Name | Age |\n| --- | --- |\n| Jill | Smith |\n\n'

0 commit comments

Comments
 (0)