Skip to content

Commit 9b35736

Browse files
authored
Merge pull request #786 from lightpanda-io/custom-elements
add CustomElementRegistry
2 parents e9d7a94 + 9f54cb3 commit 9b35736

File tree

8 files changed

+486
-6
lines changed

8 files changed

+486
-6
lines changed

src/browser/dom/document.zig

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
const std = @import("std");
2020

21+
const log = @import("../../log.zig");
2122
const parser = @import("../netsurf.zig");
2223
const Page = @import("../page.zig").Page;
2324

@@ -120,9 +121,28 @@ pub const Document = struct {
120121
return try Element.toInterface(e);
121122
}
122123

123-
pub fn _createElement(self: *parser.Document, tag_name: []const u8) !ElementUnion {
124-
const e = try parser.documentCreateElement(self, tag_name);
125-
return try Element.toInterface(e);
124+
const CreateElementResult = union(enum) {
125+
element: ElementUnion,
126+
custom: Env.JsObject,
127+
};
128+
129+
pub fn _createElement(self: *parser.Document, tag_name: []const u8, page: *Page) !CreateElementResult {
130+
const custom_element = page.window.custom_elements._get(tag_name) orelse {
131+
const e = try parser.documentCreateElement(self, tag_name);
132+
return .{ .element = try Element.toInterface(e) };
133+
};
134+
135+
var result: Env.Function.Result = undefined;
136+
const js_obj = custom_element.newInstance(&result) catch |err| {
137+
log.fatal(.user_script, "newInstance error", .{
138+
.err = result.exception,
139+
.stack = result.stack,
140+
.tag_name = tag_name,
141+
.source = "createElement",
142+
});
143+
return err;
144+
};
145+
return .{ .custom = js_obj };
126146
}
127147

128148
pub fn _createElementNS(self: *parser.Document, ns: []const u8, tag_name: []const u8) !ElementUnion {

src/browser/env.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const WebApis = struct {
3333
@import("xhr/xhr.zig").Interfaces,
3434
@import("xhr/form_data.zig").Interfaces,
3535
@import("xmlserializer/xmlserializer.zig").Interfaces,
36+
@import("webcomponents/webcomponents.zig").Interfaces,
3637
});
3738
};
3839

0 commit comments

Comments
 (0)