diff --git a/src/dom/html_collection.zig b/src/dom/html_collection.zig index 1bd28034..c528a063 100644 --- a/src/dom/html_collection.zig +++ b/src/dom/html_collection.zig @@ -338,6 +338,34 @@ pub const WalkerNone = struct { } }; +pub const HTMLCollectionIterator = struct { + pub const mem_guarantied = true; + + coll: *HTMLCollection, + index: u32 = 0, + + pub const Return = struct { + value: ?Union, + done: bool, + }; + + pub fn _next(self: *HTMLCollectionIterator, allocator: std.mem.Allocator) !Return { + const e = try self.coll._item(allocator, self.index); + if (e == null) { + return Return{ + .value = null, + .done = true, + }; + } + + self.index += 1; + return Return{ + .value = e, + .done = false, + }; + } +}; + // WEB IDL https://dom.spec.whatwg.org/#htmlcollection // HTMLCollection is re implemented in zig here because libdom // dom_html_collection expects a comparison function callback as arguement. @@ -370,6 +398,12 @@ pub const HTMLCollection = struct { return try self.walker.get_next(self.root.?, null); } + pub fn _symbol_iterator(self: *HTMLCollection) HTMLCollectionIterator { + return HTMLCollectionIterator{ + .coll = self, + }; + } + /// get_length computes the collection's length dynamically according to /// the current root structure. // TODO: nodes retrieved must be de-referenced. diff --git a/src/dom/node.zig b/src/dom/node.zig index 51eaacac..5ba2ee91 100644 --- a/src/dom/node.zig +++ b/src/dom/node.zig @@ -19,6 +19,7 @@ const Document = @import("document.zig").Document; const DocumentType = @import("document_type.zig").DocumentType; const DocumentFragment = @import("document_fragment.zig").DocumentFragment; const HTMLCollection = @import("html_collection.zig").HTMLCollection; +const HTMLCollectionIterator = @import("html_collection.zig").HTMLCollectionIterator; // HTML const HTML = @import("../html/html.zig"); @@ -34,6 +35,7 @@ pub const Interfaces = generate.Tuple(.{ DocumentType, DocumentFragment, HTMLCollection, + HTMLCollectionIterator, HTML.Interfaces, });