mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 23:23:28 +00:00
cdp: refacto sendChildNodes
This commit is contained in:
@@ -88,27 +88,22 @@ fn performSearch(cmd: anytype) !void {
|
|||||||
|
|
||||||
// dispatchSetChildNodes send the setChildNodes event for the whole DOM tree
|
// dispatchSetChildNodes send the setChildNodes event for the whole DOM tree
|
||||||
// hierarchy of each nodes.
|
// hierarchy of each nodes.
|
||||||
// If a parent has already been registred, we don't re-send a setChildNodes
|
|
||||||
// event anymore.
|
|
||||||
// We dispatch event in the reverse order: from the top level to the direct parents.
|
// We dispatch event in the reverse order: from the top level to the direct parents.
|
||||||
|
// TODO we should dispatch a node only if it has never been sent.
|
||||||
fn dispatchSetChildNodes(cmd: anytype, nodes: []*parser.Node) !void {
|
fn dispatchSetChildNodes(cmd: anytype, nodes: []*parser.Node) !void {
|
||||||
const arena = cmd.arena;
|
const arena = cmd.arena;
|
||||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||||
const session_id = bc.session_id orelse return error.SessionIdNotLoaded;
|
const session_id = bc.session_id orelse return error.SessionIdNotLoaded;
|
||||||
|
|
||||||
var parents: std.ArrayListUnmanaged(*parser.Node) = .{};
|
var parents: std.ArrayListUnmanaged(*Node) = .{};
|
||||||
for (nodes) |_n| {
|
for (nodes) |_n| {
|
||||||
var n = _n;
|
var n = _n;
|
||||||
while (true) {
|
while (true) {
|
||||||
const p = try parser.nodeParentNode(n) orelse break;
|
const p = try parser.nodeParentNode(n) orelse break;
|
||||||
// If the parent exists in he registry, don't send the event.
|
|
||||||
// In this case we stop browsing the tree, b/c parents must have
|
|
||||||
// been sent already.
|
|
||||||
if (bc.node_registry.lookup_by_node.contains(p)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
try parents.append(arena, p);
|
|
||||||
|
|
||||||
|
// Register the node.
|
||||||
|
const node = try bc.node_registry.register(p);
|
||||||
|
try parents.append(arena, node);
|
||||||
n = p;
|
n = p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,42 +111,21 @@ fn dispatchSetChildNodes(cmd: anytype, nodes: []*parser.Node) !void {
|
|||||||
const plen = parents.items.len;
|
const plen = parents.items.len;
|
||||||
if (plen == 0) return;
|
if (plen == 0) return;
|
||||||
|
|
||||||
|
var uniq: std.AutoHashMapUnmanaged(Node.Id, bool) = .{};
|
||||||
var i: usize = plen;
|
var i: usize = plen;
|
||||||
while (i > 0) {
|
while (i > 0) {
|
||||||
i -= 1;
|
i -= 1;
|
||||||
const n = parents.items[i];
|
const node = parents.items[i];
|
||||||
// If the parent exists in he registry, don't send the event.
|
|
||||||
// Indeed the parent can be twice in the array.
|
if (uniq.contains(node.id)) continue;
|
||||||
if (bc.node_registry.lookup_by_node.contains(n)) {
|
try uniq.putNoClobber(arena, node.id, true);
|
||||||
|
|
||||||
|
// If the node has no parent, it's the root node.
|
||||||
|
// We don't dispatch event for it because we assume the root node is
|
||||||
|
// dispatched via the DOM.getDocument command.
|
||||||
|
const p = try parser.nodeParentNode(node._node) orelse {
|
||||||
continue;
|
continue;
|
||||||
}
|
};
|
||||||
|
|
||||||
// Register the node.
|
|
||||||
const node = try bc.node_registry.register(n);
|
|
||||||
// If the node has no parent, it's the root node.
|
|
||||||
// We don't dispatch event for it because we assume the root node is
|
|
||||||
// dispatched via the DOM.getDocument command.
|
|
||||||
const p = try parser.nodeParentNode(n) orelse continue;
|
|
||||||
|
|
||||||
// Retrieve the parent from the registry.
|
|
||||||
const parent_node = try bc.node_registry.register(p);
|
|
||||||
|
|
||||||
try cmd.sendEvent("DOM.setChildNodes", .{
|
|
||||||
.parentId = parent_node.id,
|
|
||||||
.nodes = .{bc.nodeWriter(node, .{})},
|
|
||||||
}, .{
|
|
||||||
.session_id = session_id,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// now dispatch the event for the node list.
|
|
||||||
for (nodes) |n| {
|
|
||||||
// Register the node.
|
|
||||||
const node = try bc.node_registry.register(n);
|
|
||||||
// If the node has no parent, it's the root node.
|
|
||||||
// We don't dispatch event for it because we assume the root node is
|
|
||||||
// dispatched via the DOM.getDocument command.
|
|
||||||
const p = try parser.nodeParentNode(n) orelse continue;
|
|
||||||
|
|
||||||
// Retrieve the parent from the registry.
|
// Retrieve the parent from the registry.
|
||||||
const parent_node = try bc.node_registry.register(p);
|
const parent_node = try bc.node_registry.register(p);
|
||||||
|
|||||||
Reference in New Issue
Block a user