From 834423b51d498e0acbc4d2199406c648920bbdbd Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 12 Apr 2025 17:13:39 +0200 Subject: [PATCH] Add more logical assignment in the `src/` folder This patch uses nullish coalescing assignment in cases where it's immediately obvious from surrounding code that doing so is safe, and logical OR assignment elsewhere (mostly the changes in XFA code). --- src/core/annotation.js | 4 ++-- src/core/catalog.js | 12 +++------- src/core/document.js | 4 +--- src/core/xfa/parser.js | 9 ++------ src/core/xfa/template.js | 48 ++++++++++++++-------------------------- src/scripting_api/app.js | 21 ++++++++---------- 6 files changed, 33 insertions(+), 65 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index d6b612b14..a1a7a76a6 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -3488,8 +3488,8 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation { // always make the field value an array with zero, one or multiple items. if (typeof this.data.fieldValue === "string") { this.data.fieldValue = [this.data.fieldValue]; - } else if (!this.data.fieldValue) { - this.data.fieldValue = []; + } else { + this.data.fieldValue ||= []; } } else { // The specs say that we should have an indices array only with diff --git a/src/core/catalog.js b/src/core/catalog.js index 8c07a2f3f..ee5484e26 100644 --- a/src/core/catalog.js +++ b/src/core/catalog.js @@ -998,9 +998,7 @@ class Catalog { warn(`Bad value, for key "${key}", in ViewerPreferences: ${value}.`); continue; } - if (!prefs) { - prefs = Object.create(null); - } + prefs ??= Object.create(null); prefs[key] = prefValue; } return shadow(this, "viewerPreferences", prefs); @@ -1042,9 +1040,7 @@ class Catalog { const nameTree = new NameTree(obj.getRaw("EmbeddedFiles"), this.xref); for (const [key, value] of nameTree.getAll()) { const fs = new FileSpec(value, this.xref); - if (!attachments) { - attachments = Object.create(null); - } + attachments ??= Object.create(null); attachments[stringToPDFString(key)] = fs.serializable; } } @@ -1058,9 +1054,7 @@ class Catalog { if (obj instanceof Dict && obj.has("XFAImages")) { const nameTree = new NameTree(obj.getRaw("XFAImages"), this.xref); for (const [key, value] of nameTree.getAll()) { - if (!xfaImages) { - xfaImages = new Dict(this.xref); - } + xfaImages ??= new Dict(this.xref); xfaImages.set(stringToPDFString(key), value); } } diff --git a/src/core/document.js b/src/core/document.js index 7f91414cd..b1e1caaec 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -1532,9 +1532,7 @@ class PDFDocument { warn(`Bad value, for custom key "${key}", in Info: ${value}.`); continue; } - if (!docInfo.Custom) { - docInfo.Custom = Object.create(null); - } + docInfo.Custom ??= Object.create(null); docInfo.Custom[key] = customValue; continue; } diff --git a/src/core/xfa/parser.js b/src/core/xfa/parser.js index 8b6bdd699..4f506fb24 100644 --- a/src/core/xfa/parser.js +++ b/src/core/xfa/parser.js @@ -91,9 +91,7 @@ class XFAParser extends XMLParserBase { } } else if (name.startsWith("xmlns:")) { const prefix = name.substring("xmlns:".length); - if (!prefixes) { - prefixes = []; - } + prefixes ??= []; prefixes.push({ prefix, value }); } else { const i = name.indexOf(":"); @@ -102,10 +100,7 @@ class XFAParser extends XMLParserBase { } else { // Attributes can have their own namespace. // For example in data, we can have - let nsAttrs = attributeObj[$nsAttributes]; - if (!nsAttrs) { - nsAttrs = attributeObj[$nsAttributes] = Object.create(null); - } + const nsAttrs = (attributeObj[$nsAttributes] ??= Object.create(null)); const [ns, attrName] = [name.slice(0, i), name.slice(i + 1)]; const attrs = (nsAttrs[ns] ||= Object.create(null)); attrs[attrName] = value; diff --git a/src/core/xfa/template.js b/src/core/xfa/template.js index 594ab25a7..46cbffb1c 100644 --- a/src/core/xfa/template.js +++ b/src/core/xfa/template.js @@ -2462,9 +2462,7 @@ class ExclGroup extends XFAObject { setAccess(this, attributes.class); - if (!this[$extra]) { - this[$extra] = Object.create(null); - } + this[$extra] ||= Object.create(null); Object.assign(this[$extra], { children, @@ -2953,9 +2951,7 @@ class Field extends XFAObject { } } - if (!ui.attributes.style) { - ui.attributes.style = Object.create(null); - } + ui.attributes.style ||= Object.create(null); let aElement = null; @@ -3048,9 +3044,7 @@ class Field extends XFAObject { caption.attributes.class[0] = "xfaCaptionForCheckButton"; } - if (!ui.attributes.class) { - ui.attributes.class = []; - } + ui.attributes.class ||= []; ui.children.splice(0, 0, caption); @@ -4067,11 +4061,9 @@ class PageArea extends XFAObject { } [$getNextPage]() { - if (!this[$extra]) { - this[$extra] = { - numberOfUse: 0, - }; - } + this[$extra] ||= { + numberOfUse: 0, + }; const parent = this[$getParent](); if (parent.relation === "orderedOccurrence") { @@ -4090,11 +4082,9 @@ class PageArea extends XFAObject { [$toHTML]() { // TODO: incomplete. - if (!this[$extra]) { - this[$extra] = { - numberOfUse: 1, - }; - } + this[$extra] ||= { + numberOfUse: 1, + }; const children = []; this[$extra].children = children; @@ -4186,13 +4176,11 @@ class PageSet extends XFAObject { } [$getNextPage]() { - if (!this[$extra]) { - this[$extra] = { - numberOfUse: 1, - pageIndex: -1, - pageSetIndex: -1, - }; - } + this[$extra] ||= { + numberOfUse: 1, + pageIndex: -1, + pageSetIndex: -1, + }; if (this.relation === "orderedOccurrence") { if (this[$extra].pageIndex + 1 < this.pageArea.children.length) { @@ -5067,9 +5055,7 @@ class Subform extends XFAObject { setAccess(this, attributes.class); - if (!this[$extra]) { - this[$extra] = Object.create(null); - } + this[$extra] ||= Object.create(null); Object.assign(this[$extra], { children, @@ -5495,9 +5481,7 @@ class Template extends XFAObject { } } - if (!pageArea) { - pageArea = pageAreas[0]; - } + pageArea ||= pageAreas[0]; pageArea[$extra] = { numberOfUse: 1, diff --git a/src/scripting_api/app.js b/src/scripting_api/app.js index 74dba793d..978025936 100644 --- a/src/scripting_api/app.js +++ b/src/scripting_api/app.js @@ -202,18 +202,15 @@ class App extends PDFObject { } get constants() { - if (!this._constants) { - this._constants = Object.freeze({ - align: Object.freeze({ - left: 0, - center: 1, - right: 2, - top: 3, - bottom: 4, - }), - }); - } - return this._constants; + return (this._constants ??= Object.freeze({ + align: Object.freeze({ + left: 0, + center: 1, + right: 2, + top: 3, + bottom: 4, + }), + })); } set constants(_) {