mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
Merge pull request #132 from lightpanda-io/dom-parentnode-prepend
Dom parentnode prepend/append/replaceChildren
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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 {}
|
||||
};
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Submodule tests/wpt updated: 28814be2ea...c9e7658223
Reference in New Issue
Block a user