mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
refactor: use walker to traverse the nodes
This commit is contained in:
@@ -199,26 +199,15 @@ pub const Node = struct {
|
|||||||
|
|
||||||
pub fn _appendChild(self: *parser.Node, child: *parser.Node) !Union {
|
pub fn _appendChild(self: *parser.Node, child: *parser.Node) !Union {
|
||||||
const self_owner = try parser.nodeOwnerDocument(self);
|
const self_owner = try parser.nodeOwnerDocument(self);
|
||||||
const new_node_owner = try parser.nodeOwnerDocument(child);
|
const child_owner = try parser.nodeOwnerDocument(child);
|
||||||
|
|
||||||
if (new_node_owner == null or (new_node_owner.? != self_owner.?)) {
|
|
||||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const allocator = arena.allocator();
|
|
||||||
var worklist = std.ArrayList(*parser.Node).init(allocator);
|
|
||||||
|
|
||||||
|
if (child_owner == null or (child_owner.? != self_owner.?)) {
|
||||||
|
const w = Walker{};
|
||||||
child.owner = self_owner;
|
child.owner = self_owner;
|
||||||
try worklist.append(child);
|
var current = child;
|
||||||
|
while (try w.get_next(child, current)) |current_node| {
|
||||||
// change ownerDocument of all children of a given node
|
current_node.owner = self_owner;
|
||||||
while (worklist.pop()) |current_node| {
|
current = current_node;
|
||||||
var maybe_child: ?*parser.Node = try parser.nodeFirstChild(current_node);
|
|
||||||
while (maybe_child) |child_node| {
|
|
||||||
child_node.owner = self_owner;
|
|
||||||
try worklist.append(child_node);
|
|
||||||
maybe_child = try parser.nodeNextSibling(child_node);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,25 +307,15 @@ pub const Node = struct {
|
|||||||
const new_node_owner = try parser.nodeOwnerDocument(new_node);
|
const new_node_owner = try parser.nodeOwnerDocument(new_node);
|
||||||
|
|
||||||
if (new_node_owner == null or (new_node_owner.? != self_owner.?)) {
|
if (new_node_owner == null or (new_node_owner.? != self_owner.?)) {
|
||||||
|
const w = Walker{};
|
||||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
const allocator = arena.allocator();
|
|
||||||
|
|
||||||
var worklist = std.ArrayList(*parser.Node).init(allocator);
|
|
||||||
new_node.owner = self_owner;
|
new_node.owner = self_owner;
|
||||||
try worklist.append(new_node);
|
var current = new_node;
|
||||||
|
while (try w.get_next(new_node, current)) |current_node| {
|
||||||
while (worklist.pop()) |current_node| {
|
current_node.owner = self_owner;
|
||||||
var maybe_child: ?*parser.Node = try parser.nodeFirstChild(current_node);
|
current = current_node;
|
||||||
while (maybe_child) |child| {
|
|
||||||
child.owner = self_owner;
|
|
||||||
try worklist.append(child);
|
|
||||||
maybe_child = try parser.nodeNextSibling(child);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ref_node_) |ref_node| {
|
if (ref_node_) |ref_node| {
|
||||||
return Node.toInterface(try parser.nodeInsertBefore(self, new_node, ref_node));
|
return Node.toInterface(try parser.nodeInsertBefore(self, new_node, ref_node));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user