2024-11-12 11:26:32 +05:30
|
|
|
/* Copyright 2025 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2025-02-28 14:20:31 +01:00
|
|
|
import {
|
|
|
|
awaitPromise,
|
|
|
|
closePages,
|
|
|
|
createPromise,
|
|
|
|
loadAndWait,
|
|
|
|
} from "./test_utils.mjs";
|
2025-02-12 01:30:38 +01:00
|
|
|
|
2025-02-28 14:20:31 +01:00
|
|
|
function waitForLinkAnnotations(page, pageNumber) {
|
|
|
|
return page.evaluateHandle(
|
|
|
|
number => [
|
|
|
|
new Promise(resolve => {
|
|
|
|
const { eventBus } = window.PDFViewerApplication;
|
|
|
|
eventBus.on("linkannotationsadded", function listener(e) {
|
|
|
|
if (number === undefined || e.pageNumber === number) {
|
|
|
|
resolve();
|
|
|
|
eventBus.off("linkannotationsadded", listener);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
pageNumber
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function recordInitialLinkAnnotationsEvent(eventBus) {
|
|
|
|
globalThis.initialLinkAnnotationsEventFired = false;
|
|
|
|
eventBus.on(
|
|
|
|
"linkannotationsadded",
|
|
|
|
() => {
|
|
|
|
globalThis.initialLinkAnnotationsEventFired = true;
|
|
|
|
},
|
|
|
|
{ once: true }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
function waitForInitialLinkAnnotations(page) {
|
2025-02-12 01:30:38 +01:00
|
|
|
return createPromise(page, resolve => {
|
2025-02-28 14:20:31 +01:00
|
|
|
if (globalThis.initialLinkAnnotationsEventFired) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
2025-02-12 01:30:38 +01:00
|
|
|
window.PDFViewerApplication.eventBus.on("linkannotationsadded", resolve, {
|
|
|
|
once: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2024-11-12 11:26:32 +05:30
|
|
|
|
|
|
|
describe("autolinker", function () {
|
|
|
|
describe("bug1019475_2.pdf", function () {
|
|
|
|
let pages;
|
|
|
|
|
2025-04-13 14:13:12 +02:00
|
|
|
beforeEach(async () => {
|
2024-11-12 11:26:32 +05:30
|
|
|
pages = await loadAndWait(
|
|
|
|
"bug1019475_2.pdf",
|
|
|
|
".annotationLayer",
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
enableAutoLinking: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2025-04-13 14:13:12 +02:00
|
|
|
afterEach(async () => {
|
2024-11-12 11:26:32 +05:30
|
|
|
await closePages(pages);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("must appropriately add link annotations when relevant", async () => {
|
|
|
|
await Promise.all(
|
|
|
|
pages.map(async ([browserName, page]) => {
|
2025-02-12 01:30:38 +01:00
|
|
|
await waitForLinkAnnotations(page);
|
2024-11-12 11:26:32 +05:30
|
|
|
const url = await page.$$eval(
|
|
|
|
".annotationLayer > .linkAnnotation > a",
|
|
|
|
annotations => annotations.map(a => a.href)
|
|
|
|
);
|
|
|
|
expect(url.length).withContext(`In ${browserName}`).toEqual(1);
|
|
|
|
expect(url[0])
|
|
|
|
.withContext(`In ${browserName}`)
|
|
|
|
.toEqual("http://www.mozilla.org/");
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("bug1019475_1.pdf", function () {
|
|
|
|
let pages;
|
|
|
|
|
2025-04-13 14:13:12 +02:00
|
|
|
beforeEach(async () => {
|
2024-11-12 11:26:32 +05:30
|
|
|
pages = await loadAndWait(
|
|
|
|
"bug1019475_1.pdf",
|
|
|
|
".annotationLayer",
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
enableAutoLinking: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2025-04-13 14:13:12 +02:00
|
|
|
afterEach(async () => {
|
2024-11-12 11:26:32 +05:30
|
|
|
await closePages(pages);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("must not add links when unnecessary", async () => {
|
|
|
|
await Promise.all(
|
|
|
|
pages.map(async ([browserName, page]) => {
|
2025-02-12 01:30:38 +01:00
|
|
|
await waitForLinkAnnotations(page);
|
2024-11-12 11:26:32 +05:30
|
|
|
const linkIds = await page.$$eval(
|
|
|
|
".annotationLayer > .linkAnnotation > a",
|
|
|
|
annotations =>
|
|
|
|
annotations.map(a => a.getAttribute("data-element-id"))
|
|
|
|
);
|
|
|
|
expect(linkIds.length).withContext(`In ${browserName}`).toEqual(3);
|
|
|
|
linkIds.forEach(id =>
|
|
|
|
expect(id)
|
|
|
|
.withContext(`In ${browserName}`)
|
2025-02-08 15:38:22 +01:00
|
|
|
.not.toContain("inferred_link_")
|
2024-11-12 11:26:32 +05:30
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2025-02-08 17:07:03 +01:00
|
|
|
|
|
|
|
describe("pr19449.pdf", function () {
|
|
|
|
let pages;
|
|
|
|
|
2025-04-13 14:13:12 +02:00
|
|
|
beforeEach(async () => {
|
2025-02-08 17:07:03 +01:00
|
|
|
pages = await loadAndWait("pr19449.pdf", ".annotationLayer", null, null, {
|
|
|
|
docBaseUrl: "http://example.com",
|
|
|
|
enableAutoLinking: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2025-04-13 14:13:12 +02:00
|
|
|
afterEach(async () => {
|
2025-02-08 17:07:03 +01:00
|
|
|
await closePages(pages);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("must not add links that overlap even if the URLs are different", async () => {
|
|
|
|
await Promise.all(
|
|
|
|
pages.map(async ([browserName, page]) => {
|
2025-02-12 01:30:38 +01:00
|
|
|
await waitForLinkAnnotations(page);
|
2025-02-08 17:07:03 +01:00
|
|
|
const linkIds = await page.$$eval(
|
|
|
|
".annotationLayer > .linkAnnotation > a",
|
|
|
|
annotations =>
|
|
|
|
annotations.map(a => a.getAttribute("data-element-id"))
|
|
|
|
);
|
|
|
|
expect(linkIds.length).withContext(`In ${browserName}`).toEqual(1);
|
|
|
|
linkIds.forEach(id =>
|
|
|
|
expect(id)
|
|
|
|
.withContext(`In ${browserName}`)
|
|
|
|
.not.toContain("inferred_link_")
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2025-02-12 01:30:38 +01:00
|
|
|
|
|
|
|
describe("PR 19470", function () {
|
|
|
|
let pages;
|
|
|
|
|
2025-04-13 14:13:12 +02:00
|
|
|
beforeEach(async () => {
|
2025-02-12 01:30:38 +01:00
|
|
|
pages = await loadAndWait(
|
|
|
|
"bug1019475_2.pdf",
|
|
|
|
".annotationLayer",
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
enableAutoLinking: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2025-04-13 14:13:12 +02:00
|
|
|
afterEach(async () => {
|
2025-02-12 01:30:38 +01:00
|
|
|
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);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2025-02-28 14:20:31 +01:00
|
|
|
|
|
|
|
describe("when highlighting search results", function () {
|
|
|
|
let pages;
|
|
|
|
|
2025-04-13 14:13:12 +02:00
|
|
|
beforeEach(async () => {
|
2025-02-28 14:20:31 +01:00
|
|
|
pages = await loadAndWait(
|
|
|
|
"issue3115r.pdf",
|
|
|
|
".annotationLayer",
|
|
|
|
null,
|
|
|
|
{ eventBusSetup: recordInitialLinkAnnotationsEvent },
|
|
|
|
{ enableAutoLinking: true }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2025-04-13 14:13:12 +02:00
|
|
|
afterEach(async () => {
|
2025-02-28 14:20:31 +01:00
|
|
|
await closePages(pages);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("must find links that overlap with search results", async () => {
|
|
|
|
await Promise.all(
|
|
|
|
pages.map(async ([browserName, page]) => {
|
|
|
|
await awaitPromise(await waitForInitialLinkAnnotations(page));
|
|
|
|
|
|
|
|
const linkAnnotationsPromise = await waitForLinkAnnotations(page, 36);
|
|
|
|
|
|
|
|
// Search for "rich.edu"
|
|
|
|
await page.click("#viewFindButton");
|
|
|
|
await page.waitForSelector("#viewFindButton", { hidden: false });
|
|
|
|
await page.type("#findInput", "rich.edu");
|
|
|
|
await page.waitForSelector(".textLayer .highlight");
|
|
|
|
|
|
|
|
await awaitPromise(linkAnnotationsPromise);
|
|
|
|
|
|
|
|
const urls = await page.$$eval(
|
|
|
|
".page[data-page-number='36'] > .annotationLayer > .linkAnnotation > a",
|
|
|
|
annotations => annotations.map(a => a.href)
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(urls)
|
|
|
|
.withContext(`In ${browserName}`)
|
|
|
|
.toContain(jasmine.stringContaining("rich.edu"));
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2024-11-12 11:26:32 +05:30
|
|
|
});
|