html_collection: free generated array like keys

This commit is contained in:
Pierre Tachoire
2024-01-11 10:49:17 +01:00
parent 39afb6593d
commit f59f36422c

View File

@@ -359,6 +359,10 @@ pub const HTMLCollection = struct {
cur_idx: ?u32 = undefined, cur_idx: ?u32 = undefined,
cur_node: ?*parser.Node = undefined, cur_node: ?*parser.Node = undefined,
// array_like_keys is used to keep reference to array like interface implementation.
// the collection generates keys string which must be free on deinit.
array_like_keys: std.ArrayListUnmanaged([]u8) = .{},
// start returns the first node to walk on. // start returns the first node to walk on.
fn start(self: HTMLCollection) !?*parser.Node { fn start(self: HTMLCollection) !?*parser.Node {
if (self.root == null) return null; if (self.root == null) return null;
@@ -488,6 +492,8 @@ pub const HTMLCollection = struct {
const k = try std.fmt.allocPrint(alloc, "{d}", .{i}); const k = try std.fmt.allocPrint(alloc, "{d}", .{i});
try js_obj.set(k, v); try js_obj.set(k, v);
self.array_like_keys.append(alloc, k);
if (try item_name(v)) |name| { if (try item_name(v)) |name| {
try js_obj.set(name, v); try js_obj.set(name, v);
} }
@@ -495,6 +501,8 @@ pub const HTMLCollection = struct {
} }
pub fn deinit(self: *HTMLCollection, alloc: std.mem.Allocator) void { pub fn deinit(self: *HTMLCollection, alloc: std.mem.Allocator) void {
for (self.array_like_keys_) |k| alloc.free(k);
self.array_like_keys.deinit(alloc);
self.matcher.deinit(alloc); self.matcher.deinit(alloc);
} }
}; };