Skip to content

Commit 9373cf9

Browse files
committed
cdp: refacto sendChildNodes
1 parent f040309 commit 9373cf9

File tree

1 file changed

+12
-38
lines changed

1 file changed

+12
-38
lines changed

src/cdp/domains/dom.zig

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -88,70 +88,44 @@ fn performSearch(cmd: anytype) !void {
8888

8989
// dispatchSetChildNodes send the setChildNodes event for the whole DOM tree
9090
// hierarchy of each nodes.
91-
// If a parent has already been registred, we don't re-send a setChildNodes
92-
// event anymore.
9391
// We dispatch event in the reverse order: from the top level to the direct parents.
92+
// TODO we should dispatch a node only if it has never been sent.
9493
fn dispatchSetChildNodes(cmd: anytype, nodes: []*parser.Node) !void {
9594
const arena = cmd.arena;
9695
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
9796
const session_id = bc.session_id orelse return error.SessionIdNotLoaded;
9897

99-
var parents: std.ArrayListUnmanaged(*parser.Node) = .{};
98+
var parents: std.ArrayListUnmanaged(*Node) = .{};
10099
for (nodes) |_n| {
101100
var n = _n;
102101
while (true) {
103102
const p = try parser.nodeParentNode(n) orelse break;
104-
// If the parent exists in he registry, don't send the event.
105-
// In this case we stop browsing the tree, b/c parents must have
106-
// been sent already.
107-
if (bc.node_registry.lookup_by_node.contains(p)) {
108-
break;
109-
}
110-
try parents.append(arena, p);
111103

104+
// Register the node.
105+
const node = try bc.node_registry.register(p);
106+
try parents.append(arena, node);
112107
n = p;
113108
}
114109
}
115110

116111
const plen = parents.items.len;
117112
if (plen == 0) return;
118113

114+
var uniq: std.AutoHashMapUnmanaged(Node.Id, bool) = .{};
119115
var i: usize = plen;
120116
while (i > 0) {
121117
i -= 1;
122-
const n = parents.items[i];
123-
// If the parent exists in he registry, don't send the event.
124-
// Indeed the parent can be twice in the array.
125-
if (bc.node_registry.lookup_by_node.contains(n)) {
126-
continue;
127-
}
118+
const node = parents.items[i];
128119

129-
// Register the node.
130-
const node = try bc.node_registry.register(n);
131-
// If the node has no parent, it's the root node.
132-
// We don't dispatch event for it because we assume the root node is
133-
// dispatched via the DOM.getDocument command.
134-
const p = try parser.nodeParentNode(n) orelse continue;
120+
if (uniq.contains(node.id)) continue;
121+
try uniq.putNoClobber(arena, node.id, true);
135122

136-
// Retrieve the parent from the registry.
137-
const parent_node = try bc.node_registry.register(p);
138-
139-
try cmd.sendEvent("DOM.setChildNodes", .{
140-
.parentId = parent_node.id,
141-
.nodes = .{bc.nodeWriter(node, .{})},
142-
}, .{
143-
.session_id = session_id,
144-
});
145-
}
146-
147-
// now dispatch the event for the node list.
148-
for (nodes) |n| {
149-
// Register the node.
150-
const node = try bc.node_registry.register(n);
151123
// If the node has no parent, it's the root node.
152124
// We don't dispatch event for it because we assume the root node is
153125
// dispatched via the DOM.getDocument command.
154-
const p = try parser.nodeParentNode(n) orelse continue;
126+
const p = try parser.nodeParentNode(node._node) orelse {
127+
continue;
128+
};
155129

156130
// Retrieve the parent from the registry.
157131
const parent_node = try bc.node_registry.register(p);

0 commit comments

Comments
 (0)