Skip to content

Commit

Permalink
Don't steal the copy event (bug 1857823)
Browse files Browse the repository at this point in the history
There were no specific reasons to stop the propagation of the event
and it can be useful for some third parties to catch the event to let
them do their own stuff.
  • Loading branch information
calixteman committed Oct 9, 2023
1 parent b4cd8ad commit 68f7337
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
49 changes: 49 additions & 0 deletions test/integration/copy_paste_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ describe("Copy and paste", () => {
);
});
});

describe("Copy/paste and ligatures", () => {
let pages;

Expand Down Expand Up @@ -197,4 +198,52 @@ describe("Copy and paste", () => {
);
});
});

describe("Copy event", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait(
"tracemonkey.pdf",
".textLayer .endOfContent",
100,
async page => {
await page.waitForFunction(
() => !!window.PDFViewerApplication.eventBus
);
await waitForTextLayer(page);
}
);
});

afterAll(async () => {
await closePages(pages);
});

it("must check that the copy event is not stolen (bug 1857823)", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.evaluate(async () => {
const promise = new Promise(resolve => {
document.addEventListener("selectionchange", resolve, {
once: true,
});
});
const range = document.createRange();
const span = document.querySelector(".textLayer span");
range.selectNode(span);
const selection = window.getSelection();
selection.addRange(range);
await promise;
});

const promise = waitForEvent(page, "copy", 300000);
await page.keyboard.down("Control");
await page.keyboard.press("c");
await page.keyboard.up("Control");
await promise;
})
);
});
});
});
26 changes: 15 additions & 11 deletions web/text_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,21 @@ class TextLayerBuilder {
end.classList.remove("active");
});

div.addEventListener("copy", event => {
if (!this.#enablePermissions) {
const selection = document.getSelection();
event.clipboardData.setData(
"text/plain",
removeNullCharacters(normalizeUnicode(selection.toString()))
);
}
event.preventDefault();
event.stopPropagation();
});
div.addEventListener(
"copy",
event => {
if (!this.#enablePermissions) {
const selection = document.getSelection();
event.clipboardData.setData(
"text/plain",
removeNullCharacters(normalizeUnicode(selection.toString()))
);
}
},
// Capture the event in order to be sure that the copy event has the
// correct value.
{ capture: true, passive: true }
);
}
}

Expand Down

0 comments on commit 68f7337

Please sign in to comment.