Skip to content

Commit dd8b471

Browse files
committed
Handle AlternateContent without Fallback in office_xml
1 parent e7041cb commit dd8b471

2 files changed

Lines changed: 33 additions & 17 deletions

File tree

mammoth/docx/office_xml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def read(fileobj):
3737
def _collapse_alternate_content(node):
3838
if isinstance(node, XmlElement):
3939
if node.name == "mc:AlternateContent":
40-
return node.find_child("mc:Fallback").children
40+
return node.find_child_or_null("mc:Fallback").children
4141
else:
4242
node.children = flat_map(_collapse_alternate_content, node.children)
4343
return [node]

tests/docx/office_xml_tests.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,35 @@
66
from ..testing import assert_equal
77

88

9-
def test_alternate_content_is_replaced_by_contents_of_fallback():
10-
xml_string = (
11-
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
12-
'<numbering xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">' +
13-
'<mc:AlternateContent>' +
14-
'<mc:Choice Requires="w14">' +
15-
'<choice/>' +
16-
'</mc:Choice>' +
17-
'<mc:Fallback>' +
18-
'<fallback/>' +
19-
'</mc:Fallback>' +
20-
'</mc:AlternateContent>' +
21-
'</numbering>')
22-
23-
result = office_xml.read(io.StringIO(xml_string))
24-
assert_equal([xml.element("fallback")], result.children)
9+
class AlternateContentTests(object):
10+
def test_when_fallback_is_present_then_fallback_is_read(self):
11+
xml_string = (
12+
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
13+
'<numbering xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">' +
14+
'<mc:AlternateContent>' +
15+
'<mc:Choice Requires="w14">' +
16+
'<choice/>' +
17+
'</mc:Choice>' +
18+
'<mc:Fallback>' +
19+
'<fallback/>' +
20+
'</mc:Fallback>' +
21+
'</mc:AlternateContent>' +
22+
'</numbering>')
23+
24+
result = office_xml.read(io.StringIO(xml_string))
25+
assert_equal([xml.element("fallback")], result.children)
26+
27+
28+
def test_when_fallback_is_not_present_then_element_is_ignored(self):
29+
xml_string = (
30+
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
31+
'<numbering xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">' +
32+
'<mc:AlternateContent>' +
33+
'<mc:Choice Requires="w14">' +
34+
'<choice/>' +
35+
'</mc:Choice>' +
36+
'</mc:AlternateContent>' +
37+
'</numbering>')
38+
39+
result = office_xml.read(io.StringIO(xml_string))
40+
assert_equal([], result.children)

0 commit comments

Comments
 (0)