summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-09-12 13:49:45 +0100
committerMichael Williamson <mike@zwobble.org>2015-09-12 13:49:45 +0100
commita34fb36159d9369aec9171a0036b687abb88541f (patch)
treef81e5220ebdd13be4ccb791cd4a9f606a697e907
parent41cdf4345f9a64f66d6df12bb7d0a68c13370e8c (diff)
Remove duplicated code for adding or updating element
-rw-r--r--lib/docx/style-map.js39
1 files changed, 17 insertions, 22 deletions
diff --git a/lib/docx/style-map.js b/lib/docx/style-map.js
index ea88cc7..70c0b83 100644
--- a/lib/docx/style-map.js
+++ b/lib/docx/style-map.js
@@ -21,26 +21,18 @@ function writeStyleMap(docxFile, styleMap) {
function updateRelationships(docxFile) {
var path = "word/_rels/document.xml.rels";
var relationshipsUri = "http://schemas.openxmlformats.org/package/2006/relationships";
+ var relationshipElementName = "{" + relationshipsUri + "}Relationship";
return docxFile.read(path, "utf8")
.then(xml.readString)
.then(function(relationshipsDocument) {
var relationshipsContainer = relationshipsDocument.root;
var relationships = relationshipsContainer.children;
- var relationshipId = "rMammothStyleMap";
- var attributes = {
- "Id": relationshipId,
+ addOrUpdateElement(relationships, relationshipElementName, "Id", {
+ "Id": "rMammothStyleMap",
"Type": schema,
"Target": styleMapAbsolutePath
- };
-
- 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(relationshipsContainer, namespaces));
});
@@ -55,24 +47,27 @@ function updateContentTypes(docxFile) {
.then(function(typesDocument) {
var typesElement = typesDocument.root;
var children = typesElement.children;
- var attributes = {
+ addOrUpdateElement(children, overrideName, "PartName", {
"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 addOrUpdateElement(elements, name, identifyingAttribute, attributes) {
+ var existingElement = _.find(elements, function(element) {
+ return element.name === name &&
+ element.attributes[identifyingAttribute] === attributes[identifyingAttribute];
+ });
+ if (existingElement) {
+ existingElement.attributes = attributes;
+ } else {
+ elements.push(xml.element(name, attributes));
+ }
+}
+
function readStyleMap(docxFile) {
if (docxFile.exists(styleMapPath)) {
return docxFile.read(styleMapPath, "utf8");