Skip to content

Commit b3ce26c

Browse files
committed
Test reading of table rows when non-rows are present
1 parent 0c87234 commit b3ce26c

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

mammoth/docx/body_xml.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ def calculate_row_spans(rows):
399399
for row in rows
400400
)
401401
if unexpected_non_rows:
402+
remove_unmerged_table_cells(rows)
402403
return _elements_result_with_messages(rows, [results.warning(
403404
"unexpected non-row element in table, cell merging may be incorrect"
404405
)])
@@ -438,6 +439,17 @@ def calculate_row_spans(rows):
438439
return _success(rows)
439440

440441

442+
def remove_unmerged_table_cells(rows):
443+
for row in rows:
444+
for cell_index, cell in enumerate(row.children):
445+
if isinstance(cell, documents.TableCellUnmerged):
446+
row.children[cell_index] = documents.table_cell(
447+
children=cell.children,
448+
colspan=cell.colspan,
449+
rowspan=cell.rowspan,
450+
)
451+
452+
441453
def read_child_elements(element):
442454
return _read_xml_elements(element.children)
443455

tests/docx/body_xml_tests.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,49 @@ def test_when_row_is_marked_as_deleted_in_row_properties_then_row_is_ignored(sel
11181118

11191119

11201120
def test_warning_if_non_row_in_table(self):
1121-
element = xml_element("w:tbl", {}, [xml_element("w:p")])
1121+
# Include normal rows to ensure they're still read correctly.
1122+
element = xml_element("w:tbl", {}, [
1123+
xml_element("w:tr", {}, [
1124+
xml_element("w:tc", {}, [
1125+
xml_element("w:p", {}, [
1126+
_run_element_with_text("Row 1"),
1127+
]),
1128+
]),
1129+
]),
1130+
xml_element("w:p"),
1131+
xml_element("w:tr", {}, [
1132+
xml_element("w:tc", {}, [
1133+
xml_element("w:p", {}, [
1134+
_run_element_with_text("Row 2"),
1135+
]),
1136+
]),
1137+
]),
1138+
])
1139+
11221140
result = _read_document_xml_element(element)
1141+
1142+
expected_value = documents.table([
1143+
documents.table_row([
1144+
documents.table_cell([
1145+
documents.paragraph([
1146+
documents.run([
1147+
documents.text("Row 1"),
1148+
]),
1149+
]),
1150+
]),
1151+
]),
1152+
documents.paragraph([]),
1153+
documents.table_row([
1154+
documents.table_cell([
1155+
documents.paragraph([
1156+
documents.run([
1157+
documents.text("Row 2"),
1158+
]),
1159+
]),
1160+
]),
1161+
]),
1162+
])
1163+
assert_equal(expected_value, result.value)
11231164
expected_warning = results.warning("unexpected non-row element in table, cell merging may be incorrect")
11241165
assert_equal([expected_warning], result.messages)
11251166

0 commit comments

Comments
 (0)