Merge pull request #1340 from lightpanda-io/nikneym/parse-from-string-return
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
zig-test / zig build dev (push) Has been cancelled
zig-test / browser fetch (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled

Return `*Document` instead of tagged union in `parseFromString`
This commit is contained in:
Karl Seguin
2026-01-08 19:16:14 +08:00
committed by GitHub

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 {