Skip to content

Commit

Permalink
[Annotation] Send correctly the updated values to the JS sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Nov 29, 2022
1 parent ff9d21f commit 476c43c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
const elementData = {
userValue: textContent,
formattedValue: null,
valueOnFocus: "",
lastCommittedValue: "",
};

if (this.data.multiLine) {
Expand Down Expand Up @@ -1125,7 +1125,6 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
if (elementData.userValue) {
event.target.value = elementData.userValue;
}
elementData.valueOnFocus = event.target.value;
});

element.addEventListener("updatefromsandbox", jsEvent => {
Expand Down Expand Up @@ -1207,9 +1206,10 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
return;
}
const { value } = event.target;
if (elementData.valueOnFocus === value) {
if (elementData.lastCommittedValue === value) {
return;
}
elementData.lastCommittedValue = value;
// Save the entered value
elementData.userValue = value;
this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
Expand All @@ -1230,7 +1230,11 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
element.addEventListener("blur", event => {
const { value } = event.target;
elementData.userValue = value;
if (this._mouseState.isDown && elementData.valueOnFocus !== value) {
if (
this._mouseState.isDown &&
elementData.lastCommittedValue !== value
) {
elementData.lastCommittedValue = value;
// Focus out using the mouse: data are committed
this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
source: this,
Expand All @@ -1250,6 +1254,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {

if (this.data.actions?.Keystroke) {
element.addEventListener("beforeinput", event => {
elementData.lastCommittedValue = null;
const { data, target } = event;
const { value, selectionStart, selectionEnd } = target;

Expand Down
53 changes: 53 additions & 0 deletions test/integration/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1598,4 +1598,57 @@ describe("Interaction", () => {
);
});
});

describe("in issue15753.pdf", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("issue15753.pdf", getSelector("27R"));
});

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

it("must check field value is correctly updated when committed with ENTER key", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);

await page.type(getSelector("27R"), "abc", {
delay: 10,
});
await page.keyboard.press("Enter");
await page.waitForFunction(`${getQuerySelector("28R")}.value !== ""`);
let value = await page.$eval(getSelector("28R"), el => el.value);
expect(value).withContext(`In ${browserName}`).toEqual("abc");

await page.type(getSelector("27R"), "def", {
delay: 10,
});

await page.keyboard.press("Enter");
await page.waitForFunction(
`${getQuerySelector("28R")}.value !== "abc"`
);
value = await page.$eval(getSelector("28R"), el => el.value);
expect(value).withContext(`In ${browserName}`).toEqual("abcdef");

await page.keyboard.down("Control");
await page.keyboard.press("A");
await page.keyboard.up("Control");
await page.keyboard.press("Backspace");

await page.keyboard.press("Enter");
await page.waitForFunction(
`${getQuerySelector("28R")}.value !== "abcdef"`
);
value = await page.$eval(getSelector("28R"), el => el.value);
expect(value).withContext(`In ${browserName}`).toEqual("");
})
);
});
});
});
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,4 @@
!issue15690.pdf
!bug1802888.pdf
!issue15759.pdf
!issue15753.pdf
Binary file added test/pdfs/issue15753.pdf
Binary file not shown.

0 comments on commit 476c43c

Please sign in to comment.