mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 08:18:59 +00:00
dom: add element.replaceChildren
This commit is contained in:
@@ -315,6 +315,33 @@ pub const Element = struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn deinit(_: *parser.Element, _: std.mem.Allocator) void {}
|
pub fn deinit(_: *parser.Element, _: std.mem.Allocator) void {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -620,6 +620,13 @@ pub fn nodeSetTextContent(node: *Node, value: []const u8) !void {
|
|||||||
try DOMErr(err);
|
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 {
|
pub fn nodeAppendChild(node: *Node, child: *Node) !*Node {
|
||||||
var res: ?*Node = undefined;
|
var res: ?*Node = undefined;
|
||||||
const err = nodeVtable(node).dom_node_append_child.?(node, child, &res);
|
const err = nodeVtable(node).dom_node_append_child.?(node, child, &res);
|
||||||
|
|||||||
Reference in New Issue
Block a user