mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-19 22:58:07 +02:00
Fix autolinking with highlighted search results
The current logic assumes that all spans in the text layer contain only one text node, and thus that the position information returned by `highlighter._convertMatches` can be directly used on the element's only child. This is not true in case of highlighted search results: they will be injected in the DOM as `<span>` elements, causing the `<span>`s in the text layer to have more than one child. This patch fixes the problem by properly converting the (span, offset) pair in a (textNode, offset) pair that points to the right text node.
This commit is contained in:
parent
50c573d16d
commit
b47d81536c
2 changed files with 127 additions and 4 deletions
|
@ -13,10 +13,46 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { closePages, createPromise, loadAndWait } from "./test_utils.mjs";
|
||||
import {
|
||||
awaitPromise,
|
||||
closePages,
|
||||
createPromise,
|
||||
loadAndWait,
|
||||
} from "./test_utils.mjs";
|
||||
|
||||
function waitForLinkAnnotations(page) {
|
||||
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) {
|
||||
return createPromise(page, resolve => {
|
||||
if (globalThis.initialLinkAnnotationsEventFired) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
window.PDFViewerApplication.eventBus.on("linkannotationsadded", resolve, {
|
||||
once: true,
|
||||
});
|
||||
|
@ -178,4 +214,49 @@ describe("autolinker", function () {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when highlighting search results", function () {
|
||||
let pages;
|
||||
|
||||
beforeAll(async () => {
|
||||
pages = await loadAndWait(
|
||||
"issue3115r.pdf",
|
||||
".annotationLayer",
|
||||
null,
|
||||
{ eventBusSetup: recordInitialLinkAnnotationsEvent },
|
||||
{ enableAutoLinking: true }
|
||||
);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
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"));
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue