Skip to content

Commit

Permalink
Merge pull request mozilla#17882 from calixteman/bug1889122
Browse files Browse the repository at this point in the history
Use the string value of the field when calling the Format callback (bug 1889122)
  • Loading branch information
calixteman authored Apr 10, 2024
2 parents 2c7ae50 + b643c0f commit 77ee914
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/scripting_api/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ class EventDispatcher {
}

dispatch(baseEvent) {
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("TESTING") &&
baseEvent.name === "sandboxtripbegin"
) {
this._externalCall("send", [{ command: "sandboxTripEnd" }]);
return;
}

const id = baseEvent.id;
if (!(id in this._objects)) {
let event;
Expand Down Expand Up @@ -233,7 +242,7 @@ class EventDispatcher {
// Run format actions if any for all the fields.
const event = (globalThis.event = new Event({}));
for (const source of Object.values(this._objects)) {
event.value = source.obj.value;
event.value = source.obj._getValue();
this.runActions(source, source, event, "Format");
}
}
Expand All @@ -245,8 +254,7 @@ class EventDispatcher {

this.runCalculate(source, event);

const savedValue = source.obj._getValue();
event.value = source.obj.value;
const savedValue = (event.value = source.obj._getValue());
let formattedValue = null;

if (this.runActions(source, source, event, "Format")) {
Expand Down
38 changes: 38 additions & 0 deletions test/integration/scripting_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
loadAndWait,
scrollIntoView,
waitForEntryInStorage,
waitForSandboxTrip,
waitForTimeout,
} from "./test_utils.mjs";

Expand Down Expand Up @@ -2367,4 +2368,41 @@ describe("Interaction", () => {
);
});
});

describe("Textfield with a zip code starting with 0", () => {
let pages;
let otherPages;

beforeAll(async () => {
otherPages = await Promise.all(
global.integrationSessions.map(async session =>
session.browser.newPage()
)
);
pages = await loadAndWait("bug1889122.pdf", getSelector("24R"));
});

afterAll(async () => {
await closePages(pages);
await Promise.all(otherPages.map(page => page.close()));
});

it("must check the zip code is correctly formatted", async () => {
await Promise.all(
pages.map(async ([browserName, page], i) => {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);

await page.click(getSelector("24R"));
await page.type(getSelector("24R"), "01234", { delay: 10 });
await page.keyboard.press("Tab");
await waitForSandboxTrip(page);

const text = await page.$eval(getSelector("24R"), el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("01234");
})
);
});
});
});
11 changes: 11 additions & 0 deletions test/integration/test_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ function closePages(pages) {
);
}

async function waitForSandboxTrip(page) {
const handle = await page.evaluateHandle(() => [
new Promise(resolve => {
window.addEventListener("sandboxtripend", resolve, { once: true });
window.PDFViewerApplication.pdfScriptingManager.sandboxTrip();
}),
]);
await awaitPromise(handle);
}

function waitForTimeout(milliseconds) {
/**
* Wait for the given number of milliseconds.
Expand Down Expand Up @@ -583,6 +593,7 @@ export {
waitForAnnotationEditorLayer,
waitForEntryInStorage,
waitForEvent,
waitForSandboxTrip,
waitForSelectedEditor,
waitForSerialized,
waitForStorageEntries,
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,4 @@
!issue17808.pdf
!issue17871_bottom_right.pdf
!issue17871_top_right.pdf
!bug1889122.pdf
Binary file added test/pdfs/bug1889122.pdf
Binary file not shown.
24 changes: 24 additions & 0 deletions web/pdf_scripting_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ class PDFScriptingManager {
this.#eventBus = eventBus;
this.#externalServices = externalServices;
this.#docProperties = docProperties;

if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) {
Object.defineProperty(this, "sandboxTrip", {
value: () =>
setTimeout(
() =>
this.#scripting?.dispatchEventInSandbox({
name: "sandboxtripbegin",
}),
0
),
});
}
}

setViewer(pdfViewer) {
Expand Down Expand Up @@ -258,6 +271,17 @@ class PDFScriptingManager {

const { id, siblings, command, value } = detail;
if (!id) {
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("TESTING") &&
command === "sandboxTripEnd"
) {
window.setTimeout(() => {
window.dispatchEvent(new CustomEvent("sandboxtripend"));
}, 0);
return;
}

switch (command) {
case "clear":
console.clear();
Expand Down

0 comments on commit 77ee914

Please sign in to comment.