return *Document instead of tagged union in parseFromString

Did a detour to XML PR and realized this is simpler.
This commit is contained in:
Halil Durak
2026-01-08 12:46:46 +03:00
parent 060afcd459
commit 282a9bbf65

View File

@@ -25,6 +25,7 @@ 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 Document = @import("Document.zig");
const ProcessingInstruction = @import("../webapi/cdata/ProcessingInstruction.zig"); const ProcessingInstruction = @import("../webapi/cdata/ProcessingInstruction.zig");
const DOMParser = @This(); const DOMParser = @This();
@@ -33,26 +34,21 @@ pub fn init() DOMParser {
return .{}; return .{};
} }
pub const HTMLDocumentOrXMLDocument = union(enum) {
html_document: *HTMLDocument,
xml_document: *XMLDocument,
};
pub fn parseFromString( pub fn parseFromString(
_: *const DOMParser, _: *const DOMParser,
html: []const u8, html: []const u8,
mime_type: []const u8, mime_type: []const u8,
page: *Page, page: *Page,
) !HTMLDocumentOrXMLDocument { ) !*Document {
const maybe_target_mime = std.meta.stringToEnum(enum { const target_mime = std.meta.stringToEnum(enum {
@"text/html", @"text/html",
@"text/xml", @"text/xml",
@"application/xml", @"application/xml",
@"application/xhtml+xml", @"application/xhtml+xml",
@"image/svg+xml", @"image/svg+xml",
}, mime_type); }, mime_type) orelse return error.NotSupported;
if (maybe_target_mime) |target_mime| switch (target_mime) { return switch (target_mime) {
.@"text/html" => { .@"text/html" => {
// Create a new HTMLDocument // Create a new HTMLDocument
const doc = try page._factory.document(HTMLDocument{ const doc = try page._factory.document(HTMLDocument{
@@ -72,7 +68,7 @@ pub fn parseFromString(
return pe.err; return pe.err;
} }
return .{ .html_document = doc }; return doc.asDocument();
}, },
else => { else => {
// Create a new XMLDocument. // Create a new XMLDocument.
@@ -100,11 +96,9 @@ pub fn parseFromString(
_ = doc_node.removeChild(first_child, page) catch unreachable; _ = doc_node.removeChild(first_child, page) catch unreachable;
} }
return .{ .xml_document = doc }; return doc.asDocument();
}, },
}; };
return error.NotSupported;
} }
pub const JsApi = struct { pub const JsApi = struct {