diff --git a/src/dom/element.zig b/src/dom/element.zig index aa8bc2f5..c696c61f 100644 --- a/src/dom/element.zig +++ b/src/dom/element.zig @@ -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 diff --git a/src/dom/html_collection.zig b/src/dom/html_collection.zig index 871b1d8d..5928a35e 100644 --- a/src/dom/html_collection.zig +++ b/src/dom/html_collection.zig @@ -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" },