1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 06:38:07 +02:00

Fix autolinking error due to redundant annotations on zooming

Fix an issue where redundant links were being added to the annotation
layer on zooming on the page with the links.
This commit is contained in:
Ujjwal Sharma 2025-02-12 01:30:38 +01:00
parent 70e2873430
commit 38ab358fb1
2 changed files with 62 additions and 2 deletions

View file

@ -13,7 +13,15 @@
* limitations under the License.
*/
import { closePages, loadAndWait } from "./test_utils.mjs";
import { closePages, createPromise, loadAndWait } from "./test_utils.mjs";
function waitForLinkAnnotations(page) {
return createPromise(page, resolve => {
window.PDFViewerApplication.eventBus.on("linkannotationsadded", resolve, {
once: true,
});
});
}
describe("autolinker", function () {
describe("bug1019475_2.pdf", function () {
@ -38,6 +46,7 @@ describe("autolinker", function () {
it("must appropriately add link annotations when relevant", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await waitForLinkAnnotations(page);
const url = await page.$$eval(
".annotationLayer > .linkAnnotation > a",
annotations => annotations.map(a => a.href)
@ -73,6 +82,7 @@ describe("autolinker", function () {
it("must not add links when unnecessary", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await waitForLinkAnnotations(page);
const linkIds = await page.$$eval(
".annotationLayer > .linkAnnotation > a",
annotations =>
@ -106,6 +116,7 @@ describe("autolinker", function () {
it("must not add links that overlap even if the URLs are different", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await waitForLinkAnnotations(page);
const linkIds = await page.$$eval(
".annotationLayer > .linkAnnotation > a",
annotations =>
@ -121,4 +132,50 @@ describe("autolinker", function () {
);
});
});
describe("PR 19470", function () {
let pages;
beforeAll(async () => {
pages = await loadAndWait(
"bug1019475_2.pdf",
".annotationLayer",
null,
null,
{
enableAutoLinking: true,
}
);
});
afterAll(async () => {
await closePages(pages);
});
it("must not repeatedly add link annotations redundantly", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await waitForLinkAnnotations(page);
let url = await page.$$eval(
".annotationLayer > .linkAnnotation > a",
annotations => annotations.map(a => a.href)
);
expect(url.length).withContext(`In ${browserName}`).toEqual(1);
await page.evaluate(() =>
window.PDFViewerApplication.pdfViewer.updateScale({
drawingDelay: -1,
scaleFactor: 2,
})
);
await waitForLinkAnnotations(page);
url = await page.$$eval(
".annotationLayer > .linkAnnotation > a",
annotations => annotations.map(a => a.href)
);
expect(url.length).withContext(`In ${browserName}`).toEqual(1);
})
);
});
});
});

View file

@ -77,6 +77,8 @@ class AnnotationLayerBuilder {
#eventAbortController = null;
#linksInjected = false;
/**
* @param {AnnotationLayerBuilderOptions} options
*/
@ -235,9 +237,10 @@ class AnnotationLayerBuilder {
"`render` method must be called before `injectLinkAnnotations`."
);
}
if (this._cancelled) {
if (this._cancelled || this.#linksInjected) {
return;
}
this.#linksInjected = true;
const newLinks = this.#annotations.length
? this.#checkInferredLinks(inferredLinks)