Skip to content

Commit e7041cb

Browse files
committed
Ignore AlternateContent elements when there is no Fallback element
1 parent a095ca2 commit e7041cb

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Detect checkboxes, both as complex fields and structured document tags, and
44
convert them to checkbox inputs.
55

6+
* Ignore AlternateContent elements when there is no Fallback element.
7+
68
# 1.8.0
79

810
* Add style mapping for highlights.

mammoth/docx/body_xml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ def read_comment_reference(element):
569569
return _success(documents.comment_reference(element.attributes["w:id"]))
570570

571571
def alternate_content(element):
572-
return read_child_elements(element.find_child("mc:Fallback"))
572+
return read_child_elements(element.find_child_or_null("mc:Fallback"))
573573

574574
def read_sdt(element):
575575
checkbox = element.find_child_or_null("w:sdtPr").find_child("wordml:checkbox")

tests/docx/body_xml_tests.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,17 +1406,30 @@ def test_text_boxes_have_content_appended_after_containing_paragraph():
14061406
result = _read_and_get_document_xml_elements(paragraph)
14071407
assert_equal(result[1].style_id, "textbox-content")
14081408

1409-
def test_alternate_content_is_read_using_fallback():
1410-
element = xml_element("mc:AlternateContent", {}, [
1411-
xml_element("mc:Choice", {"Requires": "wps"}, [
1412-
_paragraph_with_style_id("first")
1413-
]),
1414-
xml_element("mc:Fallback", {}, [
1415-
_paragraph_with_style_id("second")
1409+
1410+
class AlternateContentTests(object):
1411+
def test_when_fallback_is_present_then_fallback_is_read(self):
1412+
element = xml_element("mc:AlternateContent", {}, [
1413+
xml_element("mc:Choice", {"Requires": "wps"}, [
1414+
_paragraph_with_style_id("first")
1415+
]),
1416+
xml_element("mc:Fallback", {}, [
1417+
_paragraph_with_style_id("second")
1418+
])
14161419
])
1417-
])
1418-
result = _read_and_get_document_xml_element(element)
1419-
assert_equal("second", result.style_id)
1420+
result = _read_and_get_document_xml_element(element)
1421+
assert_equal("second", result.style_id)
1422+
1423+
1424+
def test_when_fallback_is_not_present_then_element_is_ignored(self):
1425+
element = xml_element("mc:AlternateContent", {}, [
1426+
xml_element("mc:Choice", {"Requires": "wps"}, [
1427+
_paragraph_with_style_id("first")
1428+
]),
1429+
])
1430+
result = _read_and_get_document_xml_elements(element)
1431+
assert_equal([], result)
1432+
14201433

14211434
def test_sdt_is_read_using_sdt_content():
14221435
element = xml_element("w:sdt", {}, [

0 commit comments

Comments
 (0)