From f3e1204fa1eef0cdc46d1d220a96bd7a67ad1f0b Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Mon, 9 Mar 2026 13:44:17 +0800 Subject: [PATCH] Throw exception, as expected, on empty input to DOMParser.parseFromString https://github.com/lightpanda-io/browser/issues/1738 --- src/browser/tests/domparser.html | 14 +++++++++++--- src/browser/webapi/DOMParser.zig | 9 +++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/browser/tests/domparser.html b/src/browser/tests/domparser.html index 70fd6ff9..24d34e89 100644 --- a/src/browser/tests/domparser.html +++ b/src/browser/tests/domparser.html @@ -4,9 +4,17 @@ diff --git a/src/browser/webapi/DOMParser.zig b/src/browser/webapi/DOMParser.zig index e2a0a438..9e27e9c7 100644 --- a/src/browser/webapi/DOMParser.zig +++ b/src/browser/webapi/DOMParser.zig @@ -90,15 +90,16 @@ pub fn parseFromString( return pe.err; } - // If first node is a `ProcessingInstruction`, skip it. const first_child = doc_node.firstChild() orelse { - // Parsing should fail if there aren't any nodes. - unreachable; + // Empty XML or no root element - this is a parse error. + // TODO: Return a document with a element per spec. + return error.JsException; }; + // If first node is a `ProcessingInstruction`, skip it. if (first_child.getNodeType() == 7) { // We're sure that firstChild exist, this cannot fail. - _ = doc_node.removeChild(first_child, page) catch unreachable; + _ = try doc_node.removeChild(first_child, page); } return doc.asDocument();