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

Introduce more optional chaining in the src/core/ folder #16424

Merged
merged 1 commit into from
May 18, 2023
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
71 changes: 24 additions & 47 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,9 @@ class Annotation {
* @param {AnnotationStorage} [annotationStorage] - Storage for annotation
*/
mustBeViewed(annotationStorage) {
const storageEntry =
annotationStorage && annotationStorage.get(this.data.id);
if (storageEntry && storageEntry.hidden !== undefined) {
return !storageEntry.hidden;
const hidden = annotationStorage?.get(this.data.id)?.hidden;
if (hidden !== undefined) {
return !hidden;
}
return this.viewable && !this._hasFlag(this.flags, AnnotationFlag.HIDDEN);
}
Expand All @@ -578,10 +577,9 @@ class Annotation {
* @param {AnnotationStorage} [annotationStorage] - Storage for annotation
*/
mustBePrinted(annotationStorage) {
const storageEntry =
annotationStorage && annotationStorage.get(this.data.id);
if (storageEntry && storageEntry.print !== undefined) {
return storageEntry.print;
const print = annotationStorage?.get(this.data.id)?.print;
if (print !== undefined) {
return print;
}
return this.printable;
}
Expand Down Expand Up @@ -1566,8 +1564,7 @@ class WidgetAnnotation extends Annotation {

const localResources = getInheritableProperty({ dict, key: "DR" });
const acroFormResources = params.acroForm.get("DR");
const appearanceResources =
this.appearance && this.appearance.dict.get("Resources");
const appearanceResources = this.appearance?.dict.get("Resources");

this._fieldResources = {
localResources,
Expand Down Expand Up @@ -1627,10 +1624,7 @@ class WidgetAnnotation extends Annotation {
}

getRotationMatrix(annotationStorage) {
const storageEntry = annotationStorage
? annotationStorage.get(this.data.id)
: undefined;
let rotation = storageEntry && storageEntry.rotation;
let rotation = annotationStorage?.get(this.data.id)?.rotation;
if (rotation === undefined) {
rotation = this.rotation;
}
Expand All @@ -1646,10 +1640,7 @@ class WidgetAnnotation extends Annotation {
}

getBorderAndBackgroundAppearances(annotationStorage) {
const storageEntry = annotationStorage
? annotationStorage.get(this.data.id)
: undefined;
let rotation = storageEntry && storageEntry.rotation;
let rotation = annotationStorage?.get(this.data.id)?.rotation;
if (rotation === undefined) {
rotation = this.rotation;
}
Expand Down Expand Up @@ -1799,11 +1790,9 @@ class WidgetAnnotation extends Annotation {
amendSavedDict(annotationStorage, dict) {}

async save(evaluator, task, annotationStorage) {
const storageEntry = annotationStorage
? annotationStorage.get(this.data.id)
: undefined;
let value = storageEntry && storageEntry.value;
let rotation = storageEntry && storageEntry.rotation;
const storageEntry = annotationStorage?.get(this.data.id);
let value = storageEntry?.value,
rotation = storageEntry?.rotation;
if (value === this.data.fieldValue || value === undefined) {
if (!this._hasValueFromXFA && rotation === undefined) {
return null;
Expand Down Expand Up @@ -1845,7 +1834,7 @@ class WidgetAnnotation extends Annotation {
}

let needAppearances = false;
if (appearance && appearance.needAppearances) {
if (appearance?.needAppearances) {
needAppearances = true;
appearance = null;
}
Expand Down Expand Up @@ -1949,10 +1938,7 @@ class WidgetAnnotation extends Annotation {
if (isPassword) {
return null;
}
const storageEntry = annotationStorage
? annotationStorage.get(this.data.id)
: undefined;

const storageEntry = annotationStorage?.get(this.data.id);
let value, rotation;
if (storageEntry) {
value = storageEntry.formattedValue || storageEntry.value;
Expand Down Expand Up @@ -1993,7 +1979,7 @@ class WidgetAnnotation extends Annotation {
const option = this.data.options.find(
({ exportValue }) => value === exportValue
);
value = (option && option.displayValue) || value;
value = option?.displayValue || value;
}

if (value === "") {
Expand Down Expand Up @@ -2383,9 +2369,7 @@ class WidgetAnnotation extends Annotation {
const { localResources, appearanceResources, acroFormResources } =
this._fieldResources;

const fontName =
this.data.defaultAppearanceData &&
this.data.defaultAppearanceData.fontName;
const fontName = this.data.defaultAppearanceData?.fontName;
if (!fontName) {
return localResources || Dict.empty;
}
Expand Down Expand Up @@ -2763,8 +2747,8 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
return null;
}
const storageEntry = annotationStorage.get(this.data.id);
timvandermeij marked this conversation as resolved.
Show resolved Hide resolved
let rotation = storageEntry && storageEntry.rotation;
let value = storageEntry && storageEntry.value;
let rotation = storageEntry?.rotation,
value = storageEntry?.value;

if (rotation === undefined) {
if (value === undefined) {
Expand Down Expand Up @@ -2825,8 +2809,8 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
return null;
}
const storageEntry = annotationStorage.get(this.data.id);
timvandermeij marked this conversation as resolved.
Show resolved Hide resolved
let rotation = storageEntry && storageEntry.rotation;
let value = storageEntry && storageEntry.value;
let rotation = storageEntry?.rotation,
value = storageEntry?.value;

if (rotation === undefined) {
if (value === undefined) {
Expand Down Expand Up @@ -3239,10 +3223,7 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
if (!this.hasIndices) {
return;
}
const storageEntry = annotationStorage
? annotationStorage.get(this.data.id)
: undefined;
let values = storageEntry && storageEntry.value;
let values = annotationStorage?.get(this.data.id)?.value;
if (!Array.isArray(values)) {
values = [values];
}
Expand All @@ -3263,10 +3244,7 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
}

let exportedValue, rotation;
const storageEntry = annotationStorage
? annotationStorage.get(this.data.id)
: undefined;

const storageEntry = annotationStorage?.get(this.data.id);
if (storageEntry) {
rotation = storageEntry.rotation;
exportedValue = storageEntry.value;
Expand Down Expand Up @@ -4176,10 +4154,9 @@ class HighlightAnnotation extends MarkupAnnotation {

const quadPoints = (this.data.quadPoints = getQuadPoints(dict, null));
if (quadPoints) {
const resources =
this.appearance && this.appearance.dict.get("Resources");
const resources = this.appearance?.dict.get("Resources");

if (!this.appearance || !(resources && resources.has("ExtGState"))) {
if (!this.appearance || !resources?.has("ExtGState")) {
if (this.appearance) {
// Workaround for cases where there's no /ExtGState-entry directly
// available, e.g. when the appearance stream contains a /XObject of
Expand Down
8 changes: 4 additions & 4 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ class Catalog {

let metadata = null;
try {
const suppressEncryption = !(
this.xref.encrypt && this.xref.encrypt.encryptMetadata
const stream = this.xref.fetch(
streamRef,
/* suppressEncryption = */ !this.xref.encrypt?.encryptMetadata
);
const stream = this.xref.fetch(streamRef, suppressEncryption);

if (stream instanceof BaseStream && stream.dict instanceof Dict) {
const type = stream.dict.get("Type");
Expand Down Expand Up @@ -616,7 +616,7 @@ class Catalog {
*/
_readDests() {
const obj = this._catDict.get("Names");
if (obj && obj.has("Dests")) {
if (obj?.has("Dests")) {
return new NameTree(obj.getRaw("Dests"), this.xref);
} else if (this._catDict.has("Dests")) {
// Simple destination dictionary.
Expand Down
2 changes: 1 addition & 1 deletion src/core/ccitt.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ class CCITTFaxDecoder {
if (this.eoblock) {
code = this._lookBits(7);
p = twoDimTable[code];
if (p && p[0] > 0) {
if (p?.[0] > 0) {
this._eatBits(p[0]);
return p[1];
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/cff_font.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CFFFont {

if (properties.composite) {
let invCidToGidMap;
if (cidToGidMap && cidToGidMap.length > 0) {
if (cidToGidMap?.length > 0) {
invCidToGidMap = Object.create(null);
for (let i = 0, ii = cidToGidMap.length; i < ii; i++) {
const gid = cidToGidMap[i];
Expand All @@ -74,7 +74,7 @@ class CFFFont {
const cid = charsets[glyphId];
charCode = cMap.charCodeOf(cid);

if (invCidToGidMap && invCidToGidMap[charCode] !== undefined) {
if (invCidToGidMap?.[charCode] !== undefined) {
// According to the PDF specification, see Table 117, it's not clear
// that a /CIDToGIDMap should be used with any non-TrueType fonts,
// however it's necessary to do so in order to fix issue 15559.
Expand Down
2 changes: 1 addition & 1 deletion src/core/cff_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ class CFFCompiler {
}

const xuid = cff.topDict.getByName("XUID");
if (xuid && xuid.length > 16) {
if (xuid?.length > 16) {
// Length of XUID array must not be greater than 16 (issue #12399).
cff.topDict.removeByName("XUID");
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ function recoverJsURL(str) {
);

const jsUrl = regex.exec(str);
if (jsUrl && jsUrl[2]) {
if (jsUrl?.[2]) {
const url = jsUrl[2];
let newWindow = false;

Expand Down
2 changes: 1 addition & 1 deletion src/core/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ const CipherTransformFactory = (function CipherTransformFactoryClosure() {
if (cfDict instanceof Dict && streamCryptoName instanceof Name) {
cfDict.suppressEncryption = true; // See comment below.
const handlerDict = cfDict.get(streamCryptoName.name);
keyLength = (handlerDict && handlerDict.get("Length")) || 128;
keyLength = handlerDict?.get("Length") || 128;
if (keyLength < 40) {
// Sometimes it's incorrect value of bits, generators specify
// bytes.
Expand Down
2 changes: 1 addition & 1 deletion src/core/dataset_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class DatasetReader {
}

const first = node.firstChild;
if (first && first.nodeName === "value") {
if (first?.nodeName === "value") {
return node.children.map(child => decodeString(child.textContent));
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/decode_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class StreamsSequenceStream extends DecodeStream {
chunk = stream.getBytes();
} catch (reason) {
if (this._onError) {
this._onError(reason, stream.dict && stream.dict.objId);
this._onError(reason, stream.dict?.objId);
return;
}
throw reason;
Expand Down
2 changes: 1 addition & 1 deletion src/core/decrypt_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DecryptStream extends DecodeStream {
return;
}
this.nextChunk = this.str.getBytes(chunkSize);
const hasMoreData = this.nextChunk && this.nextChunk.length > 0;
const hasMoreData = this.nextChunk?.length > 0;

const decrypt = this.decrypt;
chunk = decrypt(chunk, !hasMoreData);
Expand Down
6 changes: 3 additions & 3 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ class PDFDocument {
const { catalog, linearization, xref } = this;
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert(
linearization && linearization.pageFirst === pageIndex,
linearization?.pageFirst === pageIndex,
"_getLinearizationPage - invalid pageIndex argument."
);
}
Expand Down Expand Up @@ -1466,7 +1466,7 @@ class PDFDocument {
let promise;
if (xfaFactory) {
promise = Promise.resolve([Dict.empty, null]);
} else if (linearization && linearization.pageFirst === pageIndex) {
} else if (linearization?.pageFirst === pageIndex) {
promise = this._getLinearizationPage(pageIndex);
} else {
promise = catalog.getPageDict(pageIndex);
Expand Down Expand Up @@ -1630,7 +1630,7 @@ class PDFDocument {
this._localIdFactory,
/* collectFields */ true
)
.then(annotation => annotation && annotation.getFieldObject())
.then(annotation => annotation?.getFieldObject())
.catch(function (reason) {
warn(`_collectFieldObjects: "${reason}".`);
return null;
Expand Down
Loading