summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-01-26 21:44:44 +0000
committerMichael Williamson <mike@zwobble.org>2015-01-26 21:44:44 +0000
commit2105118f9047eaacf60116f2d6b62b2d21c80f3a (patch)
tree14fb2241f90f39e9c7b93540a6c3a886f76ed169
parent5d50558b527175e9651a96a4609720884942c833 (diff)
Warn on unsupported image types
-rw-r--r--mammoth/docx/document_xml.py8
-rw-r--r--tests/docx/document_xml_tests.py31
2 files changed, 38 insertions, 1 deletions
diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py
index c4f0b14..2004948 100644
--- a/mammoth/docx/document_xml.py
+++ b/mammoth/docx/document_xml.py
@@ -219,7 +219,13 @@ def _create_reader(numbering, content_types, relationships, styles, note_element
return contextlib.closing(image_file)
image = documents.image(alt_text=alt_text, content_type=content_type, open=open_image)
- return results.success(image)
+
+ if content_type in ["image/png", "image/gif", "image/jpeg", "image/svg+xml", "image/tiff"]:
+ messages = []
+ else:
+ messages = [results.warning("Image of type {0} is unlikely to display in web browsers".format(content_type))]
+
+ return results.Result(image, messages)
def note_reference_reader(note_type):
@handler("w:{0}Reference".format(note_type))
diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py
index a2eefaf..ffa5fbe 100644
--- a/tests/docx/document_xml_tests.py
+++ b/tests/docx/document_xml_tests.py
@@ -312,6 +312,37 @@ class ReadXmlElementTests(object):
with image.open() as image_file:
assert_equal(image_bytes, image_file.read())
+
+ @istest
+ @funk.with_context
+ def warning_if_unsupported_image_type(self, context):
+ drawing_element = _create_inline_image(
+ relationship_id="rId5",
+ description="It's a hat",
+ )
+
+ image_bytes = b"Not an image at all!"
+
+ relationships = Relationships({
+ "rId5": Relationship(target="media/hat.emf")
+ })
+
+ docx_file = context.mock()
+ funk.allows(docx_file).open("word/media/hat.emf").returns(io.BytesIO(image_bytes))
+
+ content_types = context.mock()
+ funk.allows(content_types).find_content_type("word/media/hat.emf").returns("image/x-emf")
+
+ result = read_document_xml_element(
+ drawing_element,
+ content_types=content_types,
+ relationships=relationships,
+ docx_file=docx_file,
+ )
+ assert_equal("image/x-emf", result.value[0].content_type)
+ expected_warning = results.warning("Image of type image/x-emf is unlikely to display in web browsers")
+ assert_equal([expected_warning], result.messages)
+
@istest
def footnotes_of_document_are_read(self):
paragraph_xml = xml_element("w:p")