[JS] Run the named actions before running the format when the file is open (issue #15818)

It's a follow-up of #14950: some format actions are ran when the document is open
but we must be sure we've everything ready for that, hence we have to run some
named actions before runnig the global format.
In playing with the form, I discovered that the blur event wasn't triggered when
JS called `setFocus` (because in such a case the mouse was never down). So I removed
the mouseState thing to just use the correct commitKey when blur is triggered by a
TAB key.
This commit is contained in:
Calixte Denizet 2022-12-13 16:08:36 +01:00
parent 2d596045d1
commit 2ebf8745a2
11 changed files with 182 additions and 159 deletions

View File

@ -67,7 +67,6 @@ function getRectDims(rect) {
* @property {boolean} [enableScripting]
* @property {boolean} [hasJSActions]
* @property {Object} [fieldObjects]
* @property {Object} [mouseState]
*/
class AnnotationElementFactory {
@ -177,7 +176,6 @@ class AnnotationElement {
this.enableScripting = parameters.enableScripting;
this.hasJSActions = parameters.hasJSActions;
this._fieldObjects = parameters.fieldObjects;
this._mouseState = parameters.mouseState;
if (isRenderable) {
this.container = this._createContainer(ignoreBorder);
@ -1053,6 +1051,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
userValue: textContent,
formattedValue: null,
lastCommittedValue: null,
commitKey: 1,
};
if (this.data.multiLine) {
@ -1114,6 +1113,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
target.value = elementData.userValue;
}
elementData.lastCommittedValue = target.value;
elementData.commitKey = 1;
});
element.addEventListener("updatefromsandbox", jsEvent => {
@ -1178,8 +1178,9 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
// Even if the field hasn't any actions
// leaving it can still trigger some actions with Calculate
element.addEventListener("keydown", event => {
// if the key is one of Escape, Enter or Tab
// then the data are committed
elementData.commitKey = 1;
// If the key is one of Escape, Enter then the data are committed.
// If we've a Tab then data will be committed on blur.
let commitKey = -1;
if (event.key === "Escape") {
commitKey = 0;
@ -1189,7 +1190,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
// (see issue #15627).
commitKey = 2;
} else if (event.key === "Tab") {
commitKey = 3;
elementData.commitKey = 3;
}
if (commitKey === -1) {
return;
@ -1217,13 +1218,12 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
const _blurListener = blurListener;
blurListener = null;
element.addEventListener("blur", event => {
if (!event.relatedTarget) {
return;
}
const { value } = event.target;
elementData.userValue = value;
if (
this._mouseState.isDown &&
elementData.lastCommittedValue !== value
) {
// Focus out using the mouse: data are committed
if (elementData.lastCommittedValue !== value) {
this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
source: this,
detail: {
@ -1231,7 +1231,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
name: "Keystroke",
value,
willCommit: true,
commitKey: 1,
commitKey: elementData.commitKey,
selStart: event.target.selectionStart,
selEnd: event.target.selectionEnd,
},
@ -2620,7 +2620,6 @@ class AnnotationLayer {
enableScripting: parameters.enableScripting,
hasJSActions: parameters.hasJSActions,
fieldObjects: parameters.fieldObjects,
mouseState: parameters.mouseState || { isDown: false },
});
if (element.isRenderable) {
const rendered = element.render();

View File

@ -101,8 +101,7 @@ class Doc extends PDFObject {
this._disableSaving = false;
}
_dispatchDocEvent(name) {
if (name === "Open") {
_initActions() {
const dontRun = new Set([
"WillClose",
"WillSave",
@ -124,6 +123,13 @@ class Doc extends PDFObject {
}
this._runActions("OpenAction");
this._disableSaving = false;
}
_dispatchDocEvent(name) {
if (name === "Open") {
this._disableSaving = true;
this._runActions("OpenAction");
this._disableSaving = false;
} else if (name === "WillPrint") {
this._disablePrinting = true;
this._runActions(name);

View File

@ -91,6 +91,10 @@ class EventDispatcher {
if (id === "doc") {
const eventName = event.name;
if (eventName === "Open") {
// Initialize named actions before calling formatAll to avoid any
// errors in the case where a formatter is using one of those named
// actions (see #15818).
this._document.obj._initActions();
// Before running the Open event, we format all the fields
// (see bug 1766987).
this.formatAll();
@ -264,6 +268,7 @@ class EventDispatcher {
value: "",
formattedValue: null,
selRange: [0, 0],
focus: true, // Stay in the field.
});
}
}

View File

@ -1002,8 +1002,9 @@ describe("Interaction", () => {
});
it("must check input for US zip format", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
// Run the tests sequentially to avoid any focus issues between the two
// browsers when an alert is displayed.
for (const [browserName, page] of pages) {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);
@ -1037,8 +1038,7 @@ describe("Interaction", () => {
text = await page.$eval(getSelector(`29R`), el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("12345");
})
);
}
});
});
@ -1059,8 +1059,9 @@ describe("Interaction", () => {
});
it("must check input for US phone number (long) format", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
// Run the tests sequentially to avoid any focus issues between the two
// browsers when an alert is displayed.
for (const [browserName, page] of pages) {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);
@ -1093,11 +1094,8 @@ describe("Interaction", () => {
await page.click("[data-annotation-id='29R']");
text = await page.$eval(getSelector("30R"), el => el.value);
expect(text)
.withContext(`In ${browserName}`)
.toEqual("(123) 456-7890");
})
);
expect(text).withContext(`In ${browserName}`).toEqual("(123) 456-7890");
}
});
});
@ -1118,8 +1116,9 @@ describe("Interaction", () => {
});
it("must check input for US phone number (short) format", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
// Run the tests sequentially to avoid any focus issues between the two
// browsers when an alert is displayed.
for (const [browserName, page] of pages) {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);
@ -1153,8 +1152,7 @@ describe("Interaction", () => {
text = await page.$eval(getSelector("30R"), el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("123-4567");
})
);
}
});
});
@ -1249,6 +1247,11 @@ describe("Interaction", () => {
"window.PDFViewerApplication.scriptingReady === true"
);
await page.click(getSelector("28R"));
await page.$eval(getSelector("28R"), el =>
el.setSelectionRange(0, 0)
);
await page.type(getSelector("28R"), "Hello", { delay: 100 });
await page.waitForFunction(
`${getQuerySelector("28R")}.value !== "123"`
@ -1701,4 +1704,53 @@ describe("Interaction", () => {
);
});
});
describe("in issue15818.pdf", () => {
let pages;
beforeAll(async () => {
pages = await loadAndWait("issue15818.pdf", getSelector("27R"));
});
afterAll(async () => {
await closePages(pages);
});
it("must check the field value set when the document is open", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);
await page.waitForFunction(`${getQuerySelector("27R")}.value !== ""`);
const text = await page.$eval(getSelector("27R"), el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("hello world");
})
);
});
it("must check the format action is called when setFocus is used", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.waitForFunction(
"window.PDFViewerApplication.scriptingReady === true"
);
await page.type(getSelector("30R"), "abc");
await page.waitForFunction(
`${getQuerySelector("30R")}.value !== "abc"`
);
await page.waitForTimeout(10);
const focusedId = await page.evaluate(_ =>
window.document.activeElement.getAttribute("data-element-id")
);
expect(focusedId).withContext(`In ${browserName}`).toEqual("31R");
})
);
});
});
});

View File

@ -562,3 +562,4 @@
!issue15789.pdf
!fields_order.pdf
!issue15815.pdf
!issue15818.pdf

BIN
test/pdfs/issue15818.pdf Executable file

Binary file not shown.

View File

@ -41,7 +41,6 @@ import { PresentationModeState } from "./ui_utils.js";
* @property {Promise<boolean>} [hasJSActionsPromise]
* @property {Promise<Object<string, Array<Object>> | null>}
* [fieldObjectsPromise]
* @property {Object} [mouseState]
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap]
* @property {TextAccessibilityManager} accessibilityManager
*/
@ -66,7 +65,6 @@ class AnnotationLayerBuilder {
enableScripting = false,
hasJSActionsPromise = null,
fieldObjectsPromise = null,
mouseState = null,
annotationCanvasMap = null,
accessibilityManager = null,
}) {
@ -81,7 +79,6 @@ class AnnotationLayerBuilder {
this.enableScripting = enableScripting;
this._hasJSActionsPromise = hasJSActionsPromise || Promise.resolve(false);
this._fieldObjectsPromise = fieldObjectsPromise || Promise.resolve(null);
this._mouseState = mouseState;
this._annotationCanvasMap = annotationCanvasMap;
this._accessibilityManager = accessibilityManager;
@ -144,7 +141,6 @@ class AnnotationLayerBuilder {
enableScripting: this.enableScripting,
hasJSActions,
fieldObjects,
mouseState: this._mouseState,
annotationCanvasMap: this._annotationCanvasMap,
accessibilityManager: this._accessibilityManager,
});

View File

@ -57,7 +57,6 @@ class DefaultAnnotationLayerFactory {
* @property {IL10n} l10n
* @property {boolean} [enableScripting]
* @property {Promise<boolean>} [hasJSActionsPromise]
* @property {Object} [mouseState]
* @property {Promise<Object<string, Array<Object>> | null>}
* [fieldObjectsPromise]
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap] - Map some
@ -78,7 +77,6 @@ class DefaultAnnotationLayerFactory {
l10n = NullL10n,
enableScripting = false,
hasJSActionsPromise = null,
mouseState = null,
fieldObjectsPromise = null,
annotationCanvasMap = null,
accessibilityManager = null,
@ -94,7 +92,6 @@ class DefaultAnnotationLayerFactory {
enableScripting,
hasJSActionsPromise,
fieldObjectsPromise,
mouseState,
annotationCanvasMap,
accessibilityManager,
});

View File

@ -199,7 +199,6 @@ class IPDFAnnotationLayerFactory {
* @property {IL10n} l10n
* @property {boolean} [enableScripting]
* @property {Promise<boolean>} [hasJSActionsPromise]
* @property {Object} [mouseState]
* @property {Promise<Object<string, Array<Object>> | null>}
* [fieldObjectsPromise]
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap] - Map some
@ -220,7 +219,6 @@ class IPDFAnnotationLayerFactory {
l10n = undefined,
enableScripting = false,
hasJSActionsPromise = null,
mouseState = null,
fieldObjectsPromise = null,
annotationCanvasMap = null,
accessibilityManager = null,

View File

@ -46,7 +46,6 @@ class PDFScriptingManager {
this._destroyCapability = null;
this._scripting = null;
this._mouseState = Object.create(null);
this._ready = false;
this._eventBus = eventBus;
@ -143,19 +142,9 @@ class PDFScriptingManager {
this._closeCapability?.resolve();
});
this._domEvents.set("mousedown", event => {
this._mouseState.isDown = true;
});
this._domEvents.set("mouseup", event => {
this._mouseState.isDown = false;
});
for (const [name, listener] of this._internalEvents) {
this._eventBus._on(name, listener);
}
for (const [name, listener] of this._domEvents) {
window.addEventListener(name, listener, true);
}
try {
const docProperties = await this._getDocProperties();
@ -229,10 +218,6 @@ class PDFScriptingManager {
});
}
get mouseState() {
return this._mouseState;
}
get destroyPromise() {
return this._destroyCapability?.promise || null;
}
@ -248,13 +233,6 @@ class PDFScriptingManager {
return shadow(this, "_internalEvents", new Map());
}
/**
* @private
*/
get _domEvents() {
return shadow(this, "_domEvents", new Map());
}
/**
* @private
*/
@ -508,16 +486,10 @@ class PDFScriptingManager {
}
this._internalEvents.clear();
for (const [name, listener] of this._domEvents) {
window.removeEventListener(name, listener, true);
}
this._domEvents.clear();
this._pageOpenPending.clear();
this._visitedPages.clear();
this._scripting = null;
delete this._mouseState.isDown;
this._ready = false;
this._destroyCapability?.resolve();

View File

@ -1703,7 +1703,6 @@ class PDFViewer {
* @property {IL10n} l10n
* @property {boolean} [enableScripting]
* @property {Promise<boolean>} [hasJSActionsPromise]
* @property {Object} [mouseState]
* @property {Promise<Object<string, Array<Object>> | null>}
* [fieldObjectsPromise]
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap] - Map some
@ -1724,7 +1723,6 @@ class PDFViewer {
l10n = NullL10n,
enableScripting = this.enableScripting,
hasJSActionsPromise = this.pdfDocument?.hasJSActions(),
mouseState = this._scriptingManager?.mouseState,
fieldObjectsPromise = this.pdfDocument?.getFieldObjects(),
annotationCanvasMap = null,
accessibilityManager = null,
@ -1740,7 +1738,6 @@ class PDFViewer {
l10n,
enableScripting,
hasJSActionsPromise,
mouseState,
fieldObjectsPromise,
annotationCanvasMap,
accessibilityManager,