1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

JS -- Add listener for sandbox events only if there are some actions

* When no actions then set it to null instead of empty object
* Even if a field has no actions, it needs to listen to events from the sandbox in order to be updated if an action changes something in it.
This commit is contained in:
Calixte Denizet 2020-10-28 19:16:56 +01:00
parent 55f55f5859
commit a5279897a7
13 changed files with 170 additions and 28 deletions

View file

@ -28,6 +28,8 @@ import { SimpleLinkService } from "./pdf_link_service.js";
* @property {IPDFLinkService} linkService
* @property {DownloadManager} downloadManager
* @property {IL10n} l10n - Localization service.
* @property {boolean} [enableScripting]
* @property {Promise<boolean>} [hasJSActionsPromise]
*/
class AnnotationLayerBuilder {
@ -43,6 +45,8 @@ class AnnotationLayerBuilder {
imageResourcesPath = "",
renderInteractiveForms = true,
l10n = NullL10n,
enableScripting = false,
hasJSActionsPromise = null,
}) {
this.pageDiv = pageDiv;
this.pdfPage = pdfPage;
@ -52,6 +56,8 @@ class AnnotationLayerBuilder {
this.renderInteractiveForms = renderInteractiveForms;
this.l10n = l10n;
this.annotationStorage = annotationStorage;
this.enableScripting = enableScripting;
this._hasJSActionsPromise = hasJSActionsPromise;
this.div = null;
this._cancelled = false;
@ -64,7 +70,10 @@ class AnnotationLayerBuilder {
* annotations is complete.
*/
render(viewport, intent = "display") {
return this.pdfPage.getAnnotations({ intent }).then(annotations => {
return Promise.all([
this.pdfPage.getAnnotations({ intent }),
this._hasJSActionsPromise,
]).then(([annotations, hasJSActions = false]) => {
if (this._cancelled) {
return;
}
@ -82,6 +91,8 @@ class AnnotationLayerBuilder {
linkService: this.linkService,
downloadManager: this.downloadManager,
annotationStorage: this.annotationStorage,
enableScripting: this.enableScripting,
hasJSActions,
};
if (this.div) {
@ -126,6 +137,8 @@ class DefaultAnnotationLayerFactory {
* for annotation icons. Include trailing slash.
* @param {boolean} renderInteractiveForms
* @param {IL10n} l10n
* @param {boolean} enableScripting
* @param {Promise<boolean>} hasJSActionsPromise
* @returns {AnnotationLayerBuilder}
*/
createAnnotationLayerBuilder(
@ -134,7 +147,9 @@ class DefaultAnnotationLayerFactory {
annotationStorage = null,
imageResourcesPath = "",
renderInteractiveForms = true,
l10n = NullL10n
l10n = NullL10n,
enableScripting = false,
hasJSActionsPromise = null
) {
return new AnnotationLayerBuilder({
pageDiv,
@ -144,6 +159,8 @@ class DefaultAnnotationLayerFactory {
linkService: new SimpleLinkService(),
l10n,
annotationStorage,
enableScripting,
hasJSActionsPromise,
});
}
}

View file

@ -449,6 +449,7 @@ const PDFViewerApplication = {
enablePrintAutoRotate: AppOptions.get("enablePrintAutoRotate"),
useOnlyCssZoom: AppOptions.get("useOnlyCssZoom"),
maxCanvasPixels: AppOptions.get("maxCanvasPixels"),
enableScripting: AppOptions.get("enableScripting"),
});
pdfRenderingQueue.setViewer(this.pdfViewer);
pdfLinkService.setViewer(this.pdfViewer);

View file

@ -77,6 +77,8 @@ const DEFAULT_CACHE_SIZE = 10;
* total pixels, i.e. width * height. Use -1 for no limit. The default value
* is 4096 * 4096 (16 mega-pixels).
* @property {IL10n} l10n - Localization service.
* @property {boolean} [enableScripting] - Enable embedded script execution.
* The default value is `false`.
*/
function PDFPageViewBuffer(size) {
@ -187,6 +189,7 @@ class BaseViewer {
this.useOnlyCssZoom = options.useOnlyCssZoom || false;
this.maxCanvasPixels = options.maxCanvasPixels;
this.l10n = options.l10n || NullL10n;
this.enableScripting = options.enableScripting || false;
this.defaultRenderingQueue = !options.renderingQueue;
if (this.defaultRenderingQueue) {
@ -527,6 +530,7 @@ class BaseViewer {
useOnlyCssZoom: this.useOnlyCssZoom,
maxCanvasPixels: this.maxCanvasPixels,
l10n: this.l10n,
enableScripting: this.enableScripting,
});
this._pages.push(pageView);
}
@ -1208,6 +1212,7 @@ class BaseViewer {
* for annotation icons. Include trailing slash.
* @param {boolean} renderInteractiveForms
* @param {IL10n} l10n
* @param {boolean} [enableScripting]
* @returns {AnnotationLayerBuilder}
*/
createAnnotationLayerBuilder(
@ -1216,7 +1221,8 @@ class BaseViewer {
annotationStorage = null,
imageResourcesPath = "",
renderInteractiveForms = false,
l10n = NullL10n
l10n = NullL10n,
enableScripting = false
) {
return new AnnotationLayerBuilder({
pageDiv,
@ -1227,6 +1233,10 @@ class BaseViewer {
linkService: this.linkService,
downloadManager: this.downloadManager,
l10n,
enableScripting,
hasJSActionsPromise: enableScripting
? this.pdfDocument.hasJSActions()
: Promise.resolve(false),
});
}

View file

@ -181,6 +181,8 @@ class IPDFAnnotationLayerFactory {
* for annotation icons. Include trailing slash.
* @param {boolean} renderInteractiveForms
* @param {IL10n} l10n
* @param {boolean} [enableScripting]
* @param {Promise<boolean>} [hasJSActionsPromise]
* @returns {AnnotationLayerBuilder}
*/
createAnnotationLayerBuilder(
@ -189,7 +191,9 @@ class IPDFAnnotationLayerFactory {
annotationStorage = null,
imageResourcesPath = "",
renderInteractiveForms = true,
l10n = undefined
l10n = undefined,
enableScripting = false,
hasJSActionsPromise = null
) {}
}

View file

@ -63,6 +63,8 @@ import { viewerCompatibilityParams } from "./viewer_compatibility.js";
* total pixels, i.e. width * height. Use -1 for no limit. The default value
* is 4096 * 4096 (16 mega-pixels).
* @property {IL10n} l10n - Localization service.
* @property {boolean} [enableScripting] - Enable embedded script execution.
* The default value is `false`.
*/
const MAX_CANVAS_PIXELS = viewerCompatibilityParams.maxCanvasPixels || 16777216;
@ -109,6 +111,7 @@ class PDFPageView {
this.renderer = options.renderer || RendererType.CANVAS;
this.enableWebGL = options.enableWebGL || false;
this.l10n = options.l10n || NullL10n;
this.enableScripting = options.enableScripting || false;
this.paintTask = null;
this.paintedViewportMap = new WeakMap();
@ -549,7 +552,8 @@ class PDFPageView {
this._annotationStorage,
this.imageResourcesPath,
this.renderInteractiveForms,
this.l10n
this.l10n,
this.enableScripting
);
}
this._renderAnnotationLayer();

View file

@ -1023,7 +1023,10 @@ function getActiveOrFocusedElement() {
*/
function generateRandomStringForSandbox(objects) {
const allObjects = Object.values(objects).flat(2);
const actions = allObjects.map(obj => Object.values(obj.actions)).flat(2);
const actions = allObjects
.filter(obj => !!obj.actions)
.map(obj => Object.values(obj.actions))
.flat(2);
while (true) {
const name = new Uint8Array(64);