Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1566,9 +1566,7 @@ class PDFPageProxy {
return;
}
}
intentState.streamReader.cancel(
new AbortException(reason && reason.message)
);
intentState.streamReader.cancel(new AbortException(reason?.message));
intentState.streamReader = null;

if (this._transport.destroyed) {
Expand Down Expand Up @@ -1615,8 +1613,7 @@ class LoopbackPort {
let buffer, result;
if ((buffer = value.buffer) && isArrayBuffer(buffer)) {
// We found object with ArrayBuffer (typed array).
const transferable = transfers && transfers.includes(buffer);
if (transferable) {
if (transfers?.includes(buffer)) {
result = new value.constructor(
buffer,
value.byteOffset,
Expand Down Expand Up @@ -1712,8 +1709,7 @@ const PDFWorker = (function PDFWorkerClosure() {
fallbackWorkerSrc = "./pdf.worker.js";
}
} else if (typeof document === "object" && "currentScript" in document) {
const pdfjsFilePath =
document.currentScript && document.currentScript.src;
const pdfjsFilePath = document.currentScript?.src;
if (pdfjsFilePath) {
fallbackWorkerSrc = pdfjsFilePath.replace(
/(\.(?:min\.)?js)(\?.*)?$/i,
Expand All @@ -1739,8 +1735,7 @@ const PDFWorker = (function PDFWorkerClosure() {
function getMainThreadWorkerMessageHandler() {
let mainWorkerMessageHandler;
try {
mainWorkerMessageHandler =
globalThis.pdfjsWorker && globalThis.pdfjsWorker.WorkerMessageHandler;
mainWorkerMessageHandler = globalThis.pdfjsWorker?.WorkerMessageHandler;
} catch (ex) {
/* Ignore errors. */
}
Expand Down Expand Up @@ -2378,11 +2373,7 @@ class WorkerTransport {
}

let fontRegistry = null;
if (
params.pdfBug &&
globalThis.FontInspector &&
globalThis.FontInspector.enabled
) {
if (params.pdfBug && globalThis.FontInspector?.enabled) {
fontRegistry = {
registerFont(font, url) {
globalThis.FontInspector.fontAdded(font, url);
Expand Down Expand Up @@ -2441,11 +2432,7 @@ class WorkerTransport {

// Heuristic that will allow us not to store large data.
const MAX_IMAGE_SIZE_TO_STORE = 8000000;
if (
imageData &&
"data" in imageData &&
imageData.data.length > MAX_IMAGE_SIZE_TO_STORE
) {
if (imageData?.data?.length > MAX_IMAGE_SIZE_TO_STORE) {
pageProxy.cleanupAfterRender = true;
}
break;
Expand Down Expand Up @@ -2868,11 +2855,7 @@ const InternalRenderTask = (function InternalRenderTaskClosure() {
canvasInRendering.add(this._canvas);
}

if (
this._pdfBug &&
globalThis.StepperManager &&
globalThis.StepperManager.enabled
) {
if (this._pdfBug && globalThis.StepperManager?.enabled) {
this.stepper = globalThis.StepperManager.create(this._pageIndex);
this.stepper.init(this.operatorList);
this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
Expand Down
2 changes: 1 addition & 1 deletion src/display/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,7 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
}
}

if (glyph && glyph.compiled) {
if (glyph?.compiled) {
glyph.compiled(ctx);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/display/fetch_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function createFetchOptions(headers, withCredentials, abortController) {
return {
method: "GET",
headers,
signal: abortController && abortController.signal,
signal: abortController?.signal,
mode: "cors",
credentials: withCredentials ? "include" : "same-origin",
redirect: "follow",
Expand Down Expand Up @@ -251,7 +251,7 @@ class PDFFetchStreamRangeReader {
this._reader = response.body.getReader();
})
.catch(reason => {
if (reason && reason.name === "AbortError") {
if (reason?.name === "AbortError") {
return;
}
throw reason;
Expand Down
6 changes: 2 additions & 4 deletions src/display/font_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ class BaseFontLoader {
}

get isFontLoadingAPISupported() {
const supported =
typeof this._document !== "undefined" && !!this._document.fonts;
return shadow(this, "isFontLoadingAPISupported", supported);
return shadow(this, "isFontLoadingAPISupported", !!this._document?.fonts);
}

// eslint-disable-next-line getter-return
Expand Down Expand Up @@ -173,7 +171,7 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
const m = /Mozilla\/5.0.*?rv:(\d+).*? Gecko/.exec(
navigator.userAgent
);
if (m && m[1] >= 14) {
if (m?.[1] >= 14) {
supported = true;
}
// TODO - other browsers...
Expand Down
3 changes: 1 addition & 2 deletions src/display/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -1015,8 +1015,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
const current = this.current;
if (
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.
current.element = current.txtElement;
Expand Down
4 changes: 1 addition & 3 deletions src/display/text_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,7 @@ const renderTextLayer = (function renderTextLayerClosure() {
this._textDivs = textDivs || [];
this._textContentItemsStr = textContentItemsStr || [];
this._enhanceTextSelection = !!enhanceTextSelection;
this._fontInspectorEnabled = !!(
globalThis.FontInspector && globalThis.FontInspector.enabled
);
this._fontInspectorEnabled = !!globalThis.FontInspector?.enabled;

this._reader = null;
this._layoutTextLastFontSize = null;
Expand Down
6 changes: 3 additions & 3 deletions src/display/transport_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PDFDataTransportStream {
this._progressiveDone = params.progressiveDone || false;

const initialData = params.initialData;
if (initialData && initialData.length > 0) {
if (initialData?.length > 0) {
const buffer = new Uint8Array(initialData).buffer;
this._queuedChunks.push(buffer);
}
Expand Down Expand Up @@ -90,12 +90,12 @@ class PDFDataTransportStream {
if (evt.total === undefined) {
// Reporting to first range reader, if it exists.
const firstReader = this._rangeReaders[0];
if (firstReader && firstReader.onProgress) {
if (firstReader?.onProgress) {
firstReader.onProgress({ loaded: evt.loaded });
}
} else {
const fullReader = this._fullRequestReader;
if (fullReader && fullReader.onProgress) {
if (fullReader?.onProgress) {
fullReader.onProgress({ loaded: evt.loaded, total: evt.total });
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/display/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,11 @@ const WebGLUtils = (function WebGLUtilsClosure() {
drawFigures,

cleanup() {
if (smaskCache && smaskCache.canvas) {
if (smaskCache?.canvas) {
smaskCache.canvas.width = 0;
smaskCache.canvas.height = 0;
}
if (figuresCache && figuresCache.canvas) {
if (figuresCache?.canvas) {
figuresCache.canvas.width = 0;
figuresCache.canvas.height = 0;
}
Expand Down