Convert files in the src/display/-folder to use optional chaining where possible

By using optional chaining, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining, it's possible to reduce unnecessary code-repetition in many cases.
Note that these changes also reduce the size of the *built* `pdf.js` file, when `SKIP_BABEL == true` is set, and for the `MOZCENTRAL` build-target that result in a `0.1%` filesize reduction from a simple and mostly mechanical code change.
This commit is contained in:
Jonas Jenwald 2020-11-06 14:36:16 +01:00
parent e3851a6765
commit 1dad255784
8 changed files with 19 additions and 41 deletions

View File

@ -1566,9 +1566,7 @@ class PDFPageProxy {
return; return;
} }
} }
intentState.streamReader.cancel( intentState.streamReader.cancel(new AbortException(reason?.message));
new AbortException(reason && reason.message)
);
intentState.streamReader = null; intentState.streamReader = null;
if (this._transport.destroyed) { if (this._transport.destroyed) {
@ -1615,8 +1613,7 @@ class LoopbackPort {
let buffer, result; let buffer, result;
if ((buffer = value.buffer) && isArrayBuffer(buffer)) { if ((buffer = value.buffer) && isArrayBuffer(buffer)) {
// We found object with ArrayBuffer (typed array). // We found object with ArrayBuffer (typed array).
const transferable = transfers && transfers.includes(buffer); if (transfers?.includes(buffer)) {
if (transferable) {
result = new value.constructor( result = new value.constructor(
buffer, buffer,
value.byteOffset, value.byteOffset,
@ -1712,8 +1709,7 @@ const PDFWorker = (function PDFWorkerClosure() {
fallbackWorkerSrc = "./pdf.worker.js"; fallbackWorkerSrc = "./pdf.worker.js";
} }
} else if (typeof document === "object" && "currentScript" in document) { } else if (typeof document === "object" && "currentScript" in document) {
const pdfjsFilePath = const pdfjsFilePath = document.currentScript?.src;
document.currentScript && document.currentScript.src;
if (pdfjsFilePath) { if (pdfjsFilePath) {
fallbackWorkerSrc = pdfjsFilePath.replace( fallbackWorkerSrc = pdfjsFilePath.replace(
/(\.(?:min\.)?js)(\?.*)?$/i, /(\.(?:min\.)?js)(\?.*)?$/i,
@ -1739,8 +1735,7 @@ const PDFWorker = (function PDFWorkerClosure() {
function getMainThreadWorkerMessageHandler() { function getMainThreadWorkerMessageHandler() {
let mainWorkerMessageHandler; let mainWorkerMessageHandler;
try { try {
mainWorkerMessageHandler = mainWorkerMessageHandler = globalThis.pdfjsWorker?.WorkerMessageHandler;
globalThis.pdfjsWorker && globalThis.pdfjsWorker.WorkerMessageHandler;
} catch (ex) { } catch (ex) {
/* Ignore errors. */ /* Ignore errors. */
} }
@ -2378,11 +2373,7 @@ class WorkerTransport {
} }
let fontRegistry = null; let fontRegistry = null;
if ( if (params.pdfBug && globalThis.FontInspector?.enabled) {
params.pdfBug &&
globalThis.FontInspector &&
globalThis.FontInspector.enabled
) {
fontRegistry = { fontRegistry = {
registerFont(font, url) { registerFont(font, url) {
globalThis.FontInspector.fontAdded(font, url); globalThis.FontInspector.fontAdded(font, url);
@ -2441,11 +2432,7 @@ class WorkerTransport {
// Heuristic that will allow us not to store large data. // Heuristic that will allow us not to store large data.
const MAX_IMAGE_SIZE_TO_STORE = 8000000; const MAX_IMAGE_SIZE_TO_STORE = 8000000;
if ( if (imageData?.data?.length > MAX_IMAGE_SIZE_TO_STORE) {
imageData &&
"data" in imageData &&
imageData.data.length > MAX_IMAGE_SIZE_TO_STORE
) {
pageProxy.cleanupAfterRender = true; pageProxy.cleanupAfterRender = true;
} }
break; break;
@ -2868,11 +2855,7 @@ const InternalRenderTask = (function InternalRenderTaskClosure() {
canvasInRendering.add(this._canvas); canvasInRendering.add(this._canvas);
} }
if ( if (this._pdfBug && globalThis.StepperManager?.enabled) {
this._pdfBug &&
globalThis.StepperManager &&
globalThis.StepperManager.enabled
) {
this.stepper = globalThis.StepperManager.create(this._pageIndex); this.stepper = globalThis.StepperManager.create(this._pageIndex);
this.stepper.init(this.operatorList); this.stepper.init(this.operatorList);
this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint(); this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();

View File

@ -2242,7 +2242,7 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
} }
} }
if (glyph && glyph.compiled) { if (glyph?.compiled) {
glyph.compiled(ctx); glyph.compiled(ctx);
return; return;
} }

View File

@ -35,7 +35,7 @@ function createFetchOptions(headers, withCredentials, abortController) {
return { return {
method: "GET", method: "GET",
headers, headers,
signal: abortController && abortController.signal, signal: abortController?.signal,
mode: "cors", mode: "cors",
credentials: withCredentials ? "include" : "same-origin", credentials: withCredentials ? "include" : "same-origin",
redirect: "follow", redirect: "follow",
@ -251,7 +251,7 @@ class PDFFetchStreamRangeReader {
this._reader = response.body.getReader(); this._reader = response.body.getReader();
}) })
.catch(reason => { .catch(reason => {
if (reason && reason.name === "AbortError") { if (reason?.name === "AbortError") {
return; return;
} }
throw reason; throw reason;

View File

@ -121,9 +121,7 @@ class BaseFontLoader {
} }
get isFontLoadingAPISupported() { get isFontLoadingAPISupported() {
const supported = return shadow(this, "isFontLoadingAPISupported", !!this._document?.fonts);
typeof this._document !== "undefined" && !!this._document.fonts;
return shadow(this, "isFontLoadingAPISupported", supported);
} }
// eslint-disable-next-line getter-return // eslint-disable-next-line getter-return
@ -173,7 +171,7 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
const m = /Mozilla\/5.0.*?rv:(\d+).*? Gecko/.exec( const m = /Mozilla\/5.0.*?rv:(\d+).*? Gecko/.exec(
navigator.userAgent navigator.userAgent
); );
if (m && m[1] >= 14) { if (m?.[1] >= 14) {
supported = true; supported = true;
} }
// TODO - other browsers... // TODO - other browsers...

View File

@ -1015,8 +1015,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
const current = this.current; const current = this.current;
if ( if (
current.textRenderingMode & TextRenderingMode.ADD_TO_PATH_FLAG && current.textRenderingMode & TextRenderingMode.ADD_TO_PATH_FLAG &&
current.txtElement && current.txtElement?.hasChildNodes()
current.txtElement.hasChildNodes()
) { ) {
// If no glyphs are shown (i.e. no child nodes), no clipping occurs. // If no glyphs are shown (i.e. no child nodes), no clipping occurs.
current.element = current.txtElement; current.element = current.txtElement;

View File

@ -526,9 +526,7 @@ const renderTextLayer = (function renderTextLayerClosure() {
this._textDivs = textDivs || []; this._textDivs = textDivs || [];
this._textContentItemsStr = textContentItemsStr || []; this._textContentItemsStr = textContentItemsStr || [];
this._enhanceTextSelection = !!enhanceTextSelection; this._enhanceTextSelection = !!enhanceTextSelection;
this._fontInspectorEnabled = !!( this._fontInspectorEnabled = !!globalThis.FontInspector?.enabled;
globalThis.FontInspector && globalThis.FontInspector.enabled
);
this._reader = null; this._reader = null;
this._layoutTextLastFontSize = null; this._layoutTextLastFontSize = null;

View File

@ -27,7 +27,7 @@ class PDFDataTransportStream {
this._progressiveDone = params.progressiveDone || false; this._progressiveDone = params.progressiveDone || false;
const initialData = params.initialData; const initialData = params.initialData;
if (initialData && initialData.length > 0) { if (initialData?.length > 0) {
const buffer = new Uint8Array(initialData).buffer; const buffer = new Uint8Array(initialData).buffer;
this._queuedChunks.push(buffer); this._queuedChunks.push(buffer);
} }
@ -90,12 +90,12 @@ class PDFDataTransportStream {
if (evt.total === undefined) { if (evt.total === undefined) {
// Reporting to first range reader, if it exists. // Reporting to first range reader, if it exists.
const firstReader = this._rangeReaders[0]; const firstReader = this._rangeReaders[0];
if (firstReader && firstReader.onProgress) { if (firstReader?.onProgress) {
firstReader.onProgress({ loaded: evt.loaded }); firstReader.onProgress({ loaded: evt.loaded });
} }
} else { } else {
const fullReader = this._fullRequestReader; const fullReader = this._fullRequestReader;
if (fullReader && fullReader.onProgress) { if (fullReader?.onProgress) {
fullReader.onProgress({ loaded: evt.loaded, total: evt.total }); fullReader.onProgress({ loaded: evt.loaded, total: evt.total });
} }
} }

View File

@ -478,11 +478,11 @@ const WebGLUtils = (function WebGLUtilsClosure() {
drawFigures, drawFigures,
cleanup() { cleanup() {
if (smaskCache && smaskCache.canvas) { if (smaskCache?.canvas) {
smaskCache.canvas.width = 0; smaskCache.canvas.width = 0;
smaskCache.canvas.height = 0; smaskCache.canvas.height = 0;
} }
if (figuresCache && figuresCache.canvas) { if (figuresCache?.canvas) {
figuresCache.canvas.width = 0; figuresCache.canvas.width = 0;
figuresCache.canvas.height = 0; figuresCache.canvas.height = 0;
} }