Skip to content

The agent that grows with you

fix(desktop): case-insensitive fast-path in find-in-page stepping (#81778)

Chen Jin0ea734933653
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 bc0d75e9048..2f99c66bb14 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', '<p>Hermes Hermes</p>')
+ 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', '<p>alpha beta alpha</p>')
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 a2dcafb3ac8..1b364eefad2 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<HTMLElement>(`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