aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2018-01-26 23:43:06 +0000
committerMichael Williamson <mike@zwobble.org>2018-01-26 23:43:06 +0000
commitfd9bfbf28e045362c83620dd888c5edbb6f39a89 (patch)
treeafa51bb64e3b62888cb791f455cccb0a886ae850
parent8b9ca6ca68fec89926cfb02a96506bbee54a5b2c (diff)
Find parts related to main document using main document relationships
-rw-r--r--lib/docx/docx-reader.js19
-rw-r--r--test/docx/docx-reader.tests.js55
2 files changed, 48 insertions, 26 deletions
diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js
index e6dd798..e0d4a13 100644
--- a/lib/docx/docx-reader.js
+++ b/lib/docx/docx-reader.js
@@ -100,26 +100,23 @@ function findPartPaths(docxFile) {
readElement: relationshipsReader.readRelationships,
defaultValue: relationshipsReader.defaultValue
})(docxFile).then(function(documentRelationships) {
- function findPartRelatedToMainDocument(options) {
+ function findPartRelatedToMainDocument(name) {
return findPartPath({
docxFile: docxFile,
relationships: documentRelationships,
- relationshipType: options.relationshipType,
+ relationshipType: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/" + name,
basePath: zipfile.splitPath(mainDocumentPath).dirname,
- fallbackPath: options.fallbackPath
+ fallbackPath: "word/" + name + ".xml"
});
}
return {
mainDocument: mainDocumentPath,
- comments: findPartRelatedToMainDocument({
- relationshipType: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
- fallbackPath: "word/comments.xml"
- }),
- endnotes: "word/endnotes.xml",
- footnotes: "word/footnotes.xml",
- numbering: "word/numbering.xml",
- styles: "word/styles.xml"
+ comments: findPartRelatedToMainDocument("comments"),
+ endnotes: findPartRelatedToMainDocument("endnotes"),
+ footnotes: findPartRelatedToMainDocument("footnotes"),
+ numbering: findPartRelatedToMainDocument("numbering"),
+ styles: findPartRelatedToMainDocument("styles")
};
});
});
diff --git a/test/docx/docx-reader.tests.js b/test/docx/docx-reader.tests.js
index 0d51f45..de36216 100644
--- a/test/docx/docx-reader.tests.js
+++ b/test/docx/docx-reader.tests.js
@@ -88,6 +88,7 @@ test("error is thrown when main document part does not exist", function() {
});
+
test("part paths", {
"main document part is found using package relationships": function() {
var relationships = xml.element("r:Relationships", {}, [
@@ -113,35 +114,59 @@ test("part paths", {
return docxReader._findPartPaths(docxFile).then(function(partPaths) {
assert.equal(partPaths.mainDocument, "word/document.xml");
});
+ }
+});
+
+[
+ {
+ name: "comments",
+ type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
},
-
- "comments part is found using main document relationships": function() {
+ {
+ name: "endnotes",
+ type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes",
+ },
+ {
+ name: "footnotes",
+ type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
+ },
+ {
+ name: "numbering",
+ type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
+ },
+ {
+ name: "styles",
+ type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
+ }
+].forEach(function(options) {
+ test(options.name + " part is found using main document relationships", function() {
var docxFile = createFakeDocxFile({
"_rels/.rels": createPackageRelationships("word/document.xml"),
"word/document.xml": " ",
"word/_rels/document.xml.rels": xml.writeString(xml.element("r:Relationships", {}, [
xml.element("r:Relationship", {
- "Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
- "Target": "comments2.xml"
+ "Type": options.type,
+ "Target": "target-path.xml"
})
]), relationshipNamespaces),
- "word/comments2.xml": " "
+ "word/target-path.xml": " "
});
return docxReader._findPartPaths(docxFile).then(function(partPaths) {
- assert.equal(partPaths.comments, "word/comments2.xml");
+ assert.equal(partPaths[options.name], "word/target-path.xml");
});
- },
-
- "word/comments.xml is used as fallback location for comments part": function() {
- var docxFile = createFakeDocxFile({
+ });
+
+ test("word/" + options.name + ".xml is used as fallback location for " + options.name + " part", function() {
+ var zipContents = {
"_rels/.rels": createPackageRelationships("word/document.xml"),
- "word/document.xml": " ",
- "word/comments.xml": " "
- });
+ "word/document.xml": " "
+ };
+ zipContents["word/" + options.name + ".xml"] = " ";
+ var docxFile = createFakeDocxFile(zipContents);
return docxReader._findPartPaths(docxFile).then(function(partPaths) {
- assert.equal(partPaths.comments, "word/comments.xml");
+ assert.equal(partPaths[options.name], "word/" + options.name + ".xml");
});
- }
+ });
});