From 0060eeb7269e8672eeb701b9387ec51170e2db3a Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 27 Mar 2026 12:18:04 +0100 Subject: [PATCH] Use more logical assignment in the code-base --- examples/mobile-viewer/viewer.mjs | 7 ++----- src/core/default_appearance.js | 7 +++---- src/core/jbig2.js | 21 ++++----------------- src/core/xfa/text.js | 8 ++------ web/internal/canvas_context_details_view.js | 4 +--- 5 files changed, 12 insertions(+), 35 deletions(-) diff --git a/examples/mobile-viewer/viewer.mjs b/examples/mobile-viewer/viewer.mjs index edf7d7079..74c40324d 100644 --- a/examples/mobile-viewer/viewer.mjs +++ b/examples/mobile-viewer/viewer.mjs @@ -158,7 +158,7 @@ const PDFViewerApplication = { ); let pdfTitle; - if (metadata && metadata.has("dc:title")) { + if (metadata?.has("dc:title")) { const title = metadata.get("dc:title"); // Ghostscript sometimes returns 'Untitled', so prevent setting the // title to 'Untitled. @@ -166,10 +166,7 @@ const PDFViewerApplication = { pdfTitle = title; } } - - if (!pdfTitle && info && info.Title) { - pdfTitle = info.Title; - } + pdfTitle ||= info?.Title; if (pdfTitle) { this.setTitle(pdfTitle + " - " + document.title); diff --git a/src/core/default_appearance.js b/src/core/default_appearance.js index 57f7feba5..42ad67c86 100644 --- a/src/core/default_appearance.js +++ b/src/core/default_appearance.js @@ -246,6 +246,8 @@ function createDefaultAppearance({ fontSize, fontName, fontColor }) { } class FakeUnicodeFont { + static #fontNameId = 1; + constructor(xref, fontFamily) { this.xref = xref; this.widths = null; @@ -256,11 +258,8 @@ class FakeUnicodeFont { const canvas = new OffscreenCanvas(1, 1); this.ctxMeasure = canvas.getContext("2d", { willReadFrequently: true }); - if (!FakeUnicodeFont._fontNameId) { - FakeUnicodeFont._fontNameId = 1; - } this.fontName = Name.get( - `InvalidPDFjsFont_${fontFamily}_${FakeUnicodeFont._fontNameId++}` + `InvalidPDFjsFont_${fontFamily}_${FakeUnicodeFont.#fontNameId++}` ); } diff --git a/src/core/jbig2.js b/src/core/jbig2.js index e31bd1f5e..842e1ecbd 100644 --- a/src/core/jbig2.js +++ b/src/core/jbig2.js @@ -1677,11 +1677,7 @@ class SimpleSegmentVisitor { } // Combines exported symbols from all referred segments - let symbols = this.symbols; - if (!symbols) { - this.symbols = symbols = {}; - } - + const symbols = (this.symbols ||= {}); const inputSymbols = []; for (const referredSegment of referredSegments) { const referredSymbols = symbols[referredSegment]; @@ -1766,10 +1762,7 @@ class SimpleSegmentVisitor { } onPatternDictionary(dictionary, currentSegment, data, start, end) { - let patterns = this.patterns; - if (!patterns) { - this.patterns = patterns = {}; - } + const patterns = (this.patterns ||= {}); const decodingContext = new DecodingContext(data, start, end); patterns[currentSegment] = decodePatternDictionary( dictionary.mmr, @@ -1811,10 +1804,7 @@ class SimpleSegmentVisitor { } onTables(currentSegment, data, start, end) { - let customTables = this.customTables; - if (!customTables) { - this.customTables = customTables = {}; - } + const customTables = (this.customTables ||= {}); customTables[currentSegment] = decodeTablesSegment(data, start, end); } } @@ -1865,10 +1855,7 @@ class HuffmanTreeNode { this.children[bit] = new HuffmanTreeNode(line); } else { // Create an intermediate node and continue recursively. - let node = this.children[bit]; - if (!node) { - this.children[bit] = node = new HuffmanTreeNode(null); - } + const node = (this.children[bit] ||= new HuffmanTreeNode(null)); node.buildTree(line, shift - 1); } } diff --git a/src/core/xfa/text.js b/src/core/xfa/text.js index 0c12c4b7f..4bad485cb 100644 --- a/src/core/xfa/text.js +++ b/src/core/xfa/text.js @@ -110,9 +110,7 @@ class FontSelector { "size", "letterSpacing", ]) { - if (!xfaFont[name]) { - xfaFont[name] = lastFont.xfaFont[name]; - } + xfaFont[name] ||= lastFont.xfaFont[name]; } for (const name of ["top", "bottom", "left", "right"]) { @@ -127,9 +125,7 @@ class FontSelector { lineHeight || lastFont.lineHeight, this.fontFinder ); - if (!fontInfo.pdfFont) { - fontInfo.pdfFont = lastFont.pdfFont; - } + fontInfo.pdfFont ||= lastFont.pdfFont; this.stack.push(fontInfo); } diff --git a/web/internal/canvas_context_details_view.js b/web/internal/canvas_context_details_view.js index 4f1ddd725..38ed541c3 100644 --- a/web/internal/canvas_context_details_view.js +++ b/web/internal/canvas_context_details_view.js @@ -199,9 +199,7 @@ class CanvasContextDetailsView { if (type !== "2d") { return ctx; } - if (!wrappedCtx) { - wrappedCtx = this.wrapContext(ctx, label); - } + wrappedCtx ??= this.wrapContext(ctx, label); return wrappedCtx; }; return canvas.getContext("2d");