Skip to content

Commit 3818a90

Browse files
airoweclaude
andcommitted
fix(scroll): guard the targeted scrollable-ancestor walk against cycles
findNearestScrollableAncestor followed parentIndex without a visited set, so a targeted scroll on a malformed tree whose parent chain loops with no scrollable ancestor could hang edge analysis. It now stops on a revisited index. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 273e341 commit 3818a90

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎packages/capture-kit/src/snapshot/__tests__/scroll-edge-state-same-rect-ancestor.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,15 @@ test('scroll top edge: a nested scroller whose rect differs from its scrollable
9090
assert.equal(state.scope, 'inner');
9191
assert.equal(state.canScroll, true);
9292
});
93+
94+
test('scroll top edge: a targeted node in a cyclic chain with no scrollable ancestor ends the walk', async () => {
95+
const nodes = [
96+
windowRoot(),
97+
scrollNode(1, { identifier: 'list', hiddenContentAbove: true }),
98+
{ ...scrollNode(2, { parentIndex: 3 }), type: 'Button', kind: 'button' },
99+
{ ...scrollNode(3, { parentIndex: 2 }), type: 'Group', kind: 'group' },
100+
];
101+
const state = await capture(nodes, 'top', { nodeIndex: 2 });
102+
assert.equal(state.scope, 'list');
103+
assert.equal(state.canScroll, true);
104+
});

‎packages/capture-kit/src/snapshot/scroll-edge-state/selection.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,14 @@ function findNearestScrollableAncestor(
144144
byIndex: ReadonlyMap<number, SnapshotNode>,
145145
): SnapshotNode | null {
146146
if (nodeIndex === undefined) return null;
147+
const visited = new Set<number>();
147148
let node = byIndex.get(nodeIndex);
148-
while (node) {
149+
while (node && !visited.has(node.index)) {
150+
visited.add(node.index);
149151
if (isScrollableNodeLike(node) && isUsableRect(node.rect)) {
150152
return node;
151153
}
152-
node = node.parentIndex === undefined ? undefined : byIndex.get(node.parentIndex);
154+
node = parentOf(node, byIndex);
153155
}
154156
return null;
155157
}

0 commit comments

Comments
 (0)