element: fix toInterface for webcomponents

The webcomponents tag can be anything. But we must return them as
HTMLElement for HTML documents.
This commit is contained in:
Pierre Tachoire
2025-08-07 12:47:02 +02:00
parent cc6d443113
commit 16c74cf3b4
2 changed files with 14 additions and 2 deletions

View File

@@ -60,14 +60,24 @@ pub const Element = struct {
pub fn toInterfaceT(comptime T: type, e: *parser.Element) !T {
const tagname = try parser.elementGetTagName(e) orelse {
// in case of null tagname, return the element as it.
// If the owner's document is HTML, assume we have an HTMLElement.
const doc = try parser.nodeOwnerDocument(parser.elementToNode(e));
if (doc != null and !doc.?.is_html) {
return .{ .HTMLElement = @as(*parser.ElementHTML, @ptrCast(e)) };
}
return .{ .Element = e };
};
// TODO SVGElement and MathML are not supported yet.
const tag = parser.Tag.fromString(tagname) catch {
// if the tag is invalid, we don't have an HTMLElement.
// If the owner's document is HTML, assume we have an HTMLElement.
const doc = try parser.nodeOwnerDocument(parser.elementToNode(e));
if (doc != null and doc.?.is_html) {
return .{ .HTMLElement = @as(*parser.ElementHTML, @ptrCast(e)) };
}
return .{ .Element = e };
};

View File

@@ -61,5 +61,7 @@ test "Browser.webcomponents" {
},
.{ "main.innerHTML", "<lightpanda-test>connected</lightpanda-test>" },
.{ "document.createElement('lightpanda-test').dataset", "[object DataSet]" },
.{ "document.createElement('lightpanda-test.x').dataset", "[object DataSet]" },
}, .{});
}