dom: add element children and getelementsby*

This commit is contained in:
Pierre Tachoire
2023-12-12 15:24:44 +01:00
parent 6f91537354
commit fb2ba6700e
2 changed files with 41 additions and 0 deletions

View File

@@ -6,6 +6,8 @@ const jsruntime = @import("jsruntime");
const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;
const collection = @import("html_collection.zig");
const Node = @import("node.zig").Node;
const HTMLElem = @import("../html/elements.zig");
pub const Union = @import("../html/elements.zig").Union;
@@ -122,6 +124,40 @@ pub const Element = struct {
// Return true.
return true;
}
pub fn _getElementsByTagName(
self: *parser.Element,
alloc: std.mem.Allocator,
tag_name: []const u8,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(
alloc,
parser.elementToNode(self),
tag_name,
false,
);
}
pub fn _getElementsByClassName(
self: *parser.Element,
alloc: std.mem.Allocator,
classNames: []const u8,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByClassName(
alloc,
parser.elementToNode(self),
classNames,
false,
);
}
// ParentNode
// https://dom.spec.whatwg.org/#parentnode
pub fn get_children(self: *parser.Element) !collection.HTMLCollection {
return try collection.HTMLCollectionChildren(parser.elementToNode(self), false);
}
pub fn deinit(_: *parser.Element, _: std.mem.Allocator) void {}
};
// Tests

View File

@@ -357,7 +357,12 @@ pub fn testExecFn(
.{ .src = "getElementsByTagNameAll.item(7).localName", .ex = "p" },
.{ .src = "getElementsByTagNameAll.namedItem('para-empty-child').localName", .ex = "span" },
.{ .src = "document.getElementById('content').getElementsByTagName('*').length", .ex = "4" },
.{ .src = "document.getElementById('content').getElementsByTagName('p').length", .ex = "2" },
.{ .src = "document.getElementById('content').getElementsByTagName('div').length", .ex = "0" },
.{ .src = "document.children.length", .ex = "1" },
.{ .src = "document.getElementById('content').children.length", .ex = "3" },
// check liveness
.{ .src = "let content = document.getElementById('content')", .ex = "undefined" },