From 0ea734933653587f3659f6fc3f400661635bfe9c Mon Sep 17 00:00:00 2001 From: Chen Jin Date: Sun, 9 Aug 2026 13:02:56 +0800 Subject: [PATCH] fix(desktop): case-insensitive fast-path in find-in-page stepping (#81778) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Triage found the findNext fast path compared each existing ``'s textContent byte-for-byte against the typed query. Since highlightMatches stores the ORIGINAL-CASE source slice, the first differently-cased match ('Hermes' for 'hermes', sentence-initial capitals, ALL-CAPS) failed the check, forcing a full re-wrap on every Enter/⌘G: fresh `` elements lose `data-find-active`, `previousActive` resolves null, and the active ordinal stays 1 forever (backward steps land on the last match every time). Compare case-insensitively. Regression test pins the differently-cased stepping behavior: same marks survive, count stays 2, ordinal advances to 2. Co-Authored-By: Claude Fable 5 --- .../desktop/src/lib/find-in-page-scope.test.ts | 18 ++++++++++++++++++ apps/desktop/src/lib/find-in-page-scope.ts | 9 ++++++++- 2 files changed, 26 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 bc0d75e904..2f99c66bb1 100644 --- a/apps/desktop/src/lib/find-in-page-scope.test.ts +++ b/apps/desktop/src/lib/find-in-page-scope.test.ts @@ -149,6 +149,24 @@ describe('performScopedFind', () => { expect(result.activeOrdinal).toBe(2) }) + it('does not re-wrap when the query differs only in case (fast path, triage #81778)', () => { + // The marks store the ORIGINAL-CASE source slice, so a byte-equality + // fast-path check fails on the first differently-cased match and every + // Enter re-wraps, losing `data-find-active` and pinning the ordinal to + // 1 forever. Case-insensitive comparison keeps stepping. + const surface = plantSurface('surface', '

Hermes Hermes

') + performScopedFind(surface, 'hermes', { forward: true, findNext: false }) + + const before = [...surface.querySelectorAll('mark.find-hit')] + const result = performScopedFind(surface, 'hermes', { forward: true, findNext: true }) + + const after = [...surface.querySelectorAll('mark.find-hit')] + expect(after.length).toBe(before.length) + expect(after.every(el => before.includes(el))).toBe(true) + expect(result.count).toBe(2) + expect(result.activeOrdinal).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 a2dcafb3ac..1b364eefad 100644 --- a/apps/desktop/src/lib/find-in-page-scope.ts +++ b/apps/desktop/src/lib/find-in-page-scope.ts @@ -320,10 +320,17 @@ export function performScopedFind( } const existingMarks = [...root.querySelectorAll(`mark.${HIGHLIGHT_CLASS}`)] + // The marks store the ORIGINAL-CASE source slice (`highlightMatches` + // writes `mark.textContent = matchText`), so byte-equality against the + // typed query fails on the first match whose casing differs — 'Hermes' + // for 'hermes', sentence-initial capitals, ALL-CAPS. Without the + // 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). const sameQuery = options.findNext && existingMarks.length > 0 && - existingMarks.every(mark => mark.textContent === query) + existingMarks.every(mark => mark.textContent.toLowerCase() === query.toLowerCase()) let marks = existingMarks