summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-10-02 12:27:24 +0100
committerMichael Williamson <mike@zwobble.org>2016-10-02 12:27:33 +0100
commit6049d3d2bc3fbf42d6b4bd4e93c17f20e2029abc (patch)
tree6478901eb5d832b93c0ea5786bd815015590bdfa
parentdc5346236b9ff2c189900bcc3e79d6450e42cdc7 (diff)
Ignore v:imagedata elements without relationship ID with warning
-rw-r--r--NEWS4
-rw-r--r--mammoth/docx/body_xml.py9
-rw-r--r--tests/docx/body_xml_tests.py9
3 files changed, 20 insertions, 2 deletions
diff --git a/NEWS b/NEWS
index b3c8205..04cf290 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+# 1.2.1
+
+* Ignore v:imagedata elements without relationship ID with warning.
+
# 1.2.0
* Use alt text title as alt text for images when the alt text description is
diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py
index fbf813c..b5e2997 100644
--- a/mammoth/docx/body_xml.py
+++ b/mammoth/docx/body_xml.py
@@ -329,8 +329,13 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f
return image_path, open_image
def read_imagedata(element):
- title = element.attributes.get("o:title")
- return _read_image(lambda: _find_embedded_image(element.attributes["r:id"]), title)
+ relationship_id = element.attributes.get("r:id")
+ if relationship_id is None:
+ warning = results.warning("A v:imagedata element without a relationship ID was ignored")
+ return _empty_result_with_message(warning)
+ else:
+ title = element.attributes.get("o:title")
+ return _read_image(lambda: _find_embedded_image(relationship_id), title)
def note_reference_reader(note_type):
def note_reference(element):
diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py
index 678d17f..b48c239 100644
--- a/tests/docx/body_xml_tests.py
+++ b/tests/docx/body_xml_tests.py
@@ -447,6 +447,15 @@ class ReadXmlElementTests(object):
assert_equal("image/png", image.content_type)
with image.open() as image_file:
assert_equal(self.IMAGE_BYTES, image_file.read())
+
+ @istest
+ def when_imagedata_element_has_no_relationship_id_then_it_is_ignored_with_error(self):
+ imagedata_element = xml_element("v:imagedata")
+
+ result = _read_document_xml_element(imagedata_element)
+ expected_warning = results.warning("A v:imagedata element without a relationship ID was ignored")
+ assert_equal([expected_warning], result.messages)
+ assert_equal(None, result.value)
@istest