diff --git a/apps/desktop/src/lib/find-in-page-scope.test.ts b/apps/desktop/src/lib/find-in-page-scope.test.ts index 3741fcf8b2..bc0d75e904 100644 --- a/apps/desktop/src/lib/find-in-page-scope.test.ts +++ b/apps/desktop/src/lib/find-in-page-scope.test.ts @@ -205,6 +205,21 @@ describe('performScopedFind', () => { expect(result.activeOrdinal).toBe(2) expect(surface.querySelectorAll('mark.find-hit').length).toBe(2) }) + + it('keeps searching sibling subtrees after a fully-consumed text node', () => { + // Regression (#81778 review): a text node that was entirely consumed by + // a match used to null the walker's `current`, terminating the sibling + // traversal — `
needleneedle
` only matched the + // first occurrence. The sibling subtree must still be searched. + const surface = plantSurface( + 'surface', + '
needleneedle

needle

' + ) + const result = performScopedFind(surface, 'needle', { forward: true, findNext: false }) + + expect(result.count).toBe(3) + expect(surface.querySelectorAll('mark.find-hit').length).toBe(3) + }) }) describe('scope lifecycle', () => { diff --git a/apps/desktop/src/lib/find-in-page-scope.ts b/apps/desktop/src/lib/find-in-page-scope.ts index 7d21222af7..a2dcafb3ac 100644 --- a/apps/desktop/src/lib/find-in-page-scope.ts +++ b/apps/desktop/src/lib/find-in-page-scope.ts @@ -150,6 +150,13 @@ function highlightMatches(root: Element, query: string): HTMLElement[] { // Search within this text node. Splits may invalidate `textNode`'s // identity, so we re-read it from the parent each iteration. let nodeValue = textNode.nodeValue ?? '' + // Capture the text node's own next sibling BEFORE any replaceChild: + // once the original node is detached, `.nextSibling` reads null and + // the outer walker would stop, skipping every following sibling + // subtree (`
needleneedle
` searching "needle" + // matched only the first). `continueFrom` is the sibling AFTER this + // text node — for a fully-consumed match, the walker resumes there. + const continueFrom = textNode.nextSibling while (true) { const lower = nodeValue.toLowerCase() @@ -205,7 +212,15 @@ function highlightMatches(root: Element, query: string): HTMLElement[] { nodeValue = afterNode.nodeValue ?? '' } - current = textNode ? textNode.nextSibling : null + if (textNode) { + current = textNode.nextSibling + } else { + // The whole text node was consumed by matches — `textNode` was + // detached by replaceChild so its own `.nextSibling` is null. Resume + // the outer walker from the parent's next sibling (captured before + // the first replaceChild), so the sibling subtree is still searched. + current = continueFrom + } } return marks