Skip to content

Commit d7ce6bd

Browse files
committed
Replace HTMLCollection postAttach's with indexed/named getter
This solves two issues. First, it's more correct, the indexers should be live. Second, it makes sure that anything with an HTMLCollection prototype, like HTMLOptionsCollection, also gets access to the index getters. We could solve the 2nd issue by making `postAttach` work up the prototype chain, but since postAttach is wrong (not live), I prefer this solution.
1 parent 2207757 commit d7ce6bd

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/browser/dom/html_collection.zig

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -429,24 +429,23 @@ pub const HTMLCollection = struct {
429429
return null;
430430
}
431431

432-
pub fn postAttach(self: *HTMLCollection, js_this: JsThis) !void {
433-
const len = try self.get_length();
434-
for (0..len) |i| {
435-
const node = try self.item(@intCast(i)) orelse unreachable;
436-
const e = @as(*parser.Element, @ptrCast(node));
437-
const as_interface = try Element.toInterface(e);
438-
try js_this.setIndex(@intCast(i), as_interface, .{});
439-
440-
if (try item_name(e)) |name| {
441-
// Even though an entry might have an empty id, the spec says
442-
// that namedItem("") should always return null
443-
if (name.len > 0) {
444-
// Named fields should not be enumerable (it is defined with
445-
// the LegacyUnenumerableNamedProperties flag.)
446-
try js_this.set(name, as_interface, .{ .DONT_ENUM = true });
447-
}
448-
}
432+
pub fn indexed_get(self: *HTMLCollection, index: u32, has_value: *bool) !?Union {
433+
return (try _item(self, index)) orelse {
434+
has_value.* = false;
435+
return undefined;
436+
};
437+
}
438+
439+
pub fn named_get(self: *const HTMLCollection, name: []const u8, has_value: *bool) !?Union {
440+
// Even though an entry might have an empty id, the spec says
441+
// that namedItem("") should always return null
442+
if (name.len == 0) {
443+
return null;
449444
}
445+
return (try _namedItem(self, name)) orelse {
446+
has_value.* = false;
447+
return undefined;
448+
};
450449
}
451450
};
452451

src/tests/html/select.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
o3.value = 'o3';
5656
options.add(o3)
5757
testing.expectEqual(3, options.length);
58+
testing.expectEqual('o3', options[2].value);
5859
testing.expectEqual('o3', options.item(2).value);
5960

6061
let o4 = document.createElement('option');
@@ -71,5 +72,9 @@
7172

7273
options.remove(3)
7374
testing.expectEqual(4, options.length);
75+
testing.expectEqual('o3', options[3].value);
7476
testing.expectEqual('o3', options.item(3).value);
77+
78+
testing.expectEqual(undefined, options[10]);
79+
testing.expectEqual(null, options.item(10));
7580
</script>

0 commit comments

Comments
 (0)