diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-01-26 21:55:45 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-01-26 21:55:45 +0000 |
| commit | c4a3d3abcf89fcba9fda9b4bad4dcc666d205c34 (patch) | |
| tree | 09894f512008fddfbe50b82b489b374446731a86 | |
| parent | b688edfa6fa6c3407e70dd9723fbeb495d11ee8e (diff) | |
Warn on unsupported image types
| -rw-r--r-- | lib/docx/document-xml-reader.js | 39 | ||||
| -rw-r--r-- | tests/docx/document-xml-reader.tests.js | 25 |
2 files changed, 49 insertions, 15 deletions
diff --git a/lib/docx/document-xml-reader.js b/lib/docx/document-xml-reader.js index 12016b0..e290792 100644 --- a/lib/docx/document-xml-reader.js +++ b/lib/docx/document-xml-reader.js @@ -215,23 +215,34 @@ function DocumentXmlReader(options) { .getElementsByTagName("pic:blipFill") .getElementsByTagName("a:blip"); - var results = blips.map(function(blip) { - var relationshipId = blip.attributes["r:embed"]; - var imagePath = joinZipPath("word", relationships[relationshipId].target); - var readImage = docxFile.read.bind(docxFile, imagePath); - var altText = element.first("wp:docPr").attributes.descr; - - var image = documents.Image({ - readImage: readImage, - altText: altText, - contentType: contentTypes.findContentType(imagePath) - }); - return new Result(image); - }); + return Result.combine(blips.map(readBlip.bind(null, element))); + } + + function readBlip(element, blip) { + var relationshipId = blip.attributes["r:embed"]; + var imagePath = joinZipPath("word", relationships[relationshipId].target); + var readImage = docxFile.read.bind(docxFile, imagePath); + var altText = element.first("wp:docPr").attributes.descr; + var contentType = contentTypes.findContentType(imagePath); - return Result.combine(results); + var image = documents.Image({ + readImage: readImage, + altText: altText, + contentType: contentType + }); + var warnings = supportedImageTypes[contentType] ? + [] : warning("Image of type " + contentType + " is unlikely to display in web browsers"); + return new Result(image, warnings); } } + +var supportedImageTypes = { + "image/png": true, + "image/gif": true, + "image/jpeg": true, + "image/svg+xml": true, + "image/tiff": true +}; var ignoreElements = { "w:bookmarkStart": true, diff --git a/tests/docx/document-xml-reader.tests.js b/tests/docx/document-xml-reader.tests.js index 53f2aa0..0b5a75f 100644 --- a/tests/docx/document-xml-reader.tests.js +++ b/tests/docx/document-xml-reader.tests.js @@ -34,7 +34,8 @@ function convertXmlToDocumentValue(element, options) { var fakeContentTypes = { findContentType: function(filePath) { var extensionTypes = { - ".png": "image/png" + ".png": "image/png", + ".emf": "image/x-emf" }; return extensionTypes[path.extname(filePath)]; } @@ -289,6 +290,28 @@ describe("readXmlElement: ", function() { }); }); + test("warning if unsupported image type", function() { + var drawing = createInlineImage({ + relationshipId: "rId5", + description: "It's a hat" + }); + + var imageBuffer = new Buffer("Not an image at all!"); + var reader = new DocumentXmlReader({ + relationships: { + "rId5": {target: "media/hat.emf"} + }, + contentTypes: fakeContentTypes, + docxFile: createFakeDocxFile({ + "word/media/hat.emf": imageBuffer + }) + }); + var result = reader.readXmlElement(drawing); + assert.deepEqual(result.messages, [warning("Image of type image/x-emf is unlikely to display in web browsers")]); + var element = single(result.value); + assert.equal(element.contentType, "image/x-emf"); + }); + test("no elements created if image cannot be found in w:drawing", function() { var drawing = new XmlElement("w:drawing", {}, []); |
