Merge pull request #16779 from Snuffleupagus/deprecate-getJavaScript

[api-minor] Deprecate the `PDFDocumentProxy.getJavaScript` method
This commit is contained in:
Jonas Jenwald 2023-08-01 20:58:36 +02:00 committed by GitHub
commit e6728f94f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 58 deletions

View File

@ -987,7 +987,10 @@ class Catalog {
return;
}
js = stringToPDFString(js).replaceAll("\x00", "");
(javaScript ||= new Map()).set(name, js);
// Skip empty entries, similar to the `_collectJS` function.
if (js) {
(javaScript ||= new Map()).set(name, js);
}
}
if (obj instanceof Dict && obj.has("JavaScript")) {
@ -1005,15 +1008,6 @@ class Catalog {
return javaScript;
}
get javaScript() {
const javaScript = this._collectJavaScript();
return shadow(
this,
"javaScript",
javaScript ? [...javaScript.values()] : null
);
}
get jsActions() {
const javaScript = this._collectJavaScript();
let actions = collectActions(
@ -1023,9 +1017,8 @@ class Catalog {
);
if (javaScript) {
if (!actions) {
actions = Object.create(null);
}
actions ||= Object.create(null);
for (const [key, val] of javaScript) {
if (key in actions) {
actions[key].push(val);

View File

@ -463,10 +463,6 @@ class WorkerMessageHandler {
return pdfManager.ensureCatalog("attachments");
});
handler.on("GetJavaScript", function (data) {
return pdfManager.ensureCatalog("javaScript");
});
handler.on("GetDocJSActions", function (data) {
return pdfManager.ensureCatalog("jsActions");
});

View File

@ -760,6 +760,26 @@ class PDFDocumentProxy {
this._pdfInfo = pdfInfo;
this._transport = transport;
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
Object.defineProperty(this, "getJavaScript", {
value: () => {
deprecated(
"`PDFDocumentProxy.getJavaScript`, " +
"please use `PDFDocumentProxy.getJSActions` instead."
);
return this.getJSActions().then(js => {
if (!js) {
return js;
}
const jsArr = [];
for (const name in js) {
jsArr.push(...js[name]);
}
return jsArr;
});
},
});
}
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
// For testing purposes.
Object.defineProperty(this, "getXFADatasets", {
@ -917,19 +937,10 @@ class PDFDocumentProxy {
return this._transport.getAttachments();
}
/**
* @returns {Promise<Array<string> | null>} A promise that is resolved with
* an {Array} of all the JavaScript strings in the name tree, or `null`
* if no JavaScript exists.
*/
getJavaScript() {
return this._transport.getJavaScript();
}
/**
* @returns {Promise<Object | null>} A promise that is resolved with
* an {Object} with the JavaScript actions:
* - from the name tree (like getJavaScript);
* - from the name tree.
* - from A or AA entries in the catalog dictionary.
* , or `null` if no JavaScript exists.
*/
@ -3016,10 +3027,6 @@ class WorkerTransport {
return this.messageHandler.sendWithPromise("GetAttachments", null);
}
getJavaScript() {
return this.messageHandler.sendWithPromise("GetJavaScript", null);
}
getDocJSActions() {
return this.#cacheSimpleMethod("GetDocJSActions");
}

View File

@ -1358,21 +1358,16 @@ describe("api", function () {
await loadingTask.destroy();
});
it("gets javascript", async function () {
const javascript = await pdfDocument.getJavaScript();
expect(javascript).toEqual(null);
});
it("gets javascript with printing instructions (JS action)", async function () {
// PDF document with "JavaScript" action in the OpenAction dictionary.
const loadingTask = getDocument(buildGetDocumentParams("issue6106.pdf"));
const pdfDoc = await loadingTask.promise;
const javascript = await pdfDoc.getJavaScript();
const { OpenAction } = await pdfDoc.getJSActions();
expect(javascript).toEqual([
expect(OpenAction).toEqual([
"this.print({bUI:true,bSilent:false,bShrinkToFit:true});",
]);
expect(javascript[0]).toMatch(AutoPrintRegExp);
expect(OpenAction[0]).toMatch(AutoPrintRegExp);
await loadingTask.destroy();
});

View File

@ -1543,9 +1543,9 @@ const PDFViewerApplication = {
* @private
*/
async _initializeAutoPrint(pdfDocument, openActionPromise) {
const [openAction, javaScript] = await Promise.all([
const [openAction, jsActions] = await Promise.all([
openActionPromise,
!this.pdfViewer.enableScripting ? pdfDocument.getJavaScript() : null,
!this.pdfViewer.enableScripting ? pdfDocument.getJSActions() : null,
]);
if (pdfDocument !== this.pdfDocument) {
@ -1556,25 +1556,18 @@ const PDFViewerApplication = {
if (openAction?.action === "Print") {
triggerAutoPrint = true;
}
if (javaScript) {
javaScript.some(js => {
if (!js) {
// Don't warn/fallback for empty JavaScript actions.
return false;
}
console.warn("Warning: JavaScript support is not enabled");
return true;
});
if (!triggerAutoPrint) {
// Hack to support auto printing.
for (const js of javaScript) {
if (js && AutoPrintRegExp.test(js)) {
triggerAutoPrint = true;
break;
}
if (jsActions) {
for (const name in jsActions) {
if (jsActions[name]) {
console.warn("Warning: JavaScript support is not enabled");
break;
}
}
// Hack to support auto printing.
triggerAutoPrint ||= !!(
jsActions.OpenAction && AutoPrintRegExp.test(jsActions.OpenAction)
);
}
if (triggerAutoPrint) {