Skip to content

Commit

Permalink
JS -- Fix events dispatchment and add tests
Browse files Browse the repository at this point in the history
 * dispatch event to take into account calculation order
 * use a map for actions in Field
  • Loading branch information
calixteman committed Nov 6, 2020
1 parent 018fd43 commit 6498357
Show file tree
Hide file tree
Showing 8 changed files with 454 additions and 94 deletions.
65 changes: 58 additions & 7 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
element.setAttribute("value", textContent);
}

element.userValue = textContent;
element.setAttribute("id", id);

element.addEventListener("input", function (event) {
Expand All @@ -507,26 +508,76 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
event.target.setSelectionRange(0, 0);
});

element.addEventListener("focus", event => {
if (event.target.userValue) {
event.target.value = event.target.userValue;
}
});

if (this.data.actions) {
element.addEventListener("updateFromSandbox", function (event) {
const data = event.detail;
if ("value" in data) {
event.target.value = event.detail.value;
} else if ("focus" in data) {
event.target.focus({ preventScroll: false });
const detail = event.detail;
const actions = {
value() {
const value = detail.value;
if (value === undefined || value === null) {
// remove data
event.target.userValue = "";
} else {
event.target.userValue = value;
}
},
valueAsString() {
const value = detail.valueAsString;
if (value === undefined || value === null) {
// remove data
event.target.value = "";
} else {
event.target.value = value;
}
storage.setValue(id, event.target.value);
},
focus() {
event.target.focus({ preventScroll: false });
},
userName() {
const tooltip = detail.userName;
event.target.title = tooltip;
},
hidden() {
event.target.style.display = detail.hidden ? "none" : "block";
},
editable() {
event.target.disabled = !detail.editable;
},
selRange() {
const [selStart, selEnd] = detail.selRange;
if (selStart >= 0 && selEnd < event.target.value.length) {
event.target.setSelectionRange(selStart, selEnd);
}
},
};
for (const name of Object.keys(detail)) {
if (name in actions) {
actions[name]();
}
}
});

for (const eventType of Object.keys(this.data.actions)) {
switch (eventType) {
case "Format":
element.addEventListener("blur", function (event) {
element.addEventListener("change", function (event) {
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id,
name: "Format",
name: "Keystroke",
value: event.target.value,
willCommit: true,
commitKey: 1,
selStart: event.target.selectionStart,
selEnd: event.target.selectionEnd,
},
})
);
Expand Down
1 change: 1 addition & 0 deletions src/scripting_api/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PDFObject } from "./pdf_object.js";
class Doc extends PDFObject {
constructor(data) {
super(data);
this.calculate = true;

this._printParams = null;
this._fields = Object.create(null);
Expand Down
127 changes: 117 additions & 10 deletions src/scripting_api/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class Event {
this.richChange = data.richChange || [];
this.richChangeEx = data.richChangeEx || [];
this.richValue = data.richValue || [];
this.selEnd = data.selEnd || 0;
this.selStart = data.selStart || 0;
this.selEnd = data.selEnd || -1;
this.selStart = data.selStart || -1;
this.shift = data.shift || false;
this.source = data.source || null;
this.target = data.target || null;
this.targetName = data.targetName || "";
this.targetName = "";
this.type = "Field";
this.value = data.value || null;
this.value = data.value || "";
this.willCommit = data.willCommit || false;
}
}
Expand All @@ -47,6 +47,21 @@ class EventDispatcher {
this._document.obj._eventDispatcher = this;
}

mergeChange(event) {
let value = event.value;
if (typeof value !== "string") {
value = value.toString();
}
const prefix =
event.selStart >= 0 ? value.substring(0, event.selStart) : "";
const postfix =
event.selEnd >= 0 && event.selEnd <= value.length
? value.substring(event.selEnd)
: "";

return `${prefix}${event.change}${postfix}`;
}

dispatch(baseEvent) {
const id = baseEvent.id;
if (!(id in this._objects)) {
Expand All @@ -56,23 +71,115 @@ class EventDispatcher {
const name = baseEvent.name.replace(" ", "");
const source = this._objects[id];
const event = (this._document.obj._event = new Event(baseEvent));
const oldValue = source.obj.value;
let savedChange;

if (source.obj._isButton()) {
source.obj._id = id;
event.value = source.obj._getExportValue(event.value);
}

if (name === "Keystroke") {
savedChange = {
value: event.value,
change: event.change,
selStart: event.selStart,
selEnd: event.selEnd,
};
} else if (name === "Blur" || name === "Focus") {
Object.defineProperty(event, "value", {
configurable: false,
writable: false,
enumerable: true,
value: event.value,
});
} else if (name === "Validate") {
this.runValidation(source, event);
return;
}

this.runActions(source, source, event, name);
if (event.rc && oldValue !== event.value) {
source.wrapped.value = event.value;

if (name === "Keystroke") {
if (event.rc) {
if (event.willCommit) {
this.runValidation(source, event);
} else if (
event.change !== savedChange.change ||
event.selStart !== savedChange.selStart ||
event.selEnd !== savedChange.selEnd
) {
source.wrapped.value = this.mergeChange(event);
}
} else if (!event.willCommit) {
source.obj._send({
id: source.obj._id,
value: savedChange.value,
selRange: [savedChange.selStart, savedChange.selEnd],
});
}
}
}

runValidation(source, event) {
const hasRan = this.runActions(source, source, event, "Validate");
if (event.rc) {
if (hasRan) {
source.wrapped.value = event.value;
} else {
source.obj.value = event.value;
}

if (this._document.obj.calculate) {
this.runCalculate(source, event);
}

event.value = source.obj.value;
this.runActions(source, source, event, "Format");
source.wrapped.valueAsString = event.value;
}
}

runActions(source, target, event, eventName) {
event.source = source.wrapped;
event.target = target.wrapped;
event.name = eventName;
event.targetName = target.obj.name;
event.rc = true;
if (!target.obj._runActions(event)) {
return true;

return target.obj._runActions(event);
}

calculateNow() {
if (this._calculationOrder.length === 0) {
return;
}
const first = this._calculationOrder[0];
const source = this._objects[first];
const event = (this._document.obj._event = new Event({}));
this.runCalculate(source, event);
}

runCalculate(source, event) {
if (this._calculationOrder.length === 0) {
return;
}

for (const targetId of this._calculationOrder) {
if (!(targetId in this._objects)) {
continue;
}

const target = this._objects[targetId];
this.runActions(source, target, event, "Calculate");
this.runActions(target, target, event, "Validate");
if (!event.rc) {
continue;
}

target.wrapped.value = event.value;
this.runActions(target, target, event, "Format");
target.wrapped.valueAsString = event.value;
}
return event.rc;
}
}

Expand Down
48 changes: 33 additions & 15 deletions src/scripting_api/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,8 @@ class Field extends PDFObject {
this.valueAsString = data.valueAsString;

// Private
this._actions = Object.create(null);
const doc = (this._document = data.doc);
for (const [eventType, actions] of Object.entries(data.actions)) {
// This code is running in a sandbox so it's safe to use Function
this._actions[eventType] = actions.map(action =>
// eslint-disable-next-line no-new-func
Function("event", `with (this) {${action}}`).bind(doc)
);
}
this._document = data.doc;
this._actions = this._createActionsMap(data.actions);
}

setAction(cTrigger, cScript) {
Expand All @@ -89,31 +82,56 @@ class Field extends PDFObject {
if (!(cTrigger in this._actions)) {
this._actions[cTrigger] = [];
}
this._actions[cTrigger].push(cScript);
this._actions[cTrigger].push(
// eslint-disable-next-line no-new-func
Function("event", `with (this) {${cScript}}`).bind(this._document)
);
}

setFocus() {
this._send({ id: this._id, focus: true });
}

_createActionsMap(actions) {
const actionsMap = new Map();
if (actions) {
const doc = this._document;
for (const [eventType, actionsForEvent] of Object.entries(actions)) {
// This stuff is running in a sandbox so it's safe to use Function
actionsMap.set(
eventType,
actionsForEvent.map(action =>
// eslint-disable-next-line no-new-func
Function("event", `with (this) {${action}}`).bind(doc)
)
);
}
}
return actionsMap;
}

_isButton() {
return false;
}

_runActions(event) {
const eventName = event.name;
if (!(eventName in this._actions)) {
if (!this._actions.has(eventName)) {
return false;
}

const actions = this._actions[eventName];
const actions = this._actions.get(eventName);
try {
for (const action of actions) {
action(event);
}
} catch (error) {
event.rc = false;
const value =
`"${error.toString()}" for event ` +
`"${eventName}" in object ${this._id}.` +
`\"${error.toString()}\" for event ` +
`\"${eventName}\" in object ${this._id}.` +
`\n${error.stack}`;
this._send({ command: "error", value });
this._send({ id: "error", value });
}

return true;
Expand Down
10 changes: 9 additions & 1 deletion src/scripting_api/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Field } from "./field.js";
import { ProxyHandler } from "./proxy.js";
import { Util } from "./util.js";

function initSandbox(data, extra, out) {
function initSandbox({ data, extra, out, testMode = false }) {
const proxyHandler = new ProxyHandler(data.dispatchEventName);
const { send, crackURL } = extra;
const doc = new Doc({ send });
Expand Down Expand Up @@ -52,6 +52,14 @@ function initSandbox(data, extra, out) {
out[name] = aform[name].bind(aform);
}
}

if (
(typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")) &&
testMode
) {
out._app = app;
}
}

export { initSandbox };
Loading

0 comments on commit 6498357

Please sign in to comment.