mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-30 07:31:47 +00:00
dom: implement HTMLCollection
We can't simply use the libdom dom_document_get_elements_by_tag_name
because it follows an old version of the specifications and returns a
NodeList.
Since
190700b7c1
the spec changed in favor of returning an HTMLCollection.
So I'm trying to re-implement the HTMLCollection in zig.
This commit is contained in:
@@ -7,22 +7,122 @@ const jsruntime = @import("jsruntime");
|
||||
const Element = @import("element.zig").Element;
|
||||
|
||||
// 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.
|
||||
// But we wanted a dynamically comparison here, according to the match tagname.
|
||||
pub const HTMLCollection = struct {
|
||||
pub const Self = parser.HTMLCollection;
|
||||
pub const mem_guarantied = true;
|
||||
|
||||
// JS funcs
|
||||
// --------
|
||||
root: *parser.Node,
|
||||
// match is used to select node against their name.
|
||||
// match comparison is case sensitive.
|
||||
match: []const u8,
|
||||
|
||||
pub fn _get_length(self: *parser.HTMLCollection) u32 {
|
||||
return parser.HTMLCollectionLength(self);
|
||||
/// _get_length computes the collection's length dynamically according to
|
||||
/// the current root structure.
|
||||
// TODO: nodes retrieved must be de-referenced.
|
||||
pub fn get_length(self: *HTMLCollection) u32 {
|
||||
var len: u32 = 0;
|
||||
var node: ?*parser.Node = self.root;
|
||||
var ntype: parser.NodeType = undefined;
|
||||
|
||||
var is_wildcard = std.mem.eql(u8, self.match, "*");
|
||||
|
||||
while (node != null) {
|
||||
ntype = parser.nodeType(node.?);
|
||||
if (ntype == .element) {
|
||||
if (is_wildcard or std.mem.eql(u8, self.match, parser.nodeName(node.?))) {
|
||||
len += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Iterate hover the DOM tree.
|
||||
var next = parser.nodeFirstChild(node.?);
|
||||
if (next != null) {
|
||||
node = next;
|
||||
continue;
|
||||
}
|
||||
|
||||
next = parser.nodeNextSibling(node.?);
|
||||
if (next != null) {
|
||||
node = next;
|
||||
continue;
|
||||
}
|
||||
|
||||
var parent = parser.nodeParentNode(node.?);
|
||||
var lastchild = parser.nodeLastChild(parent.?);
|
||||
while (node.? != self.root and node.? == lastchild) {
|
||||
node = parent;
|
||||
parent = parser.nodeParentNode(node.?);
|
||||
lastchild = parser.nodeLastChild(parent.?);
|
||||
}
|
||||
|
||||
if (node.? == self.root) {
|
||||
node = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
node = parser.nodeNextSibling(node.?);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
pub fn _item(self: *parser.HTMLCollection, index: u32) ?*parser.Element {
|
||||
return parser.HTMLCollectionItem(self, index);
|
||||
pub fn _item(self: *HTMLCollection, index: u32) ?*parser.Element {
|
||||
var len: u32 = 0;
|
||||
var node: ?*parser.Node = self.root;
|
||||
var ntype: parser.NodeType = undefined;
|
||||
|
||||
var is_wildcard = std.mem.eql(u8, self.match, "*");
|
||||
|
||||
while (node != null) {
|
||||
ntype = parser.nodeType(node.?);
|
||||
if (ntype == .element) {
|
||||
if (is_wildcard or std.mem.eql(u8, self.match, parser.nodeName(node.?))) {
|
||||
len += 1;
|
||||
|
||||
// check if we found the searched element.
|
||||
if (len == index + 1) {
|
||||
return @as(*parser.Element, @ptrCast(node));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Iterate hover the DOM tree.
|
||||
var next = parser.nodeFirstChild(node.?);
|
||||
if (next != null) {
|
||||
node = next;
|
||||
continue;
|
||||
}
|
||||
|
||||
next = parser.nodeNextSibling(node.?);
|
||||
if (next != null) {
|
||||
node = next;
|
||||
continue;
|
||||
}
|
||||
|
||||
var parent = parser.nodeParentNode(node.?);
|
||||
var lastchild = parser.nodeLastChild(parent.?);
|
||||
while (node.? != self.root and node.? == lastchild) {
|
||||
node = parent;
|
||||
parent = parser.nodeParentNode(node.?);
|
||||
lastchild = parser.nodeLastChild(parent.?);
|
||||
}
|
||||
|
||||
if (node.? == self.root) {
|
||||
node = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
node = parser.nodeNextSibling(node.?);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn _namedItem(self: *parser.HTMLCollection, name: []const u8) ?*parser.Element {
|
||||
return parser.HTMLCollectionNamedItem(self, name);
|
||||
pub fn _namedItem(self: *HTMLCollection, name: []const u8) ?*parser.Element {
|
||||
_ = name;
|
||||
_ = self;
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user