Skip to content

Commit 8e55082

Browse files
authored
Merge pull request #929 from lightpanda-io/fix-webcomponents
webcomponent must be cast as HTMLElement
2 parents cc6d443 + 29378c5 commit 8e55082

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
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/dom/node.zig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,14 @@ pub const Node = struct {
7575
.text => .{ .Text = @as(*parser.Text, @ptrCast(node)) },
7676
.cdata_section => .{ .CDATASection = @as(*parser.CDATASection, @ptrCast(node)) },
7777
.processing_instruction => .{ .ProcessingInstruction = @as(*parser.ProcessingInstruction, @ptrCast(node)) },
78-
.document => .{ .HTMLDocument = @as(*parser.DocumentHTML, @ptrCast(node)) },
78+
.document => blk: {
79+
const doc: *parser.Document = @ptrCast(node);
80+
if (doc.is_html) {
81+
break :blk .{ .HTMLDocument = @as(*parser.DocumentHTML, @ptrCast(node)) };
82+
}
83+
84+
break :blk .{ .Document = doc };
85+
},
7986
.document_type => .{ .DocumentType = @as(*parser.DocumentType, @ptrCast(node)) },
8087
.attribute => .{ .Attr = @as(*parser.Attribute, @ptrCast(node)) },
8188
.document_fragment => .{ .DocumentFragment = @as(*parser.DocumentFragment, @ptrCast(node)) },

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)