Commit c0b09a4 for mammothjs

commit c0b09a409ba7dd859aa54ff1e603eab1c1e38c91
Author: Michael Williamson <mike@zwobble.org>
Date:   Sat Dec 5 19:27:54 2026 +0000

    Clearer error message when XML document is missing root element

diff --git a/NEWS b/NEWS
index 843d4de..b4c1a23 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+# 1.13.1
+
+* Clearer error message when XML document is missing root element.
+
 # 1.13.0

 * Replace the use of bluebird with native promises.
diff --git a/lib/xml/reader.js b/lib/xml/reader.js
index 2797323..ffbec92 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.reject(new Error("XML document is missing root element"));
+    } else if (document.documentElement.tagName === "parsererror") {
         return promises.resolve(new Error(document.documentElement.textContent));
     }

diff --git a/test/xml/reader.tests.js b/test/xml/reader.tests.js
index 6b7d3a0..cc2b897 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,13 @@ test('error if XML is badly formed', function() {
         return 1;
     });
 });
+
+test("when document is missing root element then error is thrown", function() {
+    var xmlString = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
+
+    return xmlreader.readString(xmlString).then(function() {
+        throw new Error("Expected failure");
+    }, function(error) {
+        assert.strictEqual("XML document is missing root element", error.message);
+    });
+});