Skip to content

Commit 04c990d

Browse files
authored
Merge pull request #649 from lightpanda-io/html_collection_named_properties
Fix HTMLCollection named property issues
2 parents 210d4f6 + 1a83e69 commit 04c990d

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/browser/dom/html_collection.zig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,16 @@ pub const HTMLCollection = struct {
406406
for (0..len) |i| {
407407
const node = try self.item(@intCast(i)) orelse unreachable;
408408
const e = @as(*parser.Element, @ptrCast(node));
409-
try js_this.setIndex(@intCast(i), e);
409+
try js_this.setIndex(@intCast(i), e, .{});
410410

411411
if (try item_name(e)) |name| {
412-
try js_this.set(name, e);
412+
// Even though an entry might have an empty id, the spec says
413+
// that namedItem("") should always return null
414+
if (name.len > 0) {
415+
// Named fields should not be enumerable (it is defined with
416+
// the LegacyUnenumerableNamedProperties flag.)
417+
try js_this.set(name, e, .{ .DONT_ENUM = true });
418+
}
413419
}
414420
}
415421
}

src/browser/dom/nodelist.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub const NodeList = struct {
173173
const len = self.get_length();
174174
for (0..len) |i| {
175175
const node = try self._item(@intCast(i)) orelse unreachable;
176-
try js_this.setIndex(i, node);
176+
try js_this.setIndex(@intCast(i), node, .{});
177177
}
178178
}
179179
};

src/runtime/js.zig

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -894,20 +894,28 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
894894
// for this declaration is _a lot_ easier.
895895
const _JSOBJECT_ID_KLUDGE = true;
896896

897-
pub fn setIndex(self: JsObject, index: usize, value: anytype) !void {
897+
const SetOpts = packed struct(u32) {
898+
READ_ONLY: bool = false,
899+
DONT_ENUM: bool = false,
900+
DONT_DELETE: bool = false,
901+
_: u29 = 0,
902+
};
903+
pub fn setIndex(self: JsObject, index: u32, value: anytype, opts: SetOpts) !void {
898904
const key = switch (index) {
899905
inline 0...1000 => |i| std.fmt.comptimePrint("{d}", .{i}),
900906
else => try std.fmt.allocPrint(self.scope.scope_arena, "{d}", .{index}),
901907
};
902-
return self.set(key, value);
908+
return self.set(key, value, opts);
903909
}
904910

905-
pub fn set(self: JsObject, key: []const u8, value: anytype) !void {
911+
pub fn set(self: JsObject, key: []const u8, value: anytype, opts: SetOpts) !void {
906912
const scope = self.scope;
907913

908914
const js_key = v8.String.initUtf8(scope.isolate, key);
909915
const js_value = try scope.zigValueToJs(value);
910-
if (!self.js_obj.setValue(scope.context, js_key, js_value)) {
916+
917+
const res = self.js_obj.defineOwnProperty(scope.context, js_key.toName(), js_value, @bitCast(opts)) orelse false;
918+
if (!res) {
911919
return error.FailedToSet;
912920
}
913921
}
@@ -957,12 +965,12 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
957965

958966
const _JSTHIS_ID_KLUDGE = true;
959967

960-
pub fn setIndex(self: JsThis, index: usize, value: anytype) !void {
961-
return self.obj.setIndex(index, value);
968+
pub fn setIndex(self: JsThis, index: u32, value: anytype, opts: JsObject.SetOpts) !void {
969+
return self.obj.setIndex(index, value, opts);
962970
}
963971

964-
pub fn set(self: JsThis, key: []const u8, value: anytype) !void {
965-
return self.obj.set(key, value);
972+
pub fn set(self: JsThis, key: []const u8, value: anytype, opts: JsObject.SetOpts) !void {
973+
return self.obj.set(key, value, opts);
966974
}
967975
};
968976

0 commit comments

Comments
 (0)