Skip to content

Commit

Permalink
Add the possibility to collect Javascript actions
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Oct 1, 2020
1 parent d49b2f6 commit f2286d8
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 1 deletion.
145 changes: 144 additions & 1 deletion src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
/* eslint no-var: error */

import {
AnnotationActionEventType,
AnnotationBorderStyleType,
AnnotationFieldFlag,
AnnotationFlag,
AnnotationReplyType,
AnnotationType,
assert,
bytesToString,
escapeString,
getModificationDate,
isString,
Expand All @@ -31,7 +33,15 @@ import {
warn,
} from "../shared/util.js";
import { Catalog, FileSpec, ObjectLoader } from "./obj.js";
import { Dict, isDict, isName, isRef, isStream, Name } from "./primitives.js";
import {
Dict,
isDict,
isName,
isRef,
isStream,
Name,
RefSet,
} from "./primitives.js";
import { ColorSpace } from "./colorspace.js";
import { getInheritableProperty } from "./core_utils.js";
import { OperatorList } from "./operator_list.js";
Expand Down Expand Up @@ -570,6 +580,10 @@ class Annotation {
return null;
}

getAnnotationObject() {
return null;
}

/**
* Reset the annotation.
*
Expand Down Expand Up @@ -904,6 +918,7 @@ class WidgetAnnotation extends Annotation {

data.annotationType = AnnotationType.WIDGET;
data.fieldName = this._constructFieldName(dict);
data.actions = this._collectActions(params.xref, dict);

const fieldValue = getInheritableProperty({
dict,
Expand Down Expand Up @@ -938,13 +953,15 @@ class WidgetAnnotation extends Annotation {
}

data.readOnly = this.hasFieldFlag(AnnotationFieldFlag.READONLY);
data.hidden = this.hasFieldFlag(AnnotationFieldFlag.HIDDEN);

// Hide signatures because we cannot validate them, and unset the fieldValue
// since it's (most likely) a `Dict` which is non-serializable and will thus
// cause errors when sending annotations to the main-thread (issue 10347).
if (data.fieldType === "Sig") {
data.fieldValue = null;
this.setFlags(AnnotationFlag.HIDDEN);
data.hidden = true;
}
}

Expand Down Expand Up @@ -1367,6 +1384,76 @@ class WidgetAnnotation extends Annotation {
}
return localResources || Dict.empty;
}

_collectJS(entry, xref, list, parents) {
if (!entry) {
return;
}

let parent = null;
if (isRef(entry)) {
if (parents.has(entry)) {
// If we've already found entry then we've a cycle.
return;
}
parent = entry;
parents.put(parent);
entry = xref.fetch(entry);
}
if (Array.isArray(entry)) {
for (const element of entry) {
this._collectJS(element, xref, list, parents);
}
} else if (entry instanceof Dict) {
if (isName(entry.get("S"), "JavaScript") && entry.has("JS")) {
const js = entry.get("JS");
let code;
if (isStream(js)) {
code = bytesToString(js.getBytes());
} else {
code = stringToPDFString(js);
}
if (code) {
list.push(code);
}
}
this._collectJS(entry.getRaw("Next"), xref, list, parents);
}

if (parent) {
parents.remove(parent);
}
}

_collectActions(xref, dict) {
const actions = Object.create({});
if (dict.has("AA")) {
const additionalAction = dict.get("AA");
for (const key of additionalAction.getKeys()) {
if (key in AnnotationActionEventType) {
const actionDict = additionalAction.getRaw(key);
const parents = new RefSet();
const list = [];
this._collectJS(actionDict, xref, list, parents);
if (list.length > 0) {
actions[AnnotationActionEventType[key]] = list;
}
}
}
}
return actions;
}

getAnnotationObject() {
if (this.data.fieldType === "Sig") {
return {
id: this.data.id,
value: null,
type: "signature",
};
}
return null;
}
}

class TextWidgetAnnotation extends WidgetAnnotation {
Expand Down Expand Up @@ -1517,6 +1604,23 @@ class TextWidgetAnnotation extends WidgetAnnotation {

return chunks;
}

getAnnotationObject() {
return {
id: this.data.id,
value: this.data.fieldValue,
multiline: this.data.multiline,
password: this.hasFieldFlag(AnnotationFieldFlag.PASSWORD),
charLimit: this.data.maxLen,
comb: this.data.comb,
editable: !this.data.readOnly,
hidden: this.data.hidden,
name: this.data.fieldName,
rect: this.data.rect,
actions: this.data.actions,
type: "text",
};
}
}

class ButtonWidgetAnnotation extends WidgetAnnotation {
Expand Down Expand Up @@ -1791,6 +1895,28 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
docBaseUrl: params.pdfManager.docBaseUrl,
});
}

getAnnotationObject() {
let type = "button";
let value = null;
if (this.data.checkBox) {
type = "checkbox";
value = this.data.fieldValue && this.data.fieldValue !== "Off";
} else if (this.data.radioButton) {
type = "radiobutton";
value = this.data.fieldValue === this.data.buttonValue;
}
return {
id: this.data.id,
value,
editable: !this.data.readOnly,
name: this.data.fieldName,
rect: this.data.rect,
hidden: this.data.hidden,
actions: this.data.actions,
type,
};
}
}

class ChoiceWidgetAnnotation extends WidgetAnnotation {
Expand Down Expand Up @@ -1841,6 +1967,23 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
this.data.multiSelect = this.hasFieldFlag(AnnotationFieldFlag.MULTISELECT);
this._hasText = true;
}

getAnnotationObject() {
const type = this.data.combo ? "combobox" : "listbox";
const value =
this.data.fieldValue.length > 0 ? this.data.fieldValue[0] : null;
return {
id: this.data.id,
value,
editable: !this.data.readOnly,
name: this.data.fieldName,
rect: this.data.rect,
multipleSelection: this.data.multiSelect,
hidden: this.data.hidden,
actions: this.data.actions,
type,
};
}
}

class TextAnnotation extends MarkupAnnotation {
Expand Down
19 changes: 19 additions & 0 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ class Page {
return stream;
}

getAnnotationObjects() {
// Fetch the page's annotations and get annotation data
// to be used in JS sandbox.
return this._parsedAnnotations.then(function (annotations) {
const results = [];
for (const annotation of annotations) {
if (!isAnnotationRenderable(annotation, "print")) {
continue;
}
const object = annotation.getAnnotationObject();
if (object) {
results.push(object);
}
}

return results;
});
}

save(handler, task, annotationStorage) {
const partialEvaluator = new PartialEvaluator({
xref: this.xref,
Expand Down
12 changes: 12 additions & 0 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,18 @@ class WorkerMessageHandler {
});
});

handler.on("GetAnnotationObjects", function ({ numPages }) {
const promises = [];
for (let pageIndex = 0; pageIndex < numPages; pageIndex++) {
promises.push(
pdfManager.getPage(pageIndex).then(function (page) {
return page.getAnnotationObjects();
})
);
}
return Promise.all(promises).then(data => data.flat());
});

handler.on("SaveDocument", function ({
numPages,
annotationStorage,
Expand Down
14 changes: 14 additions & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,14 @@ class PDFDocumentProxy {
saveDocument(annotationStorage) {
return this._transport.saveDocument(annotationStorage);
}

/**
* @returns {Promise<Array<Object>>} A promise that is resolved with an
* {Array<Object>} containing annotation data.
*/
getAnnotationObjects() {
return this._transport.getAnnotationObjects();
}
}

/**
Expand Down Expand Up @@ -2550,6 +2558,12 @@ class WorkerTransport {
});
}

getAnnotationObjects() {
return this.messageHandler.sendWithPromise("GetAnnotationObjects", {
numPages: this._numPages,
});
}

getDestinations() {
return this.messageHandler.sendWithPromise("GetDestinations", null);
}
Expand Down
23 changes: 23 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,28 @@ const PasswordResponses = {
INCORRECT_PASSWORD: 2,
};

const AnnotationActionEventType = {
E: "MouseEnter",
X: "MouseExit",
D: "MouseDown",
U: "MouseUp",
Fo: "Focus",
Bl: "Blur",
PO: "PageOpen",
PC: "PageClosed",
PV: "PageVisible",
PI: "PageInvisible",
K: "Keystroke",
F: "Format",
V: "Validate",
C: "Calculate",
WC: "WillClose",
WS: "WillSave",
DS: "DidSave",
WP: "WillPrint",
DP: "DidPrint",
};

let verbosity = VerbosityLevel.WARNINGS;

function setVerbosityLevel(level) {
Expand Down Expand Up @@ -972,6 +994,7 @@ export {
OPS,
VerbosityLevel,
UNSUPPORTED_FEATURES,
AnnotationActionEventType,
AnnotationBorderStyleType,
AnnotationFieldFlag,
AnnotationFlag,
Expand Down
Loading

0 comments on commit f2286d8

Please sign in to comment.