Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: make Console and CSS proper namespaces

Karl Seguindbafd87fa29c
diff --git a/src/browser/js/Snapshot.zig b/src/browser/js/Snapshot.zig
index c2ab63146..62728ce72 100644
--- a/src/browser/js/Snapshot.zig
+++ b/src/browser/js/Snapshot.zig
@@ -325,7 +325,7 @@ fn createSnapshotContext(
@setEvalBranchQuota(10_000);
const template_index = comptime bridge.JsApiLookup.getId(JsApi);
const func = v8.v8__FunctionTemplate__GetFunction(templates[template_index], context);
- if (@hasDecl(JsApi.Meta, "name")) {
+ if (@hasDecl(JsApi.Meta, "name") and comptime isNamespace(JsApi) == false) {
const name = JsApi.Meta.name;
const v8_class_name = v8.v8__String__NewFromUtf8(isolate, name.ptr, v8.kNormal, @intCast(name.len));
var maybe_result: v8.MaybeBool = undefined;
@@ -788,6 +788,11 @@ fn inheritsFromHtmlElement(comptime JsApi: type) bool {
return bridge.inheritsOrIs(JsApi, HtmlElement.JsApi);
}
+// Web IDL namespace (CSS, console)
+fn isNamespace(comptime JsApi: type) bool {
+ return @hasDecl(JsApi.Meta, "namespace") and JsApi.Meta.namespace;
+}
+
const Unforgeable = struct {
Owner: type,
name: [:0]const u8,
@@ -802,7 +807,7 @@ fn attachClass(comptime JsApi: type, comptime flatten: bool, isolate: *v8.Isolat
// Namespace objects (e.g. console) expose their members as own properties
// of each instance rather than via the prototype, so Object.entries(...)
// returns them. See https://console.spec.whatwg.org/#console-namespace.
- const own_properties = @hasDecl(JsApi.Meta, "own_properties") and JsApi.Meta.own_properties;
+ const own_properties = isNamespace(JsApi);
const member_template = if (own_properties) instance else prototype;
const decl_names = @typeInfo(JsApi).@"struct".decl_names;
diff --git a/src/browser/tests/console/console.html b/src/browser/tests/console/console.html
index dc7d0ba47..e1aefc286 100644
--- a/src/browser/tests/console/console.html
+++ b/src/browser/tests/console/console.html
@@ -40,7 +40,9 @@
count("ns");
console.countReset("ns");
- testing.expectEqual(true, true);
+ // a namespace has no interface object
+ testing.expectEqual('undefined', typeof Console);
+ testing.expectEqual(false, 'Console' in window);
</script>
<script id=shapes>
diff --git a/src/browser/tests/css.html b/src/browser/tests/css.html
index 48d4ea387..d5ed9193d 100644
--- a/src/browser/tests/css.html
+++ b/src/browser/tests/css.html
@@ -5,6 +5,9 @@
testing.expectEqual('object', typeof CSS);
testing.expectEqual('function', typeof CSS.escape);
testing.expectEqual('function', typeof CSS.supports);
+ // a namespace has no interface object
+ testing.expectEqual('undefined', typeof Css);
+ testing.expectEqual(false, 'Css' in window);
</script>
<script id="entries">
diff --git a/src/browser/webapi/CSS.zig b/src/browser/webapi/CSS.zig
index 3b82168bf..7a95d135c 100644
--- a/src/browser/webapi/CSS.zig
+++ b/src/browser/webapi/CSS.zig
@@ -197,7 +197,7 @@ pub const JsApi = struct {
// Per the CSSOM spec, CSS is a namespace object — members are own
// properties so Object.entries(CSS) returns them.
- pub const own_properties = true;
+ pub const namespace = true;
pub const prototype_chain = bridge.prototypeChain();
pub var class_id: bridge.ClassId = undefined;
diff --git a/src/browser/webapi/Console.zig b/src/browser/webapi/Console.zig
index c0bce7847..168da2aa0 100644
--- a/src/browser/webapi/Console.zig
+++ b/src/browser/webapi/Console.zig
@@ -234,7 +234,7 @@ pub const JsApi = struct {
// Per the console spec, members are own properties of the namespace
// object, so Object.entries(console) returns them.
- pub const own_properties = true;
+ pub const namespace = true;
pub const class_string = "console";