Throw exception, as expected, on empty input to DOMParser.parseFromString

https://github.com/lightpanda-io/browser/issues/1738
This commit is contained in:
Karl Seguin
2026-03-09 13:44:17 +08:00
parent 0227afffc8
commit f3e1204fa1
2 changed files with 16 additions and 7 deletions

View File

@@ -4,9 +4,17 @@
<script id=basic> <script id=basic>
{ {
const parser = new DOMParser(); {
testing.expectEqual('object', typeof parser); const parser = new DOMParser();
testing.expectEqual('function', typeof parser.parseFromString); testing.expectEqual('object', typeof parser);
testing.expectEqual('function', typeof parser.parseFromString);
}
{
// Empty XML is a parse error (no root element)
const parser = new DOMParser();
testing.expectError('Error', () => parser.parseFromString('', 'text/xml'));
}
} }
</script> </script>

View File

@@ -90,15 +90,16 @@ pub fn parseFromString(
return pe.err; return pe.err;
} }
// If first node is a `ProcessingInstruction`, skip it.
const first_child = doc_node.firstChild() orelse { const first_child = doc_node.firstChild() orelse {
// Parsing should fail if there aren't any nodes. // Empty XML or no root element - this is a parse error.
unreachable; // TODO: Return a document with a <parsererror> element per spec.
return error.JsException;
}; };
// If first node is a `ProcessingInstruction`, skip it.
if (first_child.getNodeType() == 7) { if (first_child.getNodeType() == 7) {
// We're sure that firstChild exist, this cannot fail. // 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(); return doc.asDocument();