From e8030752f37a66ec73399d5494bb45ad5220deb5 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 18 May 2023 18:52:54 +0200 Subject: [PATCH] Introduce even more modern JavaScript features in the code-base After PR 12563 we're now free to use e.g. logical OR assignment, nullish coalescing, and optional chaining in the entire code-base. --- src/core/ccitt.js | 6 +----- src/core/colorspace.js | 4 ++-- src/core/document.js | 2 +- src/core/fonts.js | 2 +- src/core/xfa/formcalc_parser.js | 2 +- src/core/xfa/template.js | 4 ++-- src/display/canvas.js | 2 +- src/display/display_utils.js | 30 ++++++++++++++---------------- src/scripting_api/field.js | 2 +- web/chromecom.js | 2 +- 10 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/core/ccitt.js b/src/core/ccitt.js index 5b292d62d10bc..8a341c016fbad 100644 --- a/src/core/ccitt.js +++ b/src/core/ccitt.js @@ -477,11 +477,7 @@ class CCITTFaxDecoder { this.byteAlign = options.EncodedByteAlign || false; this.columns = options.Columns || 1728; this.rows = options.Rows || 0; - let eoblock = options.EndOfBlock; - if (eoblock === null || eoblock === undefined) { - eoblock = true; - } - this.eoblock = eoblock; + this.eoblock = options.EndOfBlock ?? true; this.black = options.BlackIs1 || false; this.codingLine = new Uint32Array(this.columns + 1); diff --git a/src/core/colorspace.js b/src/core/colorspace.js index bb72f5e866798..f1140e9dd40ee 100644 --- a/src/core/colorspace.js +++ b/src/core/colorspace.js @@ -928,8 +928,8 @@ const CalGrayCS = (function CalGrayCSClosure() { "WhitePoint missing - required for color space CalGray" ); } - blackPoint = blackPoint || [0, 0, 0]; - gamma = gamma || 1; + blackPoint ||= [0, 0, 0]; + gamma ||= 1; // Translate arguments to spec variables. this.XW = whitePoint[0]; diff --git a/src/core/document.js b/src/core/document.js index c659e957e8390..0180f34a284e5 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -1702,7 +1702,7 @@ class PDFDocument { get calculationOrderIds() { const acroForm = this.catalog.acroForm; - if (!acroForm || !acroForm.has("CO")) { + if (!acroForm?.has("CO")) { return shadow(this, "calculationOrderIds", null); } diff --git a/src/core/fonts.js b/src/core/fonts.js index f4e36e2b8564a..1c5ef008b9fcf 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -2579,7 +2579,7 @@ class Font { if (!isTrueType) { const isComposite = properties.composite && - ((properties.cidToGidMap || []).length > 0 || + (properties.cidToGidMap?.length > 0 || !(properties.cMap instanceof IdentityCMap)); // OpenType font (skip composite fonts with non-default glyph mapping). if ( diff --git a/src/core/xfa/formcalc_parser.js b/src/core/xfa/formcalc_parser.js index 55a6ba326d1f0..dfbc1cc717d01 100644 --- a/src/core/xfa/formcalc_parser.js +++ b/src/core/xfa/formcalc_parser.js @@ -1282,7 +1282,7 @@ class Parser { tok = tok2 || this.lexer.next(); if (tok.id === TOKEN.step) { [tok, step] = this.parseSimpleExpr(); - tok = tok || this.lexer.next(); + tok ||= this.lexer.next(); } if (tok.id !== TOKEN.do) { diff --git a/src/core/xfa/template.js b/src/core/xfa/template.js index 924f9fdc26771..c8e4e56be6dee 100644 --- a/src/core/xfa/template.js +++ b/src/core/xfa/template.js @@ -932,7 +932,7 @@ class Border extends XFAObject { const { edges } = this[$getExtra](); const edgeStyles = edges.map(node => { const style = node[$toStyle](); - style.color = style.color || "#000000"; + style.color ||= "#000000"; return style; }); @@ -3871,7 +3871,7 @@ class NumericEdit extends XFAObject { attributes: { type: "text", fieldId: field[$uid], - dataId: (field[$data] && field[$data][$uid]) || field[$uid], + dataId: field[$data]?.[$uid] || field[$uid], class: ["xfaTextfield"], style, "aria-label": ariaLabel(field), diff --git a/src/display/canvas.js b/src/display/canvas.js index f3ba138252bc4..aa151def22b1c 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -2061,7 +2061,7 @@ class CanvasGraphics { } if (isAddToPathSet) { - const paths = this.pendingTextPaths || (this.pendingTextPaths = []); + const paths = (this.pendingTextPaths ||= []); paths.push({ transform: getCurrentTransform(ctx), x, diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 54f2ff40c10e5..452111c7b77a8 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -760,22 +760,20 @@ class PDFDateString { } // Lazily initialize the regular expression. - if (!pdfDateStringRegex) { - pdfDateStringRegex = new RegExp( - "^D:" + // Prefix (required) - "(\\d{4})" + // Year (required) - "(\\d{2})?" + // Month (optional) - "(\\d{2})?" + // Day (optional) - "(\\d{2})?" + // Hour (optional) - "(\\d{2})?" + // Minute (optional) - "(\\d{2})?" + // Second (optional) - "([Z|+|-])?" + // Universal time relation (optional) - "(\\d{2})?" + // Offset hour (optional) - "'?" + // Splitting apostrophe (optional) - "(\\d{2})?" + // Offset minute (optional) - "'?" // Trailing apostrophe (optional) - ); - } + pdfDateStringRegex ||= new RegExp( + "^D:" + // Prefix (required) + "(\\d{4})" + // Year (required) + "(\\d{2})?" + // Month (optional) + "(\\d{2})?" + // Day (optional) + "(\\d{2})?" + // Hour (optional) + "(\\d{2})?" + // Minute (optional) + "(\\d{2})?" + // Second (optional) + "([Z|+|-])?" + // Universal time relation (optional) + "(\\d{2})?" + // Offset hour (optional) + "'?" + // Splitting apostrophe (optional) + "(\\d{2})?" + // Offset minute (optional) + "'?" // Trailing apostrophe (optional) + ); // Optional fields that don't satisfy the requirements from the regular // expression (such as incorrect digit counts or numbers that are out of diff --git a/src/scripting_api/field.js b/src/scripting_api/field.js index cfd37da0f52b2..19b71c7316f0b 100644 --- a/src/scripting_api/field.js +++ b/src/scripting_api/field.js @@ -371,7 +371,7 @@ class Field extends PDFObject { nIdx = Array.isArray(this._currentValueIndices) ? this._currentValueIndices[0] : this._currentValueIndices; - nIdx = nIdx || 0; + nIdx ||= 0; } if (nIdx < 0 || nIdx >= this.numItems) { diff --git a/web/chromecom.js b/web/chromecom.js index bfd0d95f3925e..60a83846c5d0f 100644 --- a/web/chromecom.js +++ b/web/chromecom.js @@ -373,7 +373,7 @@ class ChromePreferences extends BasePreferences { ); chrome.storage.managed.get(defaultManagedPrefs, function (items) { - items = items || defaultManagedPrefs; + items ||= defaultManagedPrefs; // Migration logic for deprecated preferences: If the new preference // is not defined by an administrator (i.e. the value is the same as // the default value), and a deprecated preference is set with a