dom: add HTMLCollection

This commit is contained in:
Pierre Tachoire
2023-10-24 17:12:59 +02:00
parent a238eed065
commit 2e40837f0d
3 changed files with 64 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
const std = @import("std");
const parser = @import("../netsurf.zig");
const jsruntime = @import("jsruntime");
const Element = @import("element.zig").Element;
// WEB IDL https://dom.spec.whatwg.org/#htmlcollection
pub const HTMLCollection = struct {
pub const Self = parser.HTMLCollection;
pub const mem_guarantied = true;
// JS funcs
// --------
pub fn _get_length(self: *parser.HTMLCollection) u32 {
return parser.HTMLCollectionLength(self);
}
pub fn _item(self: *parser.HTMLCollection, index: u32) ?*parser.Element {
return parser.HTMLCollectionItem(self, index);
}
pub fn _namedItem(self: *parser.HTMLCollection, name: []const u8) ?*parser.Element {
return parser.HTMLCollectionNamedItem(self, name);
}
};

View File

@@ -14,6 +14,7 @@ const EventTarget = @import("event_target.zig").EventTarget;
const CData = @import("character_data.zig");
const Element = @import("element.zig").Element;
const Document = @import("document.zig").Document;
const HTMLCollection = @import("html_collection.zig").HTMLCollection;
// HTML
const HTML = @import("../html/html.zig");
@@ -25,6 +26,7 @@ pub const Interfaces = generate.Tuple(.{
CData.Interfaces,
Element,
Document,
HTMLCollection,
HTML.Interfaces,
});

View File

@@ -618,6 +618,40 @@ pub fn elementGetAttribute(elem: *Element, name: []const u8) ?[]const u8 {
return stringToData(s.?);
}
// HTMLCollection
pub const HTMLCollection = c.dom_html_collection;
pub fn HTMLCollectionLength(collection: *HTMLCollection) u32 {
var ln: u32 = undefined;
_ = c.dom_html_collection_get_length(collection, &ln);
return ln;
}
pub fn HTMLCollectionItem(collection: *HTMLCollection, index: u32) ?*Element {
var n: [*c]c.dom_node = undefined;
_ = c.dom_html_collection_item(collection, index, &n);
if (n == null) {
return null;
}
// cast [*c]c.dom_node into *Element
return @as(*Element, @ptrCast(n));
}
pub fn HTMLCollectionNamedItem(collection: *HTMLCollection, name: []const u8) ?*Element {
var n: [*c]c.dom_node = undefined;
_ = c.dom_html_collection_named_item(collection, stringFromData(name), &n);
if (n == null) {
return null;
}
// cast [*c]c.dom_node into *Element
return @as(*Element, @ptrCast(n));
}
// ElementHTML
pub const ElementHTML = c.dom_html_element;