summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-08-19 21:33:14 +0100
committerMichael Williamson <mike@zwobble.org>2015-08-19 21:33:14 +0100
commita274a2f176303bfec529edecb8cc1773daf8ae31 (patch)
tree53deca4545f51a17335d96c143716aad7e12dae5
parent041ccdb2b378b233fab7d3c2688ee7875d9bcaa1 (diff)
Handle embedding a style map into a document that already has one
-rw-r--r--lib/docx/style-map.js26
-rw-r--r--tests/docx/style-map.tests.js22
2 files changed, 42 insertions, 6 deletions
diff --git a/lib/docx/style-map.js b/lib/docx/style-map.js
index 76f6903..04597a2 100644
--- a/lib/docx/style-map.js
+++ b/lib/docx/style-map.js
@@ -1,3 +1,5 @@
+var _ = require("underscore");
+
var promises = require("../promises");
var officeXml = require("./office-xml-reader");
var xml = require("../xml");
@@ -6,6 +8,7 @@ exports.writeStyleMap = writeStyleMap;
exports.readStyleMap = readStyleMap;
+var schema = "http://schemas.zwobble.org/mammoth/style-map";
var styleMapPath = "mammoth/style-map";
function writeStyleMap(docxFile, styleMap) {
@@ -18,14 +21,25 @@ function updateRelationships(docxFile) {
var relationshipsUri = "http://schemas.openxmlformats.org/package/2006/relationships";
return officeXml.readXmlFromZipFile(docxFile, path)
.then(function(relationshipsDocument) {
- var relationships = relationshipsDocument.root;
- relationships.children.push(xml.element("{" + relationshipsUri + "}Relationship", {
- "Id": "rMammothStyleMap",
- "Type": "http://schemas.zwobble.org/mammoth/style-map",
+ var relationshipsContainer = relationshipsDocument.root;
+ var relationships = relationshipsContainer.children;
+ var relationshipId = "rMammothStyleMap";
+ var attributes = {
+ "Id": relationshipId,
+ "Type": schema,
"Target": "/" + styleMapPath
- }));
+ };
+
+ var existingRelationship = _.find(relationships, function(relationship) {
+ return relationship.attributes.Id === relationshipId;
+ });
+ if (existingRelationship) {
+ existingRelationship.attributes = attributes;
+ } else {
+ relationships.push(xml.element("{" + relationshipsUri + "}Relationship", attributes));
+ }
var namespaces = {"": relationshipsUri};
- return docxFile.write(path, xml.writeString(relationships, namespaces));
+ return docxFile.write(path, xml.writeString(relationshipsContainer, namespaces));
});
}
diff --git a/tests/docx/style-map.tests.js b/tests/docx/style-map.tests.js
index f1ac68d..fcf22ab 100644
--- a/tests/docx/style-map.tests.js
+++ b/tests/docx/style-map.tests.js
@@ -42,6 +42,28 @@ describe("style-map", function() {
});
});
});
+
+ test('re-embedding style map replaces original', function() {
+ var zip = normalDocx();
+
+ var expectedRelationshipsXml = '<?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"/>' +
+ '<Relationship Id="rMammothStyleMap" Type="http://schemas.zwobble.org/mammoth/style-map" Target="/mammoth/style-map"/>' +
+ '</Relationships>';
+
+ return styleMap.writeStyleMap(zip, "p => h1").then(function() {
+ return styleMap.writeStyleMap(zip, "p => h2");
+ }).then(function() {
+ return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) {
+ assert.equal(contents, expectedRelationshipsXml);
+ });
+ }).then(function() {
+ return styleMap.readStyleMap(zip).then(function(contents) {
+ assert.equal(contents, "p => h2");
+ });
+ });
+ });
});
function normalDocx() {