From 3af716d93449308f34a7e48c60cc7b9b75d9d510 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 13 Dec 2023 15:19:46 +0100 Subject: [PATCH] dom: add element prepend and append --- src/dom/element.zig | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/dom/element.zig b/src/dom/element.zig index cd12f625..0c370160 100644 --- a/src/dom/element.zig +++ b/src/dom/element.zig @@ -5,6 +5,7 @@ const parser = @import("../netsurf.zig"); const jsruntime = @import("jsruntime"); const Case = jsruntime.test_utils.Case; const checkCases = jsruntime.test_utils.checkCases; +const Variadic = jsruntime.Variadic; const collection = @import("html_collection.zig"); @@ -281,6 +282,39 @@ pub const Element = struct { return list; } + // TODO according with https://dom.spec.whatwg.org/#parentnode, the + // function must accept either node or string. + // blocked by https://github.com/lightpanda-io/jsruntime-lib/issues/114 + pub fn _prepend(self: *parser.Element, nodes: ?Variadic(*parser.Node)) !void { + if (nodes == null) return; + if (nodes.?.slice.len == 0) return; + const nself = parser.elementToNode(self); + const first = try parser.nodeFirstChild(nself); + + if (first == null) { + for (nodes.?.slice) |node| { + _ = try parser.nodeAppendChild(nself, node); + } + return; + } + + for (nodes.?.slice) |node| { + _ = try parser.nodeInsertBefore(nself, first.?, node); + } + } + + // TODO according with https://dom.spec.whatwg.org/#parentnode, the + // function must accept either node or string. + // blocked by https://github.com/lightpanda-io/jsruntime-lib/issues/114 + pub fn _append(self: *parser.Element, nodes: ?Variadic(*parser.Node)) !void { + if (nodes == null) return; + if (nodes.?.slice.len == 0) return; + const nself = parser.elementToNode(self); + for (nodes.?.slice) |node| { + _ = try parser.nodeAppendChild(nself, node); + } + } + pub fn deinit(_: *parser.Element, _: std.mem.Allocator) void {} }; @@ -358,6 +392,9 @@ pub fn testExecFn( .{ .src = "c.firstElementChild.nodeName", .ex = "A" }, .{ .src = "c.lastElementChild.nodeName", .ex = "P" }, .{ .src = "c.childElementCount", .ex = "3" }, + + .{ .src = "c.prepend(document.createTextNode('foo'))", .ex = "undefined" }, + .{ .src = "c.append(document.createTextNode('bar'))", .ex = "undefined" }, }; try checkCases(js_env, &parentNode);