Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit a218fbe

Browse files
Gorashdmo-odoo
authored andcommitted
[FIX] Core: refactor range selectedNodes to improve perf
1 parent 2a85810 commit a218fbe

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

packages/core/src/VRange.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,27 @@ export class VRange {
116116
const bound = this.end.next();
117117
const endContainers = this.end.ancestors();
118118
while ((node = node.next()) && node !== bound) {
119-
if (!endContainers.includes(node)) {
120-
let selectedNode: VNode = node;
121-
while (!selectedNode?.test(FragmentNode) && selectedNode?.test(predicate)) {
122-
if (selectedNode.editable) {
123-
selectedNodes.push(selectedNode);
119+
if (
120+
!endContainers.includes(node) &&
121+
!(node instanceof FragmentNode) &&
122+
node.editable &&
123+
node?.test(predicate)
124+
) {
125+
selectedNodes.push(node);
126+
}
127+
}
128+
const alreadyTested = new Set<VNode>();
129+
for (const selectedNode of selectedNodes) {
130+
// Find the next ancestor whose children are all selected
131+
// and add it to the list.
132+
// TODO: Ideally, selected nodes should be returned in DFS order.
133+
const ancestor = selectedNode.parent;
134+
if (ancestor && !alreadyTested.has(ancestor)) {
135+
alreadyTested.add(ancestor);
136+
if (ancestor.children().every(child => selectedNodes.includes(child))) {
137+
if (!selectedNodes.includes(ancestor)) {
138+
selectedNodes.push(ancestor);
124139
}
125-
// Find the next ancestor whose children are all selected
126-
// and add it to the list.
127-
selectedNode = selectedNode.ancestor(ancestor => {
128-
return ancestor.children().every(child => {
129-
return selectedNodes.includes(child);
130-
});
131-
});
132140
}
133141
}
134142
}

0 commit comments

Comments
 (0)