summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-09-12 13:36:21 +0100
committerMichael Williamson <mike@zwobble.org>2015-09-12 13:43:30 +0100
commit41cdf4345f9a64f66d6df12bb7d0a68c13370e8c (patch)
treefb4f5d13ae49406476c8393ab7bb89e7a21d8b39
parenta84aa5097c5497799138dc0bdacd48dee4401ae2 (diff)
Add content type for embedded style map
-rw-r--r--lib/docx/style-map.js39
-rw-r--r--tests/docx/style-map.tests.js34
2 files changed, 68 insertions, 5 deletions
diff --git a/lib/docx/style-map.js b/lib/docx/style-map.js
index 04597a2..ea88cc7 100644
--- a/lib/docx/style-map.js
+++ b/lib/docx/style-map.js
@@ -1,7 +1,6 @@
var _ = require("underscore");
var promises = require("../promises");
-var officeXml = require("./office-xml-reader");
var xml = require("../xml");
exports.writeStyleMap = writeStyleMap;
@@ -10,16 +9,20 @@ exports.readStyleMap = readStyleMap;
var schema = "http://schemas.zwobble.org/mammoth/style-map";
var styleMapPath = "mammoth/style-map";
+var styleMapAbsolutePath = "/" + styleMapPath;
function writeStyleMap(docxFile, styleMap) {
docxFile.write(styleMapPath, styleMap);
- return updateRelationships(docxFile);
+ return updateRelationships(docxFile).then(function() {
+ return updateContentTypes(docxFile);
+ });
}
function updateRelationships(docxFile) {
var path = "word/_rels/document.xml.rels";
var relationshipsUri = "http://schemas.openxmlformats.org/package/2006/relationships";
- return officeXml.readXmlFromZipFile(docxFile, path)
+ return docxFile.read(path, "utf8")
+ .then(xml.readString)
.then(function(relationshipsDocument) {
var relationshipsContainer = relationshipsDocument.root;
var relationships = relationshipsContainer.children;
@@ -27,7 +30,7 @@ function updateRelationships(docxFile) {
var attributes = {
"Id": relationshipId,
"Type": schema,
- "Target": "/" + styleMapPath
+ "Target": styleMapAbsolutePath
};
var existingRelationship = _.find(relationships, function(relationship) {
@@ -41,7 +44,33 @@ function updateRelationships(docxFile) {
var namespaces = {"": relationshipsUri};
return docxFile.write(path, xml.writeString(relationshipsContainer, namespaces));
});
-
+}
+
+function updateContentTypes(docxFile) {
+ var path = "[Content_Types].xml";
+ var contentTypesUri = "http://schemas.openxmlformats.org/package/2006/content-types";
+ var overrideName = "{" + contentTypesUri + "}Override";
+ return docxFile.read(path, "utf8")
+ .then(xml.readString)
+ .then(function(typesDocument) {
+ var typesElement = typesDocument.root;
+ var children = typesElement.children;
+ var attributes = {
+ "PartName": styleMapAbsolutePath,
+ "ContentType": "text/prs.mammoth.style-map"
+ };
+
+ var existingOverride = _.find(children, function(child) {
+ return child.name === overrideName && child.attributes.PartName === styleMapAbsolutePath;
+ });
+ if (existingOverride) {
+ existingOverride.attributes = attributes;
+ } else {
+ children.push(xml.element(overrideName, attributes));
+ }
+ var namespaces = {"": contentTypesUri};
+ return docxFile.write(path, xml.writeString(typesElement, namespaces));
+ });
}
function readStyleMap(docxFile) {
diff --git a/tests/docx/style-map.tests.js b/tests/docx/style-map.tests.js
index fcf22ab..34dab8d 100644
--- a/tests/docx/style-map.tests.js
+++ b/tests/docx/style-map.tests.js
@@ -64,15 +64,49 @@ describe("style-map", function() {
});
});
});
+
+ test('embedded style map has override content type in [Content_Types].xml', function() {
+ var zip = normalDocx();
+
+ return styleMap.writeStyleMap(zip, "p => h1").then(function() {
+ return zip.read("[Content_Types].xml", "utf8").then(function(contents) {
+ assert.equal(contents, expectedContentTypesXml);
+ });
+ });
+ });
+
+ test('replacing style map keeps content type', function() {
+ var zip = normalDocx();
+
+ return styleMap.writeStyleMap(zip, "p => h1").then(function() {
+ return styleMap.writeStyleMap(zip, "p => h2");
+ }).then(function() {
+ return zip.read("[Content_Types].xml", "utf8").then(function(contents) {
+ assert.equal(contents, expectedContentTypesXml);
+ });
+ });
+ });
+
});
+var expectedContentTypesXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
+ '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' +
+ '<Default Extension="png" ContentType="image/png"/>' +
+ '<Override PartName="/mammoth/style-map" ContentType="text/prs.mammoth.style-map"/>' +
+ '</Types>';
+
function normalDocx() {
var zip = new JSZip();
var originalRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
'<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' +
'</Relationships>';
+ var originalContentTypesXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
+ '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' +
+ '<Default Extension="png" ContentType="image/png"/>' +
+ '</Types>';
zip.file("word/_rels/document.xml.rels", originalRelationshipsXml);
+ zip.file("[Content_Types].xml", originalContentTypesXml);
var buffer = zip.generate({type: "arraybuffer"});
return zipfile.openArrayBuffer(buffer);
}