dom: refacto append/prepend/replaceChild in Node

This commit is contained in:
Pierre Tachoire
2024-01-18 09:59:26 +01:00
parent b33fc68898
commit d8df27ead7
3 changed files with 64 additions and 84 deletions

View File

@@ -233,60 +233,21 @@ pub const Document = struct {
// 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 {
if (nodes == null) return;
if (nodes.?.slice.len == 0) return;
const nself = parser.documentToNode(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, node, first.?);
}
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 {
if (nodes == null) return;
if (nodes.?.slice.len == 0) return;
const nself = parser.documentToNode(self);
for (nodes.?.slice) |node| {
_ = try parser.nodeAppendChild(nself, node);
}
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 {
if (nodes == null) return;
if (nodes.?.slice.len == 0) return;
const nself = parser.documentToNode(self);
// remove existing children
if (try parser.nodeHasChildNodes(nself)) {
const children = try parser.nodeGetChildNodes(nself);
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(nself, child);
}
}
// add new children
for (nodes.?.slice) |node| {
_ = try parser.nodeAppendChild(nself, node);
}
return Node.replaceChildren(parser.documentToNode(self), nodes);
}
pub fn deinit(_: *parser.Document, _: std.mem.Allocator) void {}

View File

@@ -286,60 +286,21 @@ pub const Element = struct {
// 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, node, first.?);
}
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 {
if (nodes == null) return;
if (nodes.?.slice.len == 0) return;
const nself = parser.elementToNode(self);
for (nodes.?.slice) |node| {
_ = try parser.nodeAppendChild(nself, node);
}
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 {
if (nodes == null) return;
if (nodes.?.slice.len == 0) return;
const nself = parser.elementToNode(self);
// remove existing children
if (try parser.nodeHasChildNodes(nself)) {
const children = try parser.nodeGetChildNodes(nself);
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(nself, child);
}
}
// add new children
for (nodes.?.slice) |node| {
_ = try parser.nodeAppendChild(nself, node);
}
return Node.replaceChildren(parser.elementToNode(self), nodes);
}
pub fn deinit(_: *parser.Element, _: std.mem.Allocator) void {}

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 {}
};