summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-09-06 11:09:27 +0100
committerMichael Williamson <mike@zwobble.org>2015-09-06 11:09:27 +0100
commitd4a14afabc917a06cd3ac67ccb03c69bf0299821 (patch)
tree3c3af86f20e4ef3b933a0a5e3218dd0bcb2d8a0d
parenta274a2f176303bfec529edecb8cc1773daf8ae31 (diff)
Add fallback content types
-rw-r--r--lib/docx/content-types-reader.js32
-rw-r--r--tests/docx/content-types-reader.tests.js13
2 files changed, 39 insertions, 6 deletions
diff --git a/lib/docx/content-types-reader.js b/lib/docx/content-types-reader.js
index a5f7ec0..fc95771 100644
--- a/lib/docx/content-types-reader.js
+++ b/lib/docx/content-types-reader.js
@@ -1,10 +1,17 @@
exports.readContentTypesFromXml = readContentTypesFromXml;
-exports.defaultContentTypes = {
- findContentType: function(path) {
- return null;
- }
+
+var fallbackContentTypes = {
+ "png": "png",
+ "gif": "gif",
+ "jpeg": "jpeg",
+ "jpg": "jpeg",
+ "tif": "tiff",
+ "tiff": "tiff",
+ "bmp": "bmp"
};
+exports.defaultContentTypes = contentTypes({}, {});
+
function readContentTypesFromXml(xml) {
var extensionDefaults = {};
@@ -22,7 +29,10 @@ function readContentTypesFromXml(xml) {
overrides[name] = child.attributes.ContentType;
}
});
-
+ return contentTypes(overrides, extensionDefaults);
+}
+
+function contentTypes(overrides, extensionDefaults) {
return {
findContentType: function(path) {
var overrideContentType = overrides[path];
@@ -31,8 +41,18 @@ function readContentTypesFromXml(xml) {
} else {
var pathParts = path.split(".");
var extension = pathParts[pathParts.length - 1];
- return extensionDefaults[extension];
+ if (extensionDefaults.hasOwnProperty(extension)) {
+ return extensionDefaults[extension];
+ } else {
+ var fallback = fallbackContentTypes[extension];
+ if (fallback) {
+ return "image/" + fallback;
+ } else {
+ return null;
+ }
+ }
}
}
};
+
}
diff --git a/tests/docx/content-types-reader.tests.js b/tests/docx/content-types-reader.tests.js
index 3b8c1d3..3390e43 100644
--- a/tests/docx/content-types-reader.tests.js
+++ b/tests/docx/content-types-reader.tests.js
@@ -24,4 +24,17 @@ describe('readContentTypesFromXml', function() {
});
assert.equal(contentTypes.findContentType("word/media/hat.png"), "image/hat");
});
+
+ test('fallback content types have common image types', function() {
+ var contentTypes = readContentTypesFromXml({
+ root: new XmlElement("content-types:Types", {}, [])
+ });
+ assert.equal(contentTypes.findContentType("word/media/hat.png"), "image/png");
+ assert.equal(contentTypes.findContentType("word/media/hat.gif"), "image/gif");
+ assert.equal(contentTypes.findContentType("word/media/hat.jpg"), "image/jpeg");
+ assert.equal(contentTypes.findContentType("word/media/hat.jpeg"), "image/jpeg");
+ assert.equal(contentTypes.findContentType("word/media/hat.bmp"), "image/bmp");
+ assert.equal(contentTypes.findContentType("word/media/hat.tif"), "image/tiff");
+ assert.equal(contentTypes.findContentType("word/media/hat.tiff"), "image/tiff");
+ });
});