From fd9bfbf28e045362c83620dd888c5edbb6f39a89 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 26 Jan 2018 23:43:06 +0000 Subject: Find parts related to main document using main document relationships --- lib/docx/docx-reader.js | 19 ++++++--------- test/docx/docx-reader.tests.js | 55 ++++++++++++++++++++++++++++++------------ 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"); }); - } + }); }); -- cgit v1.2.3