1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-21 23:58:07 +02:00

Add the possibility to collect Javascript actions

This commit is contained in:
Calixte Denizet 2020-09-30 20:58:45 +02:00
parent fd1d9cc85f
commit 71ecc3129b
7 changed files with 383 additions and 3 deletions

View file

@ -298,6 +298,7 @@ describe("annotation", function () {
expect(annotation.hasFlag(AnnotationFlag.NOZOOM)).toEqual(true);
expect(annotation.hasFlag(AnnotationFlag.PRINT)).toEqual(true);
expect(annotation.hasFlag(AnnotationFlag.READONLY)).toEqual(false);
expect(annotation.hasFlag(AnnotationFlag.HIDDEN)).toEqual(false);
});
it("should be viewable and not printable by default", function () {
@ -1433,6 +1434,7 @@ describe("annotation", function () {
expect(data.textAlignment).toEqual(null);
expect(data.maxLen).toEqual(null);
expect(data.readOnly).toEqual(false);
expect(data.hidden).toEqual(false);
expect(data.multiLine).toEqual(false);
expect(data.comb).toEqual(false);
done();
@ -1457,6 +1459,7 @@ describe("annotation", function () {
expect(data.textAlignment).toEqual(null);
expect(data.maxLen).toEqual(null);
expect(data.readOnly).toEqual(false);
expect(data.hidden).toEqual(false);
expect(data.multiLine).toEqual(false);
expect(data.comb).toEqual(false);
done();
@ -1484,6 +1487,7 @@ describe("annotation", function () {
expect(data.textAlignment).toEqual(1);
expect(data.maxLen).toEqual(20);
expect(data.readOnly).toEqual(true);
expect(data.hidden).toEqual(false);
expect(data.multiLine).toEqual(true);
done();
}, done.fail);
@ -1863,6 +1867,92 @@ describe("annotation", function () {
done();
}, done.fail);
});
it("should get field object for usage in JS sandbox", function (done) {
const textWidgetRef = Ref.get(123, 0);
const xDictRef = Ref.get(141, 0);
const dDictRef = Ref.get(262, 0);
const next0Ref = Ref.get(314, 0);
const next1Ref = Ref.get(271, 0);
const next2Ref = Ref.get(577, 0);
const next00Ref = Ref.get(413, 0);
const xDict = new Dict();
const dDict = new Dict();
const next0Dict = new Dict();
const next1Dict = new Dict();
const next2Dict = new Dict();
const next00Dict = new Dict();
const xref = new XRefMock([
{ ref: textWidgetRef, data: textWidgetDict },
{ ref: xDictRef, data: xDict },
{ ref: dDictRef, data: dDict },
{ ref: next0Ref, data: next0Dict },
{ ref: next00Ref, data: next00Dict },
{ ref: next1Ref, data: next1Dict },
{ ref: next2Ref, data: next2Dict },
]);
const JS = Name.get("JavaScript");
const additionalActionsDict = new Dict();
const eDict = new Dict();
eDict.set("JS", "hello()");
eDict.set("S", JS);
additionalActionsDict.set("E", eDict);
// Test the cycle detection here
xDict.set("JS", "world()");
xDict.set("S", JS);
xDict.set("Next", [next0Ref, next1Ref, next2Ref, xDictRef]);
next0Dict.set("JS", "olleh()");
next0Dict.set("S", JS);
next0Dict.set("Next", next00Ref);
next00Dict.set("JS", "foo()");
next00Dict.set("S", JS);
next00Dict.set("Next", next0Ref);
next1Dict.set("JS", "dlrow()");
next1Dict.set("S", JS);
next1Dict.set("Next", xDictRef);
next2Dict.set("JS", "oof()");
next2Dict.set("S", JS);
dDict.set("JS", "bar()");
dDict.set("S", JS);
dDict.set("Next", dDictRef);
additionalActionsDict.set("D", dDictRef);
additionalActionsDict.set("X", xDictRef);
textWidgetDict.set("AA", additionalActionsDict);
partialEvaluator.xref = xref;
AnnotationFactory.create(
xref,
textWidgetRef,
pdfManagerMock,
idFactoryMock
)
.then(annotation => {
return annotation.getFieldObject();
})
.then(object => {
const actions = object.actions;
expect(actions.MouseEnter).toEqual(["hello()"]);
expect(actions.MouseExit).toEqual([
"world()",
"olleh()",
"foo()",
"dlrow()",
"oof()",
]);
expect(actions.MouseDown).toEqual(["bar()"]);
done();
}, done.fail);
});
});
describe("ButtonWidgetAnnotation", function () {
@ -2524,7 +2614,11 @@ describe("annotation", function () {
it("should handle push buttons", function (done) {
const buttonWidgetRef = Ref.get(124, 0);
buttonWidgetDict.set("Ff", AnnotationFieldFlag.PUSHBUTTON);
buttonWidgetDict.set("A", "whatever");
const actionDict = new Dict();
actionDict.set("S", Name.get("JavaScript"));
actionDict.set("JS", "do_something();");
buttonWidgetDict.set("A", actionDict);
const xref = new XRefMock([
{ ref: buttonWidgetRef, data: buttonWidgetDict },
@ -2538,6 +2632,7 @@ describe("annotation", function () {
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.pushButton).toEqual(true);
expect(data.actions.Action).toEqual(["do_something();"]);
done();
}, done.fail);
});
@ -2779,6 +2874,7 @@ describe("annotation", function () {
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.readOnly).toEqual(false);
expect(data.hidden).toEqual(false);
expect(data.combo).toEqual(false);
expect(data.multiSelect).toEqual(false);
done();
@ -2801,6 +2897,7 @@ describe("annotation", function () {
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.readOnly).toEqual(false);
expect(data.hidden).toEqual(false);
expect(data.combo).toEqual(false);
expect(data.multiSelect).toEqual(false);
done();
@ -2828,6 +2925,7 @@ describe("annotation", function () {
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.readOnly).toEqual(true);
expect(data.hidden).toEqual(false);
expect(data.combo).toEqual(true);
expect(data.multiSelect).toEqual(true);
done();

View file

@ -63,6 +63,7 @@ describe("document", function () {
expect(pdfDocument.formInfo).toEqual({
hasAcroForm: false,
hasXfa: false,
fields: null,
});
});
@ -75,6 +76,7 @@ describe("document", function () {
expect(pdfDocument.formInfo).toEqual({
hasAcroForm: false,
hasXfa: false,
fields: null,
});
acroForm.set("XFA", ["foo", "bar"]);
@ -82,6 +84,7 @@ describe("document", function () {
expect(pdfDocument.formInfo).toEqual({
hasAcroForm: false,
hasXfa: true,
fields: null,
});
acroForm.set("XFA", new StringStream(""));
@ -89,6 +92,7 @@ describe("document", function () {
expect(pdfDocument.formInfo).toEqual({
hasAcroForm: false,
hasXfa: false,
fields: null,
});
acroForm.set("XFA", new StringStream("non-empty"));
@ -96,6 +100,7 @@ describe("document", function () {
expect(pdfDocument.formInfo).toEqual({
hasAcroForm: false,
hasXfa: true,
fields: null,
});
});
@ -108,6 +113,7 @@ describe("document", function () {
expect(pdfDocument.formInfo).toEqual({
hasAcroForm: false,
hasXfa: false,
fields: null,
});
acroForm.set("Fields", ["foo", "bar"]);
@ -115,6 +121,7 @@ describe("document", function () {
expect(pdfDocument.formInfo).toEqual({
hasAcroForm: true,
hasXfa: false,
fields: ["foo", "bar"],
});
// If the first bit of the `SigFlags` entry is set and the `Fields` array
@ -125,6 +132,7 @@ describe("document", function () {
expect(pdfDocument.formInfo).toEqual({
hasAcroForm: true,
hasXfa: false,
fields: ["foo", "bar"],
});
const annotationDict = new Dict();
@ -147,6 +155,7 @@ describe("document", function () {
expect(pdfDocument.formInfo).toEqual({
hasAcroForm: false,
hasXfa: false,
fields: null,
});
});
});