mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-17 00:38:59 +00:00
dom: fix replace child
This commit is contained in:
@@ -294,27 +294,33 @@ pub const Node = struct {
|
|||||||
// function must accept either node or string.
|
// function must accept either node or string.
|
||||||
// blocked by https://github.com/lightpanda-io/jsruntime-lib/issues/114
|
// blocked by https://github.com/lightpanda-io/jsruntime-lib/issues/114
|
||||||
pub fn replaceChildren(self: *parser.Node, nodes: ?Variadic(*parser.Node)) !void {
|
pub fn replaceChildren(self: *parser.Node, nodes: ?Variadic(*parser.Node)) !void {
|
||||||
|
// remove existing children
|
||||||
|
try removeChildren(self);
|
||||||
|
|
||||||
if (nodes == null) return;
|
if (nodes == null) return;
|
||||||
if (nodes.?.slice.len == 0) return;
|
if (nodes.?.slice.len == 0) return;
|
||||||
|
|
||||||
// remove existing children
|
// add new children
|
||||||
if (try parser.nodeHasChildNodes(self)) {
|
for (nodes.?.slice) |node| {
|
||||||
|
_ = try parser.nodeAppendChild(self, node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn removeChildren(self: *parser.Node) !void {
|
||||||
|
if (!try parser.nodeHasChildNodes(self)) return;
|
||||||
|
|
||||||
const children = try parser.nodeGetChildNodes(self);
|
const children = try parser.nodeGetChildNodes(self);
|
||||||
const ln = try parser.nodeListLength(children);
|
const ln = try parser.nodeListLength(children);
|
||||||
var i: u32 = 0;
|
var i: u32 = 0;
|
||||||
while (i < ln) {
|
while (i < ln) {
|
||||||
defer i += 1;
|
defer i += 1;
|
||||||
const child = try parser.nodeListItem(children, i) orelse continue;
|
// we always retrieve the 0 index child on purpose: libdom nodelist
|
||||||
|
// are dynamic. So the next child to remove is always as pos 0.
|
||||||
|
const child = try parser.nodeListItem(children, 0) orelse continue;
|
||||||
_ = try parser.nodeRemoveChild(self, child);
|
_ = 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 {}
|
pub fn deinit(_: *parser.Node, _: std.mem.Allocator) void {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user