From 925807b77db7dc215738f9df2ce4e4e1ede85217 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 6 Dec 2021 16:28:24 -0800 Subject: [PATCH] Fix find on web not handling next properly Fix #138275 Introduced by bbc10afedac1f9ea35d510211a5ec59296573cd5 The root problem bbc10afedac1f9ea35d510211a5ec59296573cd5 fixed is that `window.find` starts after the current selection. If your document consists of `abc abcd` for example, the following happens: 1. Type `a` in the find box 1. The first `a` is correctly highlighted 1. Type `b` to refine the search 1. The search no longer sees the first `a`, so we skip ahead to the second `a`, which is unexpected To fix this, I added code to collapse the selection before continuing the search. However we were also collapsing the selection when you hit the next button, which just resulted in the same word being re-selected again instead of skipping ahead to the next word. This fix avoids collapsing the selection when you are simply pressing next --- .../workbench/contrib/webview/browser/pre/main.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/vs/workbench/contrib/webview/browser/pre/main.js b/src/vs/workbench/contrib/webview/browser/pre/main.js index 16cc137a10b..ed2e896233c 100644 --- a/src/vs/workbench/contrib/webview/browser/pre/main.js +++ b/src/vs/workbench/contrib/webview/browser/pre/main.js @@ -989,15 +989,21 @@ onDomReady(() => { assertIsDefined(target.contentDocument).execCommand(data); }); + /** @type {string | undefined} */ + let lastFindValue = undefined; + hostMessaging.onMessage('find', (_event, data) => { const target = getActiveFrame(); if (!target) { return; } - // Reset selection so we start search after current find result - const selection = target.contentWindow.getSelection(); - selection.collapse(selection.anchorNode); + if (!data.previous && lastFindValue !== data.value) { + // Reset selection so we start search at the head of the last search + const selection = target.contentWindow.getSelection(); + selection.collapse(selection.anchorNode); + } + lastFindValue = data.value; const didFind = (/** @type {any} */ (target.contentWindow)).find( data.value, @@ -1016,6 +1022,8 @@ onDomReady(() => { return; } + lastFindValue = undefined; + if (!data.clearSelection) { const selection = target.contentWindow.getSelection(); for (let i = 0; i < selection.rangeCount; i++) {