Lightpanda: the headless browser designed for AI and automation
webapi: XHR uses XML parser for XML content-type responses
Karl Seguindab5ff77792a
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex 7e13f3db3..764e075e0 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -2938,6 +2938,42 @@ fn parseHtmlAsChildrenInner(self: *Frame, node: *Node, html: []const u8, opts: F}}+// Build a detached XMLDocument from `xml` (DOMParser.parseFromString and+// XMLHttpRequest.responseXML). Returns null when the input isn't well-formed+// XML. The parse borrows the fragment parse-mode so frame-side hooks triggered+// from `Build.created` / `nodeIsReady` (external stylesheet fetches, script+// execution, mutation-observer fan-out, default-script injection) treat the+// parsed nodes as detached and skip side effects on the live document.+pub fn parseXmlDocument(self: *Frame, xml: []const u8) !?*Document.XMLDocument {+ const arena = try self.getArena(.medium, "Frame.parseXmlDocument");+ defer self.releaseArena(arena);++ const previous_parse_mode = self._parse_mode;+ self._parse_mode = .fragment;+ defer self._parse_mode = previous_parse_mode;++ const doc = try self._factory.document(Document.XMLDocument{ ._proto = undefined });+ const doc_node = doc.asNode();+ var parser = Parser.init(arena, doc_node, self, .{});+ parser.parseXML(xml);+ if (parser.terminated) {+ return error.ExecutionTerminated;+ }++ if (parser.err != null or doc_node.firstChild() == null) {+ return null;+ }++ // If first node is a `ProcessingInstruction` (e.g. the <?xml?>+ // declaration), skip it.+ const first_child = doc_node.firstChild().?;+ if (first_child.getNodeType() == 7) {+ _ = try doc_node.removeChild(first_child, self);+ }++ return doc;+}+// Runs the "ready" work for an inserted node and, when it's an element with// children, for its descendants in tree order: appending a subtree// containing scripts must execute them all, after the whole insertion.diff --git a/src/browser/Mime.zig b/src/browser/Mime.zigindex 8145f4979..72268c7fb 100644--- a/src/browser/Mime.zig+++ b/src/browser/Mime.zig@@ -49,6 +49,7 @@ pub const ContentTypeEnum = enum {application_json,unknown,other,+ other_xml,};pub const ContentType = union(ContentTypeEnum) {@@ -68,6 +69,7 @@ pub const ContentType = union(ContentTypeEnum) {// A valid but unrecognized type/subtype. Keeping it would require some// memory management of the input. Nothing needs it right now, so why bother.other: void,+ other_xml: void, // i.e. application/xml or */*+xml};pub fn contentTypeString(mime: *const Mime) []const u8 {@@ -346,6 +348,13 @@ pub fn isHTML(self: *const Mime) bool {return self.content_type == .text_html;}+pub fn isXML(self: *const Mime) bool {+ return switch (self.content_type) {+ .text_xml, .other_xml => true,+ else => false,+ };+}+pub fn isText(mime: *const Mime) bool {return switch (mime.content_type) {.text_xml, .text_html, .text_javascript, .text_plain, .text_css, .text_markdown => true,@@ -378,9 +387,11 @@ fn parseContentType(value: []const u8) !struct { ContentType, usize } {@"image/webp",@"application/json",+ @"application/xml",}, type_name)) |known_type| {const ct: ContentType = switch (known_type) {.@"text/xml" => .{ .text_xml = {} },+ .@"application/xml" => .{ .other_xml = {} },.@"text/html" => .{ .text_html = {} },.@"text/javascript", .@"application/javascript", .@"application/x-javascript" => .{ .text_javascript = {} },.@"text/plain" => .{ .text_plain = {} },@@ -408,6 +419,10 @@ fn parseContentType(value: []const u8) !struct { ContentType, usize } {return error.Invalid;}+ if (std.mem.endsWith(u8, type_name, "+xml")) {+ return .{ .{ .other_xml = {} }, attribute_start };+ }+return .{ .{ .other = {} }, attribute_start };}@@ -897,6 +912,28 @@ test "Mime: isHTML" {try assert(false, "over/9000");}+test "Mime: isXML" {+ defer testing.reset();++ const assert = struct {+ fn assert(expected: bool, input: []const u8) !void {+ const mutable_input = try testing.arena_allocator.dupe(u8, input);+ var mime = try Mime.parse(mutable_input);+ try testing.expectEqual(expected, mime.isXML());+ }+ }.assert;+ try assert(true, "text/xml");+ try assert(true, "TEXT/XML; charset=utf-8");+ try assert(true, "application/xml");+ try assert(true, "image/svg+xml");+ try assert(true, "application/xhtml+xml");+ try assert(true, "application/rss+xml;charset=utf-8");+ try assert(false, "text/html");+ try assert(false, "application/json");+ try assert(false, "application/xmlfoo");+ try assert(false, "text/xm");+}+test "Mime: sniff" {try testing.expectEqual(null, Mime.sniff(""));try testing.expectEqual(null, Mime.sniff("<htm"));diff --git a/src/browser/tests/net/xhr.html b/src/browser/tests/net/xhr.htmlindex 3f45b3715..0025e8966 100644--- a/src/browser/tests/net/xhr.html+++ b/src/browser/tests/net/xhr.html@@ -372,25 +372,80 @@<script id=xhr_override_mime_responsexml type=module>{- // End-to-end: server returns text/html, but overrideMimeType("text/xml")- // makes responseXML lazily parse the body as a Document, while- // responseText is unaffected (responseType is still the default "").+ // End-to-end: server returns text/plain, but overrideMimeType("text/xml")+ // makes responseXML lazily parse the body as XML, while responseText is+ // unaffected (responseType is still the default "").const state = await testing.async();const req = new XMLHttpRequest();req.overrideMimeType('text/xml');- req.open('GET', 'http://127.0.0.1:9582/xhr');+ req.open('GET', 'http://127.0.0.1:9582/xhr/xml_as_text');req.onload = () => state.resolve();req.send();await state.done(() => {testing.expectEqual(200, req.status);- testing.expectEqual(100, req.responseText.length);- testing.expectEqual(true, req.responseXML instanceof Document);+ testing.expectEqual(true, req.responseText.startsWith('<?xml'));+ testing.expectEqual(true, req.responseXML instanceof XMLDocument);+ testing.expectEqual('catalog', req.responseXML.documentElement.tagName);// Cached across calls.testing.expectEqual(req.responseXML, req.responseXML);});}</script>+<script id=xhr_override_mime_malformed_xml type=module>+ {+ // The overridden type selects the XML parser; a body that isn't+ // well-formed XML yields responseXML === null.+ const state = await testing.async();+ const req = new XMLHttpRequest();+ req.overrideMimeType('text/xml');+ req.open('GET', 'http://127.0.0.1:9582/xhr');+ req.onload = () => state.resolve();+ req.send();+ await state.done(() => {+ testing.expectEqual(100, req.responseText.length);+ testing.expectEqual(null, req.responseXML);+ });+ }+</script>++<script id=xhr_responsexml_xml_mime type=module>+ {+ // An application/xml response parses with the XML parser: XMLDocument,+ // tag case preserved, self-closing tags don't swallow their siblings.+ const state = await testing.async();+ const req = new XMLHttpRequest();+ req.open('GET', 'http://127.0.0.1:9582/xhr/xml');+ req.onload = () => state.resolve();+ req.send();+ await state.done(() => {+ const doc = req.responseXML;+ testing.expectEqual(true, doc instanceof XMLDocument);+ testing.expectEqual('catalog', doc.documentElement.tagName);+ testing.expectEqual(3, doc.documentElement.children.length);+ testing.expectEqual(2, doc.getElementsByTagName('item').length);+ testing.expectEqual(1, doc.getElementsByTagName('pubDate').length);+ });+ }+</script>++<script id=xhr_document_xml type=module>+ {+ // responseType=document with an XML MIME type also uses the XML parser.+ const state = await testing.async();+ const req = new XMLHttpRequest();+ req.open('GET', 'http://127.0.0.1:9582/xhr/xml');+ req.responseType = 'document';+ req.onload = () => state.resolve();+ req.send();+ await state.done(() => {+ testing.expectEqual(true, req.response instanceof XMLDocument);+ testing.expectEqual('catalog', req.response.documentElement.tagName);+ testing.expectEqual(req.response, req.responseXML);+ });+ }+</script>+<script id=xhr_override_mime_no_override type=module>{// Without overrideMimeType, a text/html response still yieldsdiff --git a/src/browser/webapi/DOMParser.zig b/src/browser/webapi/DOMParser.zigindex 57c0f4080..f30467608 100644--- a/src/browser/webapi/DOMParser.zig+++ b/src/browser/webapi/DOMParser.zig@@ -24,7 +24,6 @@ const Frame = @import("../Frame.zig");const Parser = @import("../parser/Parser.zig");const HTMLDocument = @import("HTMLDocument.zig");-const XMLDocument = @import("XMLDocument.zig");const Document = @import("Document.zig");const DOMParser = @This();@@ -50,22 +49,22 @@ pub fn parseFromString(@"image/svg+xml",}, mime_type) orelse return error.NotSupported;- const arena = try frame.getArena(.medium, "DOMParser.parseFromString");- defer frame.releaseArena(arena);-- // DOMParser builds a detached Document. Borrow the same fragment- // parse-mode that `parseHtmlAsChildren` uses so frame-side hooks- // triggered from `Build.created` / `nodeIsReady` (external stylesheet- // fetches, script execution, mutation-observer fan-out, default-script- // injection) treat the parsed nodes as detached and skip- // side effects on the live document. The frame's `_parse_mode` is- // restored on exit.- const previous_parse_mode = frame._parse_mode;- frame._parse_mode = .fragment;- defer frame._parse_mode = previous_parse_mode;-return switch (target_mime) {.@"text/html" => {+ const arena = try frame.getArena(.medium, "DOMParser.parseFromString");+ defer frame.releaseArena(arena);++ // DOMParser builds a detached Document. Borrow the same fragment+ // parse-mode that `parseHtmlAsChildren` uses so frame-side hooks+ // triggered from `Build.created` / `nodeIsReady` (external+ // stylesheet fetches, script execution, mutation-observer fan-out,+ // default-script injection) treat the parsed nodes as detached and+ // skip side effects on the live document. The frame's+ // `_parse_mode` is restored on exit.+ const previous_parse_mode = frame._parse_mode;+ frame._parse_mode = .fragment;+ defer frame._parse_mode = previous_parse_mode;+// Create a new HTMLDocumentconst doc = try frame._factory.document(HTMLDocument{._proto = undefined,@@ -90,35 +89,10 @@ pub fn parseFromString(return doc.asDocument();},else => {- // Create a new XMLDocument.- const doc = try frame._factory.document(XMLDocument{- ._proto = undefined,- });-- // Parse XML into XMLDocument.- const doc_node = doc.asNode();- var parser = Parser.init(arena, doc_node, frame, .{});- parser.parseXML(html);- if (parser.terminated) {- return error.ExecutionTerminated;- }-- if (parser.err != null or doc_node.firstChild() == null) {+ const doc = (try frame.parseXmlDocument(html)) orelse blk: {// Return a document with a <parsererror> element per spec.- const err_doc = try frame._factory.document(XMLDocument{ ._proto = undefined });- var err_parser = Parser.init(arena, err_doc.asNode(), frame, .{});- err_parser.parseXML("<parsererror xmlns=\"http://www.mozilla.org/newlayout/xml/parsererror.xml\">error</parsererror>");- return err_doc.asDocument();- }-- const first_child = doc_node.firstChild().?;-- // If first node is a `ProcessingInstruction`, skip it.- if (first_child.getNodeType() == 7) {- // We're sure that firstChild exist, this cannot fail.- _ = try doc_node.removeChild(first_child, frame);- }-+ break :blk (try frame.parseXmlDocument("<parsererror xmlns=\"http://www.mozilla.org/newlayout/xml/parsererror.xml\">error</parsererror>")).?;+ };return doc.asDocument();},};diff --git a/src/browser/webapi/net/XMLHttpRequest.zig b/src/browser/webapi/net/XMLHttpRequest.zigindex db3627b50..d59c6cc26 100644--- a/src/browser/webapi/net/XMLHttpRequest.zig+++ b/src/browser/webapi/net/XMLHttpRequest.zig@@ -434,14 +434,16 @@ pub fn getResponse(self: *XMLHttpRequest, exec: *const Execution) !?Response {.document => blk: {// responseType=document is only meaningful in a Frame; workers// have no DOM. Drastically different impls -> switch on global.- //- // TODO: per WHATWG XHR "set a document response", the final MIME- // type (_override_mime ?? _response_mime) should select an XML- // parser when it is an XML MIME type, and the HTML parser- // otherwise. We only have an HTML parser today, so the body is- // always parsed as HTML regardless of the override.switch (exec.js.global) {.frame => |frame| {+ const final: Mime = self._override_mime orelse self._response_mime orelse .{ .content_type = .text_xml };+ if (final.isXML()) {+ const document = (try frame.parseXmlDocument(data)) orelse return null;+ break :blk .{ .document = document.asDocument() };+ }+ if (!final.isHTML()) {+ return null;+ }const document = try exec._factory.node(Node.Document{ ._proto = undefined, ._type = .generic });try frame.parseHtmlAsChildren(document.asNode(), data);break :blk .{ .document = document };@@ -478,13 +480,15 @@ pub fn getResponseXML(self: *XMLHttpRequest, exec: *const Execution) !?*Node.Docreturn document;}- const final = self._override_mime orelse self._response_mime orelse return null;- if (final.content_type != .text_xml) return null;+ // With responseType "", only an XML final MIME type is parsed (an HTML+ // one yields null); absent a Content-Type it defaults to text/xml.+ const final: Mime = self._override_mime orelse self._response_mime orelse .{ .content_type = .text_xml };+ if (!final.isXML()) return null;switch (exec.js.global) {.frame => |frame| {- const document = try exec._factory.node(Node.Document{ ._proto = undefined, ._type = .generic });- try frame.parseHtmlAsChildren(document.asNode(), self._response_data.items);+ const xml_document = (try frame.parseXmlDocument(self._response_data.items)) orelse return null;+ const document = xml_document.asDocument();self._response_xml = document;return document;},diff --git a/src/testing.zig b/src/testing.zigindex 7cf26c4a7..3c335d0e8 100644--- a/src/testing.zig+++ b/src/testing.zig@@ -637,6 +637,24 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {});}+ const xhr_xml_body = "<?xml version=\"1.0\"?><catalog><item id=\"a\"/><item id=\"b\"/><pubDate>2026</pubDate></catalog>";++ if (std.mem.eql(u8, path, "/xhr/xml")) {+ return req.respond(xhr_xml_body, .{+ .extra_headers = &.{+ .{ .name = "Content-Type", .value = "application/xml" },+ },+ });+ }++ if (std.mem.eql(u8, path, "/xhr/xml_as_text")) {+ return req.respond(xhr_xml_body, .{+ .extra_headers = &.{+ .{ .name = "Content-Type", .value = "text/plain" },+ },+ });+ }+if (std.mem.eql(u8, path, "/xhr/json")) {return req.respond("{\"over\":\"9000!!!\",\"updated_at\":1765867200000}", .{.extra_headers = &.{