diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 95eee9e..cf47e66 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -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 @@ -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): @@ -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'])) diff --git a/tests/test_tables.py b/tests/test_tables.py index 7e0670c..b943f03 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -1,3 +1,5 @@ +import pytest + from .utils import md @@ -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 = ('

Before

<{cell} colspan="{span}">Value' + '

After

').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')