Fix find on web not handling next properly

Fix #138275

Introduced by bbc10afeda

The root problem bbc10afeda 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
This commit is contained in:
Matt Bierner
2021-12-06 16:28:24 -08:00
parent e88394000f
commit 925807b77d

View File

@@ -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++) {