Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ def strip_pre(text):
return text


def _get_colspan(el):
value = el.get('colspan', '1')
if not value.isdecimal():
return 1

# Bound the value while reading it, before Python's integer-string limit
# can reject a long attribute. Leading zeros must not change its value.
colspan = 0
for digit in value:
colspan = colspan * 10 + int(digit)
if colspan >= 1000:
return 1000
return max(1, colspan)


def chomp(text):
"""
If the text in an inline tag like b, a, or em contains a leading or trailing
Expand Down Expand Up @@ -732,15 +747,11 @@ def convert_figcaption(self, el, text, parent_tags):
return '\n\n' + text.strip() + '\n\n'

def convert_td(self, el, text, parent_tags):
colspan = 1
if 'colspan' in el.attrs and el['colspan'].isdigit():
colspan = max(1, min(1000, int(el['colspan'])))
colspan = _get_colspan(el)
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan

def convert_th(self, el, text, parent_tags):
colspan = 1
if 'colspan' in el.attrs and el['colspan'].isdigit():
colspan = max(1, min(1000, int(el['colspan'])))
colspan = _get_colspan(el)
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan

def convert_tr(self, el, text, parent_tags):
Expand All @@ -758,12 +769,7 @@ def convert_tr(self, el, text, parent_tags):
)
overline = ''
underline = ''
full_colspan = 0
for cell in cells:
if 'colspan' in cell.attrs and cell['colspan'].isdigit():
full_colspan += max(1, min(1000, int(cell['colspan'])))
else:
full_colspan += 1
full_colspan = sum(_get_colspan(cell) for cell in cells)
if ((is_headrow
or (is_head_row_missing
and self.options['table_infer_header']))
Expand Down
35 changes: 35 additions & 0 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from .utils import md


Expand Down Expand Up @@ -319,3 +321,36 @@ def test_table_infer_header():
assert md(table_with_colspan, table_infer_header=True) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
assert md(table_with_undefined_colspan, table_infer_header=True) == '\n\n| Name | Age |\n| --- | --- |\n| Jill | Smith |\n\n'
assert md(table_with_colspan_missing_head, table_infer_header=True) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'


@pytest.mark.parametrize('colspan, width', [
pytest.param('9' * 5000, 1000, id='oversized'),
pytest.param('0' * 5000 + '2', 2, id='long-leading-zeros'),
pytest.param('0' * 5000, 1, id='long-zero'),
pytest.param('\u0660' * 5000 + '\u0662', 2, id='unicode-leading-zeros'),
pytest.param('1000', 1000, id='maximum'),
pytest.param('1001', 1000, id='above-maximum'),
pytest.param('2', 2, id='ordinary'),
pytest.param('0', 1, id='zero'),
pytest.param('\u0662', 2, id='unicode-decimal'),
pytest.param('\u00b2', 1, id='superscript'),
pytest.param('\u2461', 1, id='circled-digit'),
pytest.param('9' * 5000 + 'x', 1, id='long-invalid'),
pytest.param('undefined', 1, id='invalid'),
])
@pytest.mark.parametrize('cell_type', ['td', 'th'])
@pytest.mark.parametrize('infer_header', [False, True])
def test_table_colspan_is_bounded_before_integer_conversion(
colspan, width, cell_type, infer_header):
html = ('<p>Before</p><table><tr><{cell} colspan="{span}">Value</{cell}>'
'</tr></table><p>After</p>').format(cell=cell_type, span=colspan)
row = '| Value' + ' |' * width
delimiter = '| ' + ' | '.join(['---'] * width) + ' |'
if cell_type == 'th' or infer_header:
expected_table = row + '\n' + delimiter
else:
empty_header = '| ' + ' | '.join([''] * width) + ' |'
expected_table = empty_header + '\n' + delimiter + '\n' + row

assert md(html, table_infer_header=infer_header).strip() == (
'Before\n\n' + expected_table + '\n\nAfter')