node: add replaceChild method

Signed-off-by: Francis Bouvier <francis.bouvier@gmail.com>
This commit is contained in:
Francis Bouvier
2023-09-27 11:13:53 +02:00
parent 08ded87e02
commit 2e3d1220f6
2 changed files with 17 additions and 0 deletions

View File

@@ -173,6 +173,11 @@ pub const Node = struct {
const res = parser.nodeRemoveChild(self, child);
return Node.toInterface(res);
}
pub fn _replaceChild(self: *parser.Node, new_child: *parser.Node, old_child: *parser.Node) Union {
const res = parser.nodeReplaceChild(self, new_child, old_child);
return Node.toInterface(res);
}
};
pub const Types = generate.Tuple(.{
@@ -349,4 +354,10 @@ pub fn testExecFn(
.{ .src = "content.lastChild.__proto__.constructor.name !== 'HTMLHeadingElement'", .ex = "true" },
};
try checkCases(js_env, &node_remove_child);
var node_replace_child = [_]Case{
.{ .src = "let replace = document.createElement('div')", .ex = "undefined" },
.{ .src = "link.replaceChild(replace, insertBefore) !== undefined", .ex = "true" },
};
try checkCases(js_env, &node_replace_child);
}

View File

@@ -383,6 +383,12 @@ pub fn nodeRemoveChild(node: *Node, child: *Node) *Node {
return res.?;
}
pub fn nodeReplaceChild(node: *Node, new_child: *Node, old_child: *Node) *Node {
var res: ?*Node = undefined;
_ = nodeVtable(node).dom_node_replace_child.?(node, new_child, old_child, &res);
return res.?;
}
// CharacterData
pub const CharacterData = c.dom_characterdata;