allow other XML MIMEs in parseFromString

This commit is contained in:
Halil Durak
2026-01-07 14:34:35 +03:00
parent 56d89895a8
commit 612b3a26b7

View File

@@ -19,9 +19,13 @@
const std = @import("std"); const std = @import("std");
const js = @import("../js/js.zig"); const js = @import("../js/js.zig");
const Page = @import("../Page.zig"); const Page = @import("../Page.zig");
const Parser = @import("../parser/Parser.zig");
const HTMLDocument = @import("HTMLDocument.zig"); const HTMLDocument = @import("HTMLDocument.zig");
const XMLDocument = @import("XMLDocument.zig"); const XMLDocument = @import("XMLDocument.zig");
const ProcessingInstruction = @import("../webapi/cdata/ProcessingInstruction.zig");
const DOMParser = @This(); const DOMParser = @This();
@@ -40,7 +44,16 @@ pub fn parseFromString(
mime_type: []const u8, mime_type: []const u8,
page: *Page, page: *Page,
) !HTMLDocumentOrXMLDocument { ) !HTMLDocumentOrXMLDocument {
if (std.mem.eql(u8, mime_type, "text/html")) { const maybe_target_mime = std.meta.stringToEnum(enum {
@"text/html",
@"text/xml",
@"application/xml",
@"application/xhtml+xml",
@"image/svg+xml",
}, mime_type);
if (maybe_target_mime) |target_mime| switch (target_mime) {
.@"text/html" => {
// Create a new HTMLDocument // Create a new HTMLDocument
const doc = try page._factory.document(HTMLDocument{ const doc = try page._factory.document(HTMLDocument{
._proto = undefined, ._proto = undefined,
@@ -52,7 +65,6 @@ pub fn parseFromString(
} }
// Parse HTML into the document // Parse HTML into the document
const Parser = @import("../parser/Parser.zig");
var parser = Parser.init(page.arena, doc.asNode(), page); var parser = Parser.init(page.arena, doc.asNode(), page);
parser.parse(normalized); parser.parse(normalized);
@@ -61,26 +73,37 @@ pub fn parseFromString(
} }
return .{ .html_document = doc }; return .{ .html_document = doc };
} },
else => {
if (std.mem.eql(u8, mime_type, "text/xml")) {
// Create a new XMLDocument. // Create a new XMLDocument.
const doc = try page._factory.document(XMLDocument{ const doc = try page._factory.document(XMLDocument{
._proto = undefined, ._proto = undefined,
}); });
// Parse XML into XMLDocument. // Parse XML into XMLDocument.
const Parser = @import("../parser/Parser.zig"); const doc_node = doc.asNode();
var parser = Parser.init(page.arena, doc.asNode(), page); var parser = Parser.init(page.arena, doc_node, page);
parser.parseXML(html); parser.parseXML(html);
if (parser.err) |pe| { if (parser.err) |pe| {
return pe.err; return pe.err;
} }
return .{ .xml_document = doc }; // 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;
};
if (first_child.getNodeType() == 7) {
// We're sure that firstChild exist, this cannot fail.
_ = doc_node.removeChild(first_child, page) catch unreachable;
} }
return .{ .xml_document = doc };
},
};
return error.NotSupported; return error.NotSupported;
} }