Commit ae06a2b for mammothjs
commit ae06a2bacfb74c88340e900e8a29336418936e9e
Author: Michael Williamson <mike@zwobble.org>
Date: Sat Dec 5 10:19:25 2026 +0000
Handle XML documents that are missing a root element
diff --git a/NEWS b/NEWS
index 843d4de..cb024ae 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+# 1.13.1
+
+* Handle XML documents that are missing a root element.
+
# 1.13.0
* Replace the use of bluebird with native promises.
diff --git a/lib/docx/office-xml-reader.js b/lib/docx/office-xml-reader.js
index 792cd7c..6d2c1e9 100644
--- a/lib/docx/office-xml-reader.js
+++ b/lib/docx/office-xml-reader.js
@@ -38,7 +38,11 @@ var xmlNamespaceMap = {
function read(xmlString) {
return xml.readString(xmlString, xmlNamespaceMap)
.then(function(document) {
- return collapseAlternateContent(document)[0];
+ if (document === null) {
+ return null;
+ } else {
+ return collapseAlternateContent(document)[0];
+ }
});
}
diff --git a/lib/xml/reader.js b/lib/xml/reader.js
index 2797323..dcddb0b 100644
--- a/lib/xml/reader.js
+++ b/lib/xml/reader.js
@@ -18,7 +18,9 @@ function readString(xmlString, namespaceMap) {
return promises.reject(error);
}
- if (document.documentElement.tagName === "parsererror") {
+ if (document.documentElement == null) {
+ return promises.resolve(null);
+ } else if (document.documentElement.tagName === "parsererror") {
return promises.resolve(new Error(document.documentElement.textContent));
}
diff --git a/test/docx/office-xml-reader.tests.js b/test/docx/office-xml-reader.tests.js
index a017315..6adb970 100644
--- a/test/docx/office-xml-reader.tests.js
+++ b/test/docx/office-xml-reader.tests.js
@@ -4,6 +4,15 @@ var xml = require("../../lib/xml");
var officeXmlReader = require("../../lib/docx/office-xml-reader");
var test = require("../test")(module);
+test("read", {
+ "when document is missing root element then null is returned": function() {
+ var xmlString = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
+
+ return officeXmlReader.read(xmlString).then(function(element) {
+ assert.strictEqual(element, null);
+ });
+ }
+});
test("mc:AlternateContent", {
"when mc:Fallback is present then mc:Fallback is read": function() {
diff --git a/test/xml/reader.tests.js b/test/xml/reader.tests.js
index 6b7d3a0..3515835 100644
--- a/test/xml/reader.tests.js
+++ b/test/xml/reader.tests.js
@@ -46,7 +46,7 @@ test('mapped namespaces URIs are translated using map', function() {
var namespaceMap = {
"word": "x"
};
-
+
return xmlreader.readString('<w:body xmlns:w="word"/>', namespaceMap).then(function(result) {
assert.deepEqual(result.name, "x:body");
});
@@ -83,3 +83,11 @@ test('error if XML is badly formed', function() {
return 1;
});
});
+
+test("when document is missing root element then null is returned", function() {
+ var xmlString = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
+
+ return xmlreader.readString(xmlString).then(function(element) {
+ assert.strictEqual(element, null);
+ });
+});