diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-12-05 11:30:30 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-12-05 11:30:30 +0000 |
| commit | 39c18eb51cf1a0156b5e09ef2d68b5dee6f0680e (patch) | |
| tree | 4d120413dd16e2ac71a43df58db35ed534159254 | |
| parent | ae06a2bacfb74c88340e900e8a29336418936e9e (diff) | |
Handle main document part without a root element
| -rw-r--r-- | lib/docx/docx-reader.js | 4 | ||||
| -rw-r--r-- | test/docx/docx-reader.tests.js | 38 |
2 files changed, 38 insertions, 4 deletions
diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index f343dae..9bca76b 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -74,6 +74,10 @@ function read(docxFile, input, options) { }; }).then(function(result) { return readXmlFileWithBody(result.partPaths.mainDocument, result, function(bodyReader, xml) { + if (xml === null) { + throw new Error("Main document part is missing root element. Are you sure this is a valid .docx file?"); + } + return result.notes.flatMap(function(notes) { return result.comments.flatMap(function(comments) { var reader = new DocumentXmlReader({ diff --git a/test/docx/docx-reader.tests.js b/test/docx/docx-reader.tests.js index db9faff..804215f 100644 --- a/test/docx/docx-reader.tests.js +++ b/test/docx/docx-reader.tests.js @@ -51,7 +51,7 @@ test("main document is found using _rels/.rels", function() { "Target": "/word/document2.xml" }) ]); - + var docxFile = createFakeDocxFile({ "word/document2.xml": testData("simple/word/document.xml"), "_rels/.rels": xml.writeString(relationships, relationshipNamespaces) @@ -76,7 +76,7 @@ test("error is thrown when main document part does not exist", function() { "Target": "/word/document2.xml" }) ]); - + var docxFile = createFakeDocxFile({ "_rels/.rels": xml.writeString(relationships, relationshipNamespaces) }); @@ -88,6 +88,36 @@ test("error is thrown when main document part does not exist", function() { }); +test("when main document part is empty then error is thrown", function() { + var docxFile = createFakeDocxFile({ + "word/document.xml": '<?xml version="1.0" encoding="utf-8" ?>' + }); + + return docxReader.read(docxFile).then(function() { + assert.ok(false, "Expected error"); + }, function(error) { + assert.equal(error.message, "Main document part is missing root element. Are you sure this is a valid .docx file?"); + }); +}); + + +test("when part other than main document is empty then part is ignored", function() { + var docxFile = createFakeDocxFile({ + "word/document.xml": ( + '<?xml version="1.0" encoding="utf-8" ?>' + + '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">' + + '<w:body/>' + + '</w:document>' + ), + "word/footnotes.xml": '<?xml version="1.0" encoding="utf-8" ?>' + }); + + return docxReader.read(docxFile).then(function(result) { + assert.deepEqual(documents.document([]), result.value); + }); +}); + + test("part paths", { "main document part is found using package relationships": function() { var relationships = xml.element("r:Relationships", {}, [ @@ -96,7 +126,7 @@ test("part paths", { "Target": "/word/document2.xml" }) ]); - + var docxFile = createFakeDocxFile({ "word/document2.xml": " ", "_rels/.rels": xml.writeString(relationships, relationshipNamespaces) @@ -105,7 +135,7 @@ test("part paths", { assert.equal(partPaths.mainDocument, "word/document2.xml"); }); }, - + "word/document.xml is used as fallback location for main document part": function() { var docxFile = createFakeDocxFile({ "word/document.xml": " " |
