Skip to content

Commit 16c74cf

Browse files
committed
element: fix toInterface for webcomponents
The webcomponents tag can be anything. But we must return them as HTMLElement for HTML documents.
1 parent cc6d443 commit 16c74cf

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/browser/dom/element.zig

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,24 @@ pub const Element = struct {
6060

6161
pub fn toInterfaceT(comptime T: type, e: *parser.Element) !T {
6262
const tagname = try parser.elementGetTagName(e) orelse {
63-
// in case of null tagname, return the element as it.
63+
// If the owner's document is HTML, assume we have an HTMLElement.
64+
const doc = try parser.nodeOwnerDocument(parser.elementToNode(e));
65+
if (doc != null and !doc.?.is_html) {
66+
return .{ .HTMLElement = @as(*parser.ElementHTML, @ptrCast(e)) };
67+
}
68+
6469
return .{ .Element = e };
6570
};
6671

6772
// TODO SVGElement and MathML are not supported yet.
6873

6974
const tag = parser.Tag.fromString(tagname) catch {
70-
// if the tag is invalid, we don't have an HTMLElement.
75+
// If the owner's document is HTML, assume we have an HTMLElement.
76+
const doc = try parser.nodeOwnerDocument(parser.elementToNode(e));
77+
if (doc != null and doc.?.is_html) {
78+
return .{ .HTMLElement = @as(*parser.ElementHTML, @ptrCast(e)) };
79+
}
80+
7181
return .{ .Element = e };
7282
};
7383

src/browser/polyfill/webcomponents.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,7 @@ test "Browser.webcomponents" {
6161
},
6262

6363
.{ "main.innerHTML", "<lightpanda-test>connected</lightpanda-test>" },
64+
.{ "document.createElement('lightpanda-test').dataset", "[object DataSet]" },
65+
.{ "document.createElement('lightpanda-test.x').dataset", "[object DataSet]" },
6466
}, .{});
6567
}

0 commit comments

Comments
 (0)