Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: Undefined custom elements should have a prototype of HTMLElement

Karl Seguin3ef8c46c79ee
diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zig
index fee409ad0..3d5e873a7 100644
--- a/src/browser/js/Local.zig
+++ b/src/browser/js/Local.zig
@@ -1375,7 +1375,7 @@ fn resolveT(comptime T: type, value: *T) Resolved {
const Meta = T.JsApi.Meta;
return .{
.ptr = value,
- .class_id = Meta.class_id,
+ .class_id = if (@hasDecl(Meta, "wrap_as")) Meta.wrap_as.Meta.class_id else Meta.class_id,
.prototype_chain = &Meta.prototype_chain,
.finalizer = blk: {
const FT = (comptime findFinalizerType(T)) orelse break :blk null;
diff --git a/src/browser/tests/custom_elements/upgrade.html b/src/browser/tests/custom_elements/upgrade.html
index 90179f67b..00b04b082 100644
--- a/src/browser/tests/custom_elements/upgrade.html
+++ b/src/browser/tests/custom_elements/upgrade.html
@@ -353,3 +353,25 @@
// testing.expectEqual('c', childCalls[0].newValue);
// }
</script>
+
+<my-unresolved id="unresolved"></my-unresolved>
+<script id="undefined_prototype">
+{
+ const parsed = document.getElementById('unresolved');
+ testing.expectEqual(HTMLElement.prototype, Object.getPrototypeOf(parsed));
+
+ const created = document.createElement('my-unresolved');
+ testing.expectEqual(HTMLElement.prototype, Object.getPrototypeOf(created));
+
+ const windowless = document.implementation.createHTMLDocument('');
+ const imported = windowless.createElement('my-unresolved');
+ testing.expectEqual(HTMLElement.prototype, Object.getPrototypeOf(imported));
+
+ class MyUnresolved extends HTMLElement {}
+ customElements.define('my-unresolved', MyUnresolved);
+ testing.expectEqual(MyUnresolved.prototype, Object.getPrototypeOf(parsed));
+ // define() only upgrades connected elements
+ testing.expectEqual(HTMLElement.prototype, Object.getPrototypeOf(created));
+ testing.expectEqual(HTMLElement.prototype, Object.getPrototypeOf(imported));
+}
+</script>
diff --git a/src/browser/webapi/element/html/Custom.zig b/src/browser/webapi/element/html/Custom.zig
index 60ef4cd21..cc6525f54 100644
--- a/src/browser/webapi/element/html/Custom.zig
+++ b/src/browser/webapi/element/html/Custom.zig
@@ -365,5 +365,9 @@ pub const JsApi = struct {
pub const Meta = struct {
pub const prototype_chain = bridge.prototypeChain();
pub var class_id: bridge.ClassId = undefined;
+ // The template only applies to wrappers created outside construction:
+ // undefined elements, or a defined one first seen by an isolated world.
+ // Both get HTMLElement.prototype.
+ pub const wrap_as = HtmlElement.JsApi;
};
};