From 57ee0355734d0f4f67e4e327faa39112464e2a3d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 21 Jul 2024 09:04:18 +0200 Subject: [PATCH 1/3] Change the `compatibilityParams`, used with AppOptions, to a `Map` This is needed for upcoming changes, and simplifies both checking if an entry exists and iteration of the entries. --- web/app_options.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/web/app_options.js b/web/app_options.js index 250573674..e000d0649 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -15,7 +15,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { // eslint-disable-next-line no-var - var compatibilityParams = Object.create(null); + var compatParams = new Map(); if ( typeof PDFJSDev !== "undefined" && PDFJSDev.test("LIB") && @@ -34,9 +34,9 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { // Limit canvas size to 5 mega-pixels on mobile. // Support: Android, iOS - (function checkCanvasSizeLimitation() { + (function () { if (isIOS || isAndroid) { - compatibilityParams.maxCanvasPixels = 5242880; + compatParams.set("maxCanvasPixels", 5242880); } })(); } @@ -447,8 +447,8 @@ const userOptions = Object.create(null); if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { // Apply any compatibility-values to the user-options, // see also `AppOptions.remove` below. - for (const name in compatibilityParams) { - userOptions[name] = compatibilityParams[name]; + for (const [name, value] of compatParams) { + userOptions[name] = value; } } @@ -464,10 +464,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) { if (kind & OptionKind.BROWSER) { throw new Error(`Cannot mix "PREFERENCE" and "BROWSER" kind: ${name}`); } - if ( - typeof compatibilityParams === "object" && - compatibilityParams[name] !== undefined - ) { + if (typeof compatParams === "object" && compatParams.has(name)) { throw new Error( `Should not have compatibility-value for "PREFERENCE" kind: ${name}` ); @@ -554,9 +551,8 @@ class AppOptions { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { // Re-apply a compatibility-value, if it exists, to the user-options. - const val = compatibilityParams[name]; - if (val !== undefined) { - userOptions[name] = val; + if (compatParams.has(name)) { + userOptions[name] = compatParams.get(name); } } } @@ -571,7 +567,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { } for (const name in userOptions) { // Ignore any compatibility-values in the user-options. - if (compatibilityParams[name] !== undefined) { + if (compatParams.has(name)) { continue; } console.warn( From 7bd920691fb950429d5ddea3f56da30b395b2c6c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 21 Jul 2024 09:25:29 +0200 Subject: [PATCH 2/3] Change the `userOptions`, used with AppOptions, to a `Map` This is needed for upcoming changes, and simplifies both checking if an entry exists and iteration of the entries. --- web/app_options.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/web/app_options.js b/web/app_options.js index e000d0649..4e5a7e88d 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -442,13 +442,13 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { }; } -const userOptions = Object.create(null); +const userOptions = new Map(); if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { // Apply any compatibility-values to the user-options, // see also `AppOptions.remove` below. for (const [name, value] of compatParams) { - userOptions[name] = value; + userOptions.set(name, value); } } @@ -489,7 +489,9 @@ class AppOptions { } static get(name) { - return userOptions[name] ?? defaultOptions[name]?.value ?? undefined; + return userOptions.has(name) + ? userOptions.get(name) + : defaultOptions[name]?.value; } static getAll(kind = null, defaultOnly = false) { @@ -500,9 +502,10 @@ class AppOptions { if (kind && !(kind & defaultOption.kind)) { continue; } - options[name] = defaultOnly - ? defaultOption.value - : (userOptions[name] ?? defaultOption.value); + options[name] = + !defaultOnly && userOptions.has(name) + ? userOptions.get(name) + : defaultOption.value; } return options; } @@ -513,7 +516,7 @@ class AppOptions { if (!defaultOption || typeof value !== typeof defaultOption.value) { return; } - userOptions[name] = value; + userOptions.set(name, value); } static setAll(options, prefs = false) { @@ -536,7 +539,7 @@ class AppOptions { (events ||= new Map()).set(name, userOption); } } - userOptions[name] = userOption; + userOptions.set(name, userOption); } if (events) { @@ -547,12 +550,12 @@ class AppOptions { } static remove(name) { - delete userOptions[name]; + userOptions.delete(name); if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { // Re-apply a compatibility-value, if it exists, to the user-options. if (compatParams.has(name)) { - userOptions[name] = compatParams.get(name); + userOptions.set(name, compatParams.get(name)); } } } @@ -565,7 +568,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { // opt-out of having the `Preferences` override existing `AppOptions`. return true; } - for (const name in userOptions) { + for (const [name] of userOptions) { // Ignore any compatibility-values in the user-options. if (compatParams.has(name)) { continue; From 26989fdf24fa526e8e8f95995deaf6b0bd28e47e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 21 Jul 2024 09:32:36 +0200 Subject: [PATCH 3/3] Add basic validation of the AppOptions `BROWSER`-kind --- web/app_options.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/web/app_options.js b/web/app_options.js index 4e5a7e88d..962cac375 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -477,6 +477,15 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) { ) { throw new Error(`Invalid value for "PREFERENCE" kind: ${name}`); } + } else if (kind & OptionKind.BROWSER) { + if (typeof compatParams === "object" && compatParams.has(name)) { + throw new Error( + `Should not have compatibility-value for "BROWSER" kind: ${name}` + ); + } + if (value === undefined) { + throw new Error(`Invalid value for "BROWSER" kind: ${name}`); + } } } }