Merge pull request #1741 from lightpanda-io/DOMParser_invalid_xml

Throw exception, as expected, on empty input to DOMParser.parseFromSt…
This commit is contained in:
Karl Seguin
2026-03-10 06:32:48 +08:00
committed by GitHub
2 changed files with 16 additions and 7 deletions

View File

@@ -4,9 +4,17 @@
<script id=basic>
{
{
const parser = new DOMParser();
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>

View File

@@ -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 <parsererror> 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();