1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 01:58:06 +02:00

Prefer instanceof Dict rather than calling isDict() with one argument

Unless you actually need to check that something is both a `Dict` and also of the *correct* type, using `instanceof Dict` directly should be a tiny bit more efficient since it avoids one function call and an unnecessary `undefined` check.

This patch uses ESLint to enforce this, since we obviously still want to keep the `isDict` helper function for where it makes sense.
This commit is contained in:
Jonas Jenwald 2022-02-21 12:44:56 +01:00
parent 67b658e8d5
commit 4df82ad31e
18 changed files with 83 additions and 93 deletions

View file

@ -39,7 +39,7 @@ import {
createDefaultAppearance,
parseDefaultAppearance,
} from "./default_appearance.js";
import { Dict, isDict, isName, Name, Ref, RefSet } from "./primitives.js";
import { Dict, isName, Name, Ref, RefSet } from "./primitives.js";
import { BaseStream } from "./base_stream.js";
import { bidi } from "./bidi.js";
import { Catalog } from "./catalog.js";
@ -95,7 +95,7 @@ class AnnotationFactory {
pageIndex = -1
) {
const dict = xref.fetchIfRef(ref);
if (!isDict(dict)) {
if (!(dict instanceof Dict)) {
return undefined;
}
@ -209,7 +209,7 @@ class AnnotationFactory {
static async _getPageIndex(xref, ref, pdfManager) {
try {
const annotDict = await xref.fetchIfRefAsync(ref);
if (!isDict(annotDict)) {
if (!(annotDict instanceof Dict)) {
return -1;
}
const pageRef = annotDict.getRaw("P");
@ -636,7 +636,7 @@ class Annotation {
}
this.borderStyle = new AnnotationBorderStyle();
if (!isDict(borderStyle)) {
if (!(borderStyle instanceof Dict)) {
return;
}
if (borderStyle.has("BS")) {
@ -681,7 +681,7 @@ class Annotation {
this.appearance = null;
const appearanceStates = dict.get("AP");
if (!isDict(appearanceStates)) {
if (!(appearanceStates instanceof Dict)) {
return;
}
@ -691,7 +691,7 @@ class Annotation {
this.appearance = normalAppearanceState;
return;
}
if (!isDict(normalAppearanceState)) {
if (!(normalAppearanceState instanceof Dict)) {
return;
}
@ -1418,7 +1418,7 @@ class WidgetAnnotation extends Annotation {
const { xref } = evaluator;
const dict = xref.fetchIfRef(this.ref);
if (!isDict(dict)) {
if (!(dict instanceof Dict)) {
return null;
}
@ -2085,7 +2085,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}
const dict = evaluator.xref.fetchIfRef(this.ref);
if (!isDict(dict)) {
if (!(dict instanceof Dict)) {
return null;
}
@ -2131,7 +2131,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}
const dict = evaluator.xref.fetchIfRef(this.ref);
if (!isDict(dict)) {
if (!(dict instanceof Dict)) {
return null;
}
@ -2158,7 +2158,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
parentBuffer = [`${this.parent.num} ${this.parent.gen} obj\n`];
writeDict(parent, parentBuffer, parentTransform);
parentBuffer.push("\nendobj\n");
} else if (isDict(this.parent)) {
} else if (this.parent instanceof Dict) {
this.parent.set("V", name);
}
}
@ -2250,12 +2250,12 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
_processCheckBox(params) {
const customAppearance = params.dict.get("AP");
if (!isDict(customAppearance)) {
if (!(customAppearance instanceof Dict)) {
return;
}
const normalAppearance = customAppearance.get("N");
if (!isDict(normalAppearance)) {
if (!(normalAppearance instanceof Dict)) {
return;
}
@ -2318,7 +2318,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
// The parent field's `V` entry holds a `Name` object with the appearance
// state of whichever child field is currently in the "on" state.
const fieldParent = params.dict.get("Parent");
if (isDict(fieldParent)) {
if (fieldParent instanceof Dict) {
this.parent = params.dict.getRaw("Parent");
const fieldParentValue = fieldParent.get("V");
if (isName(fieldParentValue)) {
@ -2328,11 +2328,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
// The button's value corresponds to its appearance state.
const appearanceStates = params.dict.get("AP");
if (!isDict(appearanceStates)) {
if (!(appearanceStates instanceof Dict)) {
return;
}
const normalAppearance = appearanceStates.get("N");
if (!isDict(normalAppearance)) {
if (!(normalAppearance instanceof Dict)) {
return;
}
for (const key of normalAppearance.getKeys()) {