Skip to content

Commit 5a240f7

Browse files
authored
Merge pull request #20932 from calixteman/bug2023150
Trigger the current find after a page has been moved (bug 2023150)
2 parents 5bdeb79 + 1182284 commit 5a240f7

2 files changed

Lines changed: 157 additions & 33 deletions

File tree

test/integration/reorganize_pages_spec.mjs

Lines changed: 109 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import {
1717
awaitPromise,
18-
clearInput,
1918
closePages,
2019
createPromise,
2120
createPromiseWithArgs,
@@ -404,14 +403,6 @@ describe("Reorganize Pages View", () => {
404403
]);
405404

406405
await movePages(page, [11, 2], 3);
407-
await page.waitForFunction(
408-
() => document.querySelectorAll("span.highlight").length === 0
409-
);
410-
411-
await clearInput(page, "#findInput", true);
412-
await page.type("#findInput", "1");
413-
await page.keyboard.press("Enter");
414-
415406
await page.waitForFunction(
416407
() => document.querySelectorAll("span.highlight").length === 10
417408
);
@@ -433,13 +424,6 @@ describe("Reorganize Pages View", () => {
433424
]);
434425

435426
await movePages(page, [13], 0);
436-
await page.waitForFunction(
437-
() => document.querySelectorAll("span.highlight").length === 0
438-
);
439-
440-
await clearInput(page, "#findInput", true);
441-
await page.type("#findInput", "1");
442-
await page.keyboard.press("Enter");
443427

444428
await page.waitForFunction(
445429
() => document.querySelectorAll("span.highlight").length === 10
@@ -463,6 +447,115 @@ describe("Reorganize Pages View", () => {
463447
})
464448
);
465449
});
450+
451+
it("should check if the search is working after copy and paste (bug 2023150)", async () => {
452+
await Promise.all(
453+
pages.map(async ([browserName, page]) => {
454+
await waitForThumbnailVisible(page, 1);
455+
await page.waitForSelector("#viewsManagerStatusActionButton", {
456+
visible: true,
457+
});
458+
459+
await waitAndClick(page, "#viewFindButton");
460+
await waitAndClick(page, ":has(> #findHighlightAll)");
461+
462+
await page.waitForSelector("#findInput", { visible: true });
463+
await page.type("#findInput", "1");
464+
await page.keyboard.press("Enter");
465+
466+
await page.waitForFunction(
467+
() => document.querySelectorAll("span.highlight").length === 10
468+
);
469+
470+
// Select page 1 and copy it.
471+
await waitAndClick(
472+
page,
473+
`.thumbnail:has(${getThumbnailSelector(1)}) input`
474+
);
475+
let handlePagesEdited = await waitForPagesEdited(page, "copy");
476+
await waitAndClick(page, "#viewsManagerStatusActionButton");
477+
await waitAndClick(page, "#viewsManagerStatusActionCopy");
478+
await awaitPromise(handlePagesEdited);
479+
480+
// Paste after page 3.
481+
handlePagesEdited = await waitForPagesEdited(page);
482+
await waitAndClick(page, `${getThumbnailSelector(3)}+button`);
483+
await awaitPromise(handlePagesEdited);
484+
485+
await page.waitForFunction(
486+
() => document.querySelectorAll("span.highlight").length === 11
487+
);
488+
489+
const results = await getSearchResults(page);
490+
expect(results)
491+
.withContext(`In ${browserName}`)
492+
.toEqual([
493+
// Page number, [matches]; copy of page 1 inserted at position 4
494+
[1, ["1"]],
495+
[4, ["1"]],
496+
[11, ["1"]],
497+
[12, ["1", "1"]],
498+
[13, ["1"]],
499+
[14, ["1"]],
500+
[15, ["1"]],
501+
[16, ["1"]],
502+
[17, ["1"]],
503+
[18, ["1"]],
504+
]);
505+
})
506+
);
507+
});
508+
509+
it("should check if the search is working after deleting pages (bug 2023150)", async () => {
510+
await Promise.all(
511+
pages.map(async ([browserName, page]) => {
512+
await waitForThumbnailVisible(page, 1);
513+
await page.waitForSelector("#viewsManagerStatusActionButton", {
514+
visible: true,
515+
});
516+
517+
await waitAndClick(page, "#viewFindButton");
518+
await waitAndClick(page, ":has(> #findHighlightAll)");
519+
520+
await page.waitForSelector("#findInput", { visible: true });
521+
await page.type("#findInput", "1");
522+
await page.keyboard.press("Enter");
523+
524+
await page.waitForFunction(
525+
() => document.querySelectorAll("span.highlight").length === 10
526+
);
527+
528+
// Select page 1 and delete it.
529+
await waitAndClick(
530+
page,
531+
`.thumbnail:has(${getThumbnailSelector(1)}) input`
532+
);
533+
const handlePagesEdited = await waitForPagesEdited(page);
534+
await waitAndClick(page, "#viewsManagerStatusActionButton");
535+
await waitAndClick(page, "#viewsManagerStatusActionDelete");
536+
await awaitPromise(handlePagesEdited);
537+
538+
await page.waitForFunction(
539+
() => document.querySelectorAll("span.highlight").length === 9
540+
);
541+
542+
const results = await getSearchResults(page);
543+
expect(results)
544+
.withContext(`In ${browserName}`)
545+
.toEqual([
546+
// Page number, [matches]; page 1 removed, all positions shifted
547+
[9, ["1"]],
548+
[10, ["1", "1"]],
549+
[11, ["1"]],
550+
[12, ["1"]],
551+
[13, ["1"]],
552+
[14, ["1"]],
553+
[15, ["1"]],
554+
[16, ["1"]],
555+
]);
556+
})
557+
);
558+
});
466559
});
467560

468561
describe("Links and outlines", () => {

web/pdf_find_controller.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,9 @@ class PDFFindController {
422422

423423
#visitedPagesCount = 0;
424424

425-
#copiedExtractTextPromises = null;
425+
#copiedPageData = null;
426426

427-
#savedExtractTextPromises = null;
427+
#savedPageData = null;
428428

429429
/**
430430
* @param {PDFFindControllerOptions} options
@@ -613,7 +613,7 @@ class PDFFindController {
613613
this._dirtyMatch = false;
614614
clearTimeout(this._findTimeout);
615615
this._findTimeout = null;
616-
this.#copiedExtractTextPromises = null;
616+
this.#copiedPageData = null;
617617

618618
this._firstPageCapability = Promise.withResolvers();
619619
}
@@ -1138,51 +1138,82 @@ class PDFFindController {
11381138
}
11391139

11401140
if (type === "copy") {
1141-
this.#copiedExtractTextPromises = new Map();
1141+
const promises = new Map();
1142+
const contents = new Map();
1143+
const diffs = new Map();
1144+
const diacritics = new Map();
11421145
for (const pageNum of pageNumbers) {
1143-
this.#copiedExtractTextPromises.set(
1144-
pageNum,
1145-
this._extractTextPromises[pageNum - 1]
1146-
);
1146+
promises.set(pageNum, this._extractTextPromises[pageNum - 1]);
1147+
contents.set(pageNum, this._pageContents[pageNum - 1]);
1148+
diffs.set(pageNum, this._pageDiffs[pageNum - 1]);
1149+
diacritics.set(pageNum, this._hasDiacritics[pageNum - 1]);
11471150
}
1151+
this.#copiedPageData = { promises, contents, diffs, diacritics };
11481152
return;
11491153
}
11501154

11511155
if (type === "cancelCopy") {
1152-
this.#copiedExtractTextPromises = null;
1156+
this.#copiedPageData = null;
11531157
return;
11541158
}
11551159

11561160
if (type === "delete") {
1157-
this.#savedExtractTextPromises = this._extractTextPromises;
1161+
this.#savedPageData = {
1162+
promises: this._extractTextPromises,
1163+
contents: this._pageContents,
1164+
diffs: this._pageDiffs,
1165+
diacritics: this._hasDiacritics,
1166+
};
11581167
}
11591168

11601169
if (type === "cancelDelete") {
1161-
this._extractTextPromises = this.#savedExtractTextPromises;
1170+
this._extractTextPromises = this.#savedPageData.promises;
1171+
this._pageContents = this.#savedPageData.contents;
1172+
this._pageDiffs = this.#savedPageData.diffs;
1173+
this._hasDiacritics = this.#savedPageData.diacritics;
11621174
return;
11631175
}
11641176

11651177
if (type === "cleanSavedData") {
1166-
this.#savedExtractTextPromises = null;
1178+
this.#savedPageData = null;
11671179
return;
11681180
}
11691181

11701182
this.#onFindBarClose();
11711183
this._dirtyMatch = true;
1172-
const prevTextPromises = this._extractTextPromises;
1184+
const prevPromises = this._extractTextPromises;
1185+
const prevContents = this._pageContents;
1186+
const prevDiffs = this._pageDiffs;
1187+
const prevDiacritics = this._hasDiacritics;
11731188
const extractTextPromises = (this._extractTextPromises = []);
1174-
for (let i = 1, ii = pagesMapper.length; i <= ii; i++) {
1189+
const pageContents = (this._pageContents = []);
1190+
const pageDiffs = (this._pageDiffs = []);
1191+
const hasDiacritics = (this._hasDiacritics = []);
1192+
for (let i = 1, ii = pagesMapper.pagesNumber; i <= ii; i++) {
11751193
const prevPageNumber = pagesMapper.getPrevPageNumber(i);
11761194
if (prevPageNumber < 0) {
1195+
const src = -prevPageNumber;
11771196
extractTextPromises.push(
1178-
this.#copiedExtractTextPromises?.get(-prevPageNumber) ||
1179-
Promise.resolve()
1197+
this.#copiedPageData?.promises.get(src) || Promise.resolve()
11801198
);
1199+
pageContents.push(this.#copiedPageData?.contents.get(src) ?? "");
1200+
pageDiffs.push(this.#copiedPageData?.diffs.get(src) ?? null);
1201+
hasDiacritics.push(this.#copiedPageData?.diacritics.get(src) ?? false);
11811202
continue;
11821203
}
11831204
extractTextPromises.push(
1184-
prevTextPromises[prevPageNumber - 1] || Promise.resolve()
1205+
prevPromises[prevPageNumber - 1] || Promise.resolve()
11851206
);
1207+
pageContents.push(prevContents[prevPageNumber - 1] ?? "");
1208+
pageDiffs.push(prevDiffs[prevPageNumber - 1] ?? null);
1209+
hasDiacritics.push(prevDiacritics[prevPageNumber - 1] ?? false);
1210+
}
1211+
if (this.#state) {
1212+
this._eventBus.dispatch("find", {
1213+
source: this,
1214+
type: "",
1215+
...this.#state,
1216+
});
11861217
}
11871218
}
11881219

0 commit comments

Comments
 (0)