Merge pull request #132 from lightpanda-io/dom-parentnode-prepend

Dom parentnode prepend/append/replaceChildren
This commit is contained in:
Pierre Tachoire
2024-01-18 10:15:24 +01:00
committed by GitHub
5 changed files with 119 additions and 1 deletions

View File

@@ -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 Node = @import("node.zig").Node;
const NodeList = @import("nodelist.zig").NodeList;
@@ -228,6 +229,27 @@ pub const Document = 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.Document, nodes: ?Variadic(*parser.Node)) !void {
return Node.prepend(parser.documentToNode(self), nodes);
}
// 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.Document, nodes: ?Variadic(*parser.Node)) !void {
return Node.append(parser.documentToNode(self), nodes);
}
// 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 _replaceChildren(self: *parser.Document, nodes: ?Variadic(*parser.Node)) !void {
return Node.replaceChildren(parser.documentToNode(self), nodes);
}
pub fn deinit(_: *parser.Document, _: std.mem.Allocator) void {}
};
@@ -381,6 +403,12 @@ pub fn testExecFn(
.{ .src = "nd.firstElementChild", .ex = "null" },
.{ .src = "nd.lastElementChild", .ex = "null" },
.{ .src = "nd.childElementCount", .ex = "0" },
.{ .src = "let emptydoc = document.createElement('html')", .ex = "undefined" },
.{ .src = "emptydoc.prepend(document.createElement('html'))", .ex = "undefined" },
.{ .src = "let emptydoc2 = document.createElement('html')", .ex = "undefined" },
.{ .src = "emptydoc2.append(document.createElement('html'))", .ex = "undefined" },
};
try checkCases(js_env, &parentNode);

View File

@@ -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,27 @@ 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 {
return Node.prepend(parser.elementToNode(self), nodes);
}
// 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 {
return Node.append(parser.elementToNode(self), nodes);
}
// 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 _replaceChildren(self: *parser.Element, nodes: ?Variadic(*parser.Node)) !void {
return Node.replaceChildren(parser.elementToNode(self), nodes);
}
pub fn deinit(_: *parser.Element, _: std.mem.Allocator) void {}
};
@@ -358,6 +380,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);

View File

@@ -4,6 +4,8 @@ const jsruntime = @import("jsruntime");
const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;
const runScript = jsruntime.test_utils.runScript;
const Variadic = jsruntime.Variadic;
const generate = @import("../generate.zig");
const parser = @import("../netsurf.zig");
@@ -257,6 +259,62 @@ pub const Node = struct {
return try Node.toInterface(res);
}
// 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.Node, nodes: ?Variadic(*parser.Node)) !void {
if (nodes == null) return;
if (nodes.?.slice.len == 0) return;
const first = try parser.nodeFirstChild(self);
if (first == null) {
for (nodes.?.slice) |node| {
_ = try parser.nodeAppendChild(self, node);
}
return;
}
for (nodes.?.slice) |node| {
_ = try parser.nodeInsertBefore(self, node, first.?);
}
}
// 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.Node, nodes: ?Variadic(*parser.Node)) !void {
if (nodes == null) return;
if (nodes.?.slice.len == 0) return;
for (nodes.?.slice) |node| {
_ = try parser.nodeAppendChild(self, 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 replaceChildren(self: *parser.Node, nodes: ?Variadic(*parser.Node)) !void {
if (nodes == null) return;
if (nodes.?.slice.len == 0) return;
// remove existing children
if (try parser.nodeHasChildNodes(self)) {
const children = try parser.nodeGetChildNodes(self);
const ln = try parser.nodeListLength(children);
var i: u32 = 0;
while (i < ln) {
defer i += 1;
const child = try parser.nodeListItem(children, i) orelse continue;
_ = try parser.nodeRemoveChild(self, child);
}
}
// add new children
for (nodes.?.slice) |node| {
_ = try parser.nodeAppendChild(self, node);
}
}
pub fn deinit(_: *parser.Node, _: std.mem.Allocator) void {}
};

View File

@@ -620,6 +620,13 @@ pub fn nodeSetTextContent(node: *Node, value: []const u8) !void {
try DOMErr(err);
}
pub fn nodeGetChildNodes(node: *Node) !*NodeList {
var nlist: ?*NodeList = undefined;
const err = nodeVtable(node).dom_node_get_child_nodes.?(node, &nlist);
try DOMErr(err);
return nlist.?;
}
pub fn nodeAppendChild(node: *Node, child: *Node) !*Node {
var res: ?*Node = undefined;
const err = nodeVtable(node).dom_node_append_child.?(node, child, &res);