From dd8b47154b2f9109eb6041302902d85fad6ceb08 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 28 May 2025 20:01:21 +0100 Subject: Handle AlternateContent without Fallback in office_xml --- mammoth/docx/office_xml.py | 2 +- tests/docx/office_xml_tests.py | 48 ++++++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/mammoth/docx/office_xml.py b/mammoth/docx/office_xml.py index a988894..6506127 100644 --- a/mammoth/docx/office_xml.py +++ b/mammoth/docx/office_xml.py @@ -37,7 +37,7 @@ def read(fileobj): def _collapse_alternate_content(node): if isinstance(node, XmlElement): if node.name == "mc:AlternateContent": - return node.find_child("mc:Fallback").children + return node.find_child_or_null("mc:Fallback").children else: node.children = flat_map(_collapse_alternate_content, node.children) return [node] diff --git a/tests/docx/office_xml_tests.py b/tests/docx/office_xml_tests.py index f87fa75..05897db 100644 --- a/tests/docx/office_xml_tests.py +++ b/tests/docx/office_xml_tests.py @@ -6,19 +6,35 @@ from mammoth.docx import xmlparser as xml, office_xml from ..testing import assert_equal -def test_alternate_content_is_replaced_by_contents_of_fallback(): - xml_string = ( - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '') - - result = office_xml.read(io.StringIO(xml_string)) - assert_equal([xml.element("fallback")], result.children) +class AlternateContentTests(object): + def test_when_fallback_is_present_then_fallback_is_read(self): + xml_string = ( + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '') + + result = office_xml.read(io.StringIO(xml_string)) + assert_equal([xml.element("fallback")], result.children) + + + def test_when_fallback_is_not_present_then_element_is_ignored(self): + xml_string = ( + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '') + + result = office_xml.read(io.StringIO(xml_string)) + assert_equal([], result.children) -- cgit v1.2.3