From 4e86caa1e2a07fd3895d48285213f87ddd298998 Mon Sep 17 00:00:00 2001 From: Chen Jin Date: Sun, 9 Aug 2026 18:01:51 +0800 Subject: [PATCH] fix(desktop): re-wrap on findNext when stale marks no longer cover all matches (#81778) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The fast path trusted that marks matching the query case-insensitively implied the set still covered every match. A stale mark from an earlier query (raced re-wrap, external DOM writes) can pass that per-mark comparison while live occurrences stay unwrapped — stepping then walks old highlights and the new matches never light up. Gate the fast path on a read-only coverage check (same skip rules as the walker) so any unmarked occurrence forces a re-wrap. Co-Authored-By: Claude Fable 5 --- .../src/lib/find-in-page-scope.test.ts | 42 +++++++++++++++++++ apps/desktop/src/lib/find-in-page-scope.ts | 42 ++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) 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 2f99c66bb1..334d510bc2 100644 --- a/apps/desktop/src/lib/find-in-page-scope.test.ts +++ b/apps/desktop/src/lib/find-in-page-scope.test.ts @@ -167,6 +167,48 @@ describe('performScopedFind', () => { expect(result.activeOrdinal).toBe(2) }) + it('re-wraps when a stale mark from an earlier query survives (fast path, #81778 review)', () => { + // Regression: a mark left over from an earlier query can match the + // current query case-insensitively even though the mark set no longer + // covers every occurrence — the genuine mark was destroyed by a + // re-render, the stale one survived, and the current query's live + // matches arrived unwrapped. The fast path must not step on the stale + // set — it must drop it and re-wrap, or the live match stays dark. + const surface = plantSurface('surface', '

hermes

') + performScopedFind(surface, 'hermes', { forward: true, findNext: false }) + // Re-render replaces the paragraph (destroying the genuine mark)… + surface.querySelector('p')!.innerHTML = 'beta' + // …but a stale mark survives the edit (external write / raced cleanup), + // and the live content that matches the current query arrives unwrapped. + surface.insertAdjacentHTML('beforeend', 'Hermes') + surface.insertAdjacentHTML('beforeend', '

hermes

') + + const result = performScopedFind(surface, 'hermes', { forward: true, findNext: true }) + + // Re-wrapped: the stale mark's text is re-wrapped as a live occurrence + // and the previously unwrapped match is wrapped too. + expect(result.count).toBe(2) + const marks = [...surface.querySelectorAll('mark.find-hit')] + expect(marks.length).toBe(2) + expect(marks.every(mark => mark.textContent.toLowerCase() === 'hermes')).toBe(true) + }) + + it('re-wraps when new unmarked content contains the query (fast path, #81778 review)', () => { + // Regression: content appended after the last wrap (streamed tokens, + // external writes) carries no mark, so the existing marks no longer + // cover every occurrence. Stepping must re-wrap instead of walking the + // stale set, or the new match would never be highlighted. + const surface = plantSurface('surface', '

needle

') + performScopedFind(surface, 'needle', { forward: true, findNext: false }) + + surface.insertAdjacentHTML('beforeend', '

needle

') + + const result = performScopedFind(surface, 'needle', { forward: true, findNext: true }) + + expect(result.count).toBe(2) + expect(surface.querySelectorAll('mark.find-hit').length).toBe(2) + }) + it('re-wraps when the query changes (marks are not reused across queries)', () => { const surface = plantSurface('surface', '

alpha beta alpha

') performScopedFind(surface, 'alpha', { forward: true, findNext: false }) diff --git a/apps/desktop/src/lib/find-in-page-scope.ts b/apps/desktop/src/lib/find-in-page-scope.ts index 1b364eefad..8b2578cde2 100644 --- a/apps/desktop/src/lib/find-in-page-scope.ts +++ b/apps/desktop/src/lib/find-in-page-scope.ts @@ -246,6 +246,39 @@ function shouldSkipElement(el: Element): boolean { return false } +/** + * True when `root` contains an occurrence of `query` that is NOT inside one + * of our find-hit marks. Read-only counterpart of `highlightMatches` with + * the identical skip rules, used by the findNext fast path to verify that + * the existing marks still cover every match — a stale mark from an earlier + * query can satisfy the per-mark text comparison yet leave live occurrences + * unwrapped, and stepping on that set would never highlight them. + */ +function hasUnmarkedMatch(root: Element, query: string): boolean { + const lowerQuery = query.toLowerCase() + + if (!lowerQuery) { + return false + } + + const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT) + + while (walker.nextNode()) { + const node = walker.currentNode as Text + const parent = node.parentElement + + if (parent && shouldSkipElement(parent)) { + continue + } + + if (node.nodeValue?.toLowerCase().includes(lowerQuery)) { + return true + } + } + + return false +} + /** Remove every find-hit mark we previously added, restoring original text. */ function clearHighlights(root: Element): void { const marks = root.querySelectorAll(`mark.${HIGHLIGHT_CLASS}`) @@ -327,10 +360,17 @@ export function performScopedFind( // case-insensitive comparison, every Enter/⌘G step re-wraps all // highlights, `data-find-active` is lost on the fresh DOM, and the // active ordinal resets to 1 forever (triage finding on #81778). + // The per-mark comparison alone is not enough: a stale mark from an + // earlier query that survived a raced re-wrap (or external DOM writes) + // can match the current query case-insensitively while the mark set no + // longer covers every match — stepping would walk old highlights and + // never wrap the live ones. Only step when the marks match AND cover + // every occurrence (review finding on #81778). const sameQuery = options.findNext && existingMarks.length > 0 && - existingMarks.every(mark => mark.textContent.toLowerCase() === query.toLowerCase()) + existingMarks.every(mark => mark.textContent.toLowerCase() === query.toLowerCase()) && + !hasUnmarkedMatch(root, query) let marks = existingMarks