dom: add HTMLCollectionIterator

This commit is contained in:
Pierre Tachoire
2023-11-20 10:50:20 +01:00
committed by Francis Bouvier
parent 7cef1bb550
commit 0b17d79baa
2 changed files with 36 additions and 0 deletions

View File

@@ -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.

View File

@@ -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,
});