From 4df82ad31eb6598fbbb1301ff0a45cb89700a324 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 21 Feb 2022 12:44:56 +0100 Subject: [PATCH] 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. --- .eslintrc | 4 ++++ src/core/annotation.js | 30 ++++++++++++++-------------- src/core/catalog.js | 38 ++++++++++++++++++------------------ src/core/ccitt_stream.js | 4 ++-- src/core/colorspace.js | 6 +++--- src/core/crypto.js | 6 +++--- src/core/document.js | 6 +++--- src/core/evaluator.js | 29 ++++++++++----------------- src/core/file_spec.js | 4 ++-- src/core/function.js | 4 ++-- src/core/jbig2_stream.js | 4 ++-- src/core/jpeg_stream.js | 4 ++-- src/core/name_number_tree.js | 4 ++-- src/core/parser.js | 13 ++---------- src/core/predictor_stream.js | 4 ++-- src/core/struct_tree.js | 8 ++++---- src/core/writer.js | 4 ++-- test/unit/primitives_spec.js | 4 ++++ 18 files changed, 83 insertions(+), 93 deletions(-) diff --git a/.eslintrc b/.eslintrc index 37f9f057b..c38de3598 100644 --- a/.eslintrc +++ b/.eslintrc @@ -183,6 +183,10 @@ "selector": "CallExpression[callee.name='isCmd'][arguments.length<2]", "message": "Use `instanceof Cmd` rather than `isCmd()` with one argument.", }, + { + "selector": "CallExpression[callee.name='isDict'][arguments.length<2]", + "message": "Use `instanceof Dict` rather than `isDict()` with one argument.", + }, { "selector": "NewExpression[callee.name='Cmd']", "message": "Use `Cmd.get()` rather than `new Cmd()`.", diff --git a/src/core/annotation.js b/src/core/annotation.js index 0e6792811..4ee78145f 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -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()) { diff --git a/src/core/catalog.js b/src/core/catalog.js index 662f4012f..9ed56f017 100644 --- a/src/core/catalog.js +++ b/src/core/catalog.js @@ -120,7 +120,7 @@ class Catalog { let collection = null; try { const obj = this._catDict.get("Collection"); - if (isDict(obj) && obj.size > 0) { + if (obj instanceof Dict && obj.size > 0) { collection = obj; } } catch (ex) { @@ -136,7 +136,7 @@ class Catalog { let acroForm = null; try { const obj = this._catDict.get("AcroForm"); - if (isDict(obj) && obj.size > 0) { + if (obj instanceof Dict && obj.size > 0) { acroForm = obj; } } catch (ex) { @@ -208,7 +208,7 @@ class Catalog { */ _readMarkInfo() { const obj = this._catDict.get("MarkInfo"); - if (!isDict(obj)) { + if (!(obj instanceof Dict)) { return null; } @@ -249,7 +249,7 @@ class Catalog { */ _readStructTreeRoot() { const obj = this._catDict.get("StructTreeRoot"); - if (!isDict(obj)) { + if (!(obj instanceof Dict)) { return null; } const root = new StructTreeRoot(obj); @@ -259,7 +259,7 @@ class Catalog { get toplevelPagesDict() { const pagesObj = this._catDict.get("Pages"); - if (!isDict(pagesObj)) { + if (!(pagesObj instanceof Dict)) { throw new FormatError("Invalid top-level pages dictionary."); } return shadow(this, "toplevelPagesDict", pagesObj); @@ -283,7 +283,7 @@ class Catalog { */ _readDocumentOutline() { let obj = this._catDict.get("Outlines"); - if (!isDict(obj)) { + if (!(obj instanceof Dict)) { return null; } obj = obj.getRaw("First"); @@ -376,7 +376,7 @@ class Catalog { */ _readPermissions() { const encrypt = this.xref.trailer.get("Encrypt"); - if (!isDict(encrypt)) { + if (!(encrypt instanceof Dict)) { return null; } @@ -654,7 +654,7 @@ class Catalog { const labelDict = nums.get(i); if (labelDict !== undefined) { - if (!isDict(labelDict)) { + if (!(labelDict instanceof Dict)) { throw new FormatError("PageLabel is not a dictionary."); } @@ -799,7 +799,7 @@ class Catalog { const obj = this._catDict.get("ViewerPreferences"); let prefs = null; - if (isDict(obj)) { + if (obj instanceof Dict) { for (const key in ViewerPreferencesValidators) { if (!obj.has(key)) { continue; @@ -921,7 +921,7 @@ class Catalog { const obj = this._catDict.get("OpenAction"); const openAction = Object.create(null); - if (isDict(obj)) { + if (obj instanceof Dict) { // Convert the OpenAction dictionary into a format that works with // `parseDestDictionary`, to avoid having to re-implement those checks. const destDict = new Dict(this.xref); @@ -1337,7 +1337,7 @@ class Catalog { if ( isRefsEqual(kidRef, pageRef) && !isDict(node, "Page") && - !(isDict(node) && !node.has("Type") && node.has("Contents")) + !(node instanceof Dict && !node.has("Type") && node.has("Contents")) ) { throw new FormatError( "The reference does not point to a /Page dictionary." @@ -1346,7 +1346,7 @@ class Catalog { if (!node) { return null; } - if (!isDict(node)) { + if (!(node instanceof Dict)) { throw new FormatError("Node must be a dictionary."); } parentRef = node.getRaw("Parent"); @@ -1356,7 +1356,7 @@ class Catalog { if (!parent) { return null; } - if (!isDict(parent)) { + if (!(parent instanceof Dict)) { throw new FormatError("Parent must be a dictionary."); } return parent.getAsync("Kids"); @@ -1379,7 +1379,7 @@ class Catalog { } kidPromises.push( xref.fetchAsync(kid).then(function (obj) { - if (!isDict(obj)) { + if (!(obj instanceof Dict)) { throw new FormatError("Kid node must be a dictionary."); } if (obj.has("Count")) { @@ -1430,7 +1430,7 @@ class Catalog { */ static parseDestDictionary(params) { const destDict = params.destDict; - if (!isDict(destDict)) { + if (!(destDict instanceof Dict)) { warn("parseDestDictionary: `destDict` must be a dictionary."); return; } @@ -1444,14 +1444,14 @@ class Catalog { let action = destDict.get("A"), url, dest; - if (!isDict(action)) { + if (!(action instanceof Dict)) { if (destDict.has("Dest")) { // A /Dest entry should *only* contain a Name or an Array, but some bad // PDF generators ignore that and treat it as an /A entry. action = destDict.get("Dest"); } else { action = destDict.get("AA"); - if (isDict(action)) { + if (action instanceof Dict) { if (action.has("D")) { // MouseDown action = action.get("D"); @@ -1463,7 +1463,7 @@ class Catalog { } } - if (isDict(action)) { + if (action instanceof Dict) { const actionType = action.get("S"); if (!isName(actionType)) { warn("parseDestDictionary: Invalid type in Action dictionary."); @@ -1508,7 +1508,7 @@ class Catalog { case "GoToR": const urlDict = action.get("F"); - if (isDict(urlDict)) { + if (urlDict instanceof Dict) { // We assume that we found a FileSpec dictionary // and fetch the URL without checking any further. url = urlDict.get("F") || null; diff --git a/src/core/ccitt_stream.js b/src/core/ccitt_stream.js index d716b4841..0e3b1cea3 100644 --- a/src/core/ccitt_stream.js +++ b/src/core/ccitt_stream.js @@ -13,9 +13,9 @@ * limitations under the License. */ -import { Dict, isDict } from "./primitives.js"; import { CCITTFaxDecoder } from "./ccitt.js"; import { DecodeStream } from "./decode_stream.js"; +import { Dict } from "./primitives.js"; class CCITTFaxStream extends DecodeStream { constructor(str, maybeLength, params) { @@ -24,7 +24,7 @@ class CCITTFaxStream extends DecodeStream { this.str = str; this.dict = str.dict; - if (!isDict(params)) { + if (!(params instanceof Dict)) { params = Dict.empty; } diff --git a/src/core/colorspace.js b/src/core/colorspace.js index 5dc8fc681..1a5d2826a 100644 --- a/src/core/colorspace.js +++ b/src/core/colorspace.js @@ -21,7 +21,7 @@ import { unreachable, warn, } from "../shared/util.js"; -import { isDict, isName, Name, Ref } from "./primitives.js"; +import { Dict, isName, Name, Ref } from "./primitives.js"; import { BaseStream } from "./base_stream.js"; import { MissingDataException } from "./core_utils.js"; @@ -392,9 +392,9 @@ class ColorSpace { case "Pattern": return new PatternCS(/* baseCS = */ null); default: - if (isDict(resources)) { + if (resources instanceof Dict) { const colorSpaces = resources.get("ColorSpace"); - if (isDict(colorSpaces)) { + if (colorSpaces instanceof Dict) { const resourcesCS = colorSpaces.get(cs.name); if (resourcesCS) { if (isName(resourcesCS)) { diff --git a/src/core/crypto.js b/src/core/crypto.js index d76b6f483..f41831824 100644 --- a/src/core/crypto.js +++ b/src/core/crypto.js @@ -24,7 +24,7 @@ import { utf8StringToString, warn, } from "../shared/util.js"; -import { isDict, isName, Name } from "./primitives.js"; +import { Dict, isName, Name } from "./primitives.js"; import { DecryptStream } from "./decrypt_stream.js"; class ARCFourCipher { @@ -1713,7 +1713,7 @@ const CipherTransformFactory = (function CipherTransformFactoryClosure() { // Trying to find default handler -- it usually has Length. const cfDict = dict.get("CF"); const streamCryptoName = dict.get("StmF"); - if (isDict(cfDict) && isName(streamCryptoName)) { + if (cfDict instanceof Dict && isName(streamCryptoName)) { cfDict.suppressEncryption = true; // See comment below. const handlerDict = cfDict.get(streamCryptoName.name); keyLength = (handlerDict && handlerDict.get("Length")) || 128; @@ -1838,7 +1838,7 @@ const CipherTransformFactory = (function CipherTransformFactoryClosure() { if (algorithm >= 4) { const cf = dict.get("CF"); - if (isDict(cf)) { + if (cf instanceof Dict) { // The 'CF' dictionary itself should not be encrypted, and by setting // `suppressEncryption` we can prevent an infinite loop inside of // `XRef_fetchUncompressed` if the dictionary contains indirect diff --git a/src/core/document.js b/src/core/document.js index a11b75365..cf30f5555 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -44,7 +44,7 @@ import { XRefEntryException, XRefParseException, } from "./core_utils.js"; -import { Dict, isDict, isName, Name, Ref } from "./primitives.js"; +import { Dict, isName, Name, Ref } from "./primitives.js"; import { getXfaFontDict, getXfaFontName } from "./xfa_fonts.js"; import { NullStream, Stream } from "./stream.js"; import { AnnotationFactory } from "./annotation.js"; @@ -120,7 +120,7 @@ class Page { if (!Array.isArray(value)) { return value; } - if (value.length === 1 || !isDict(value[0])) { + if (value.length === 1 || !(value[0] instanceof Dict)) { return value[0]; } return Dict.merge({ xref: this.xref, dictArray: value }); @@ -1175,7 +1175,7 @@ class PDFDocument { info("The document information dictionary is invalid."); } - if (isDict(infoDict)) { + if (infoDict instanceof Dict) { // Fill the document info with valid entries from the specification, // as well as any existing well-formed custom entries. for (const key of infoDict.getKeys()) { diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 47bc38133..7f5e7b911 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -35,16 +35,7 @@ import { warn, } from "../shared/util.js"; import { CMapFactory, IdentityCMap } from "./cmap.js"; -import { - Cmd, - Dict, - EOF, - isDict, - isName, - Name, - Ref, - RefSet, -} from "./primitives.js"; +import { Cmd, Dict, EOF, isName, Name, Ref, RefSet } from "./primitives.js"; import { ErrorFont, Font } from "./fonts.js"; import { FontFlags, getFontType } from "./fonts_utils.js"; import { @@ -1042,7 +1033,7 @@ class PartialEvaluator { gStateObj.push([key, false]); break; } - if (isDict(value)) { + if (value instanceof Dict) { isSimpleGState = false; promise = promise.then(() => { @@ -1162,7 +1153,7 @@ class PartialEvaluator { } font = xref.fetchIfRef(fontRef); - if (!isDict(font)) { + if (!(font instanceof Dict)) { return errorFont(); } @@ -1190,7 +1181,7 @@ class PartialEvaluator { fontID = `f${fontRef.toString()}`; } - if (hash && isDict(descriptor)) { + if (hash && descriptor instanceof Dict) { if (!descriptor.fontAliases) { descriptor.fontAliases = Object.create(null); } @@ -1493,7 +1484,7 @@ class PartialEvaluator { if (isName(contentProperties)) { const properties = resources.get("Properties"); optionalContent = properties.get(contentProperties.name); - } else if (isDict(contentProperties)) { + } else if (contentProperties instanceof Dict) { optionalContent = contentProperties; } else { throw new FormatError("Optional content properties malformed."); @@ -1521,7 +1512,7 @@ class PartialEvaluator { const optionalContentGroups = optionalContent.get("OCGs"); if ( Array.isArray(optionalContentGroups) || - isDict(optionalContentGroups) + optionalContentGroups instanceof Dict ) { const groupIds = []; if (Array.isArray(optionalContentGroups)) { @@ -3133,7 +3124,7 @@ class PartialEvaluator { if (includeMarkedContent) { flushTextContentItem(); let mcid = null; - if (isDict(args[1])) { + if (args[1] instanceof Dict) { mcid = args[1].get("MCID"); } textContent.items.push({ @@ -3197,7 +3188,7 @@ class PartialEvaluator { if (properties.composite) { // CIDSystemInfo helps to match CID to glyphs const cidSystemInfo = dict.get("CIDSystemInfo"); - if (isDict(cidSystemInfo)) { + if (cidSystemInfo instanceof Dict) { properties.cidSystemInfo = { registry: stringToPDFString(cidSystemInfo.get("Registry")), ordering: stringToPDFString(cidSystemInfo.get("Ordering")), @@ -3222,7 +3213,7 @@ class PartialEvaluator { let encoding; if (dict.has("Encoding")) { encoding = dict.get("Encoding"); - if (isDict(encoding)) { + if (encoding instanceof Dict) { baseEncodingName = encoding.get("BaseEncoding"); baseEncodingName = isName(baseEncodingName) ? baseEncodingName.name @@ -3784,7 +3775,7 @@ class PartialEvaluator { hash.update(encoding.name); } else if (encoding instanceof Ref) { hash.update(encoding.toString()); - } else if (isDict(encoding)) { + } else if (encoding instanceof Dict) { for (const entry of encoding.getRawValues()) { if (isName(entry)) { hash.update(entry.name); diff --git a/src/core/file_spec.js b/src/core/file_spec.js index 5d0dda9f4..3eb1c5a7d 100644 --- a/src/core/file_spec.js +++ b/src/core/file_spec.js @@ -15,7 +15,7 @@ import { stringToPDFString, warn } from "../shared/util.js"; import { BaseStream } from "./base_stream.js"; -import { isDict } from "./primitives.js"; +import { Dict } from "./primitives.js"; function pickPlatformItem(dict) { // Look for the filename in this order: @@ -43,7 +43,7 @@ function pickPlatformItem(dict) { */ class FileSpec { constructor(root, xref) { - if (!root || !isDict(root)) { + if (!(root instanceof Dict)) { return; } this.xref = xref; diff --git a/src/core/function.js b/src/core/function.js index 05a2e317d..d402d067b 100644 --- a/src/core/function.js +++ b/src/core/function.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import { Dict, isDict, Ref } from "./primitives.js"; +import { Dict, Ref } from "./primitives.js"; import { FormatError, info, @@ -508,7 +508,7 @@ function isPDFFunction(v) { let fnDict; if (typeof v !== "object") { return false; - } else if (isDict(v)) { + } else if (v instanceof Dict) { fnDict = v; } else if (v instanceof BaseStream) { fnDict = v.dict; diff --git a/src/core/jbig2_stream.js b/src/core/jbig2_stream.js index a83d6a9b1..bbea4c539 100644 --- a/src/core/jbig2_stream.js +++ b/src/core/jbig2_stream.js @@ -15,7 +15,7 @@ import { BaseStream } from "./base_stream.js"; import { DecodeStream } from "./decode_stream.js"; -import { isDict } from "./primitives.js"; +import { Dict } from "./primitives.js"; import { Jbig2Image } from "./jbig2.js"; import { shadow } from "../shared/util.js"; @@ -50,7 +50,7 @@ class Jbig2Stream extends DecodeStream { const jbig2Image = new Jbig2Image(); const chunks = []; - if (isDict(this.params)) { + if (this.params instanceof Dict) { const globalsStream = this.params.get("JBIG2Globals"); if (globalsStream instanceof BaseStream) { const globals = globalsStream.getBytes(); diff --git a/src/core/jpeg_stream.js b/src/core/jpeg_stream.js index 7cd822ec7..11bdf5e43 100644 --- a/src/core/jpeg_stream.js +++ b/src/core/jpeg_stream.js @@ -14,7 +14,7 @@ */ import { DecodeStream } from "./decode_stream.js"; -import { isDict } from "./primitives.js"; +import { Dict } from "./primitives.js"; import { JpegImage } from "./jpg.js"; import { shadow } from "../shared/util.js"; @@ -81,7 +81,7 @@ class JpegStream extends DecodeStream { } } // Fetching the 'ColorTransform' entry, if it exists. - if (isDict(this.params)) { + if (this.params instanceof Dict) { const colorTransform = this.params.get("ColorTransform"); if (Number.isInteger(colorTransform)) { jpegOptions.colorTransform = colorTransform; diff --git a/src/core/name_number_tree.js b/src/core/name_number_tree.js index e0bf13ec8..96089841c 100644 --- a/src/core/name_number_tree.js +++ b/src/core/name_number_tree.js @@ -13,8 +13,8 @@ * limitations under the License. */ +import { Dict, RefSet } from "./primitives.js"; import { FormatError, unreachable, warn } from "../shared/util.js"; -import { isDict, RefSet } from "./primitives.js"; /** * A NameTree/NumberTree is like a Dict but has some advantageous properties, @@ -43,7 +43,7 @@ class NameOrNumberTree { const queue = [this.root]; while (queue.length > 0) { const obj = xref.fetchIfRef(queue.shift()); - if (!isDict(obj)) { + if (!(obj instanceof Dict)) { continue; } if (obj.has("Kids")) { diff --git a/src/core/parser.js b/src/core/parser.js index 7eae17afa..3b608f373 100644 --- a/src/core/parser.js +++ b/src/core/parser.js @@ -22,16 +22,7 @@ import { StreamType, warn, } from "../shared/util.js"; -import { - Cmd, - Dict, - EOF, - isCmd, - isDict, - isName, - Name, - Ref, -} from "./primitives.js"; +import { Cmd, Dict, EOF, isCmd, isName, Name, Ref } from "./primitives.js"; import { isWhiteSpace, MissingDataException, @@ -1401,7 +1392,7 @@ class Linearization { Number.isInteger(obj1) && Number.isInteger(obj2) && isCmd(obj3, "obj") && - isDict(linDict) && + linDict instanceof Dict && isNum((obj = linDict.get("Linearized"))) && obj > 0 ) diff --git a/src/core/predictor_stream.js b/src/core/predictor_stream.js index afeec2adb..e36920a11 100644 --- a/src/core/predictor_stream.js +++ b/src/core/predictor_stream.js @@ -14,14 +14,14 @@ */ import { DecodeStream } from "./decode_stream.js"; +import { Dict } from "./primitives.js"; import { FormatError } from "../shared/util.js"; -import { isDict } from "./primitives.js"; class PredictorStream extends DecodeStream { constructor(str, maybeLength, params) { super(maybeLength); - if (!isDict(params)) { + if (!(params instanceof Dict)) { return str; // no prediction } const predictor = (this.predictor = params.get("Predictor") || 1); diff --git a/src/core/struct_tree.js b/src/core/struct_tree.js index e91124f5e..42fd5b5b1 100644 --- a/src/core/struct_tree.js +++ b/src/core/struct_tree.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import { isDict, isName, Ref } from "./primitives.js"; +import { Dict, isName, Ref } from "./primitives.js"; import { isString, stringToPDFString, warn } from "../shared/util.js"; import { NumberTree } from "./name_number_tree.js"; @@ -38,7 +38,7 @@ class StructTreeRoot { readRoleMap() { const roleMapDict = this.dict.get("RoleMap"); - if (!isDict(roleMapDict)) { + if (!(roleMapDict instanceof Dict)) { return; } roleMapDict.forEach((key, value) => { @@ -112,7 +112,7 @@ class StructElementNode { let kidDict = null; if (kid instanceof Ref) { kidDict = this.dict.xref.fetch(kid); - } else if (isDict(kid)) { + } else if (kid instanceof Dict) { kidDict = kid; } if (!kidDict) { @@ -256,7 +256,7 @@ class StructTreePage { return false; } - if (isDict(obj)) { + if (obj instanceof Dict) { if (obj.objId !== dict.objId) { return false; } diff --git a/src/core/writer.js b/src/core/writer.js index 9985f7bb1..6a66bb8fb 100644 --- a/src/core/writer.js +++ b/src/core/writer.js @@ -14,7 +14,7 @@ */ import { bytesToString, escapeString, warn } from "../shared/util.js"; -import { Dict, isDict, isName, Name, Ref } from "./primitives.js"; +import { Dict, isName, Name, Ref } from "./primitives.js"; import { escapePDFName, parseXFAPath } from "./core_utils.js"; import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js"; import { BaseStream } from "./base_stream.js"; @@ -86,7 +86,7 @@ function writeValue(value, buffer, transform) { buffer.push(numberToString(value)); } else if (typeof value === "boolean") { buffer.push(value.toString()); - } else if (isDict(value)) { + } else if (value instanceof Dict) { writeDict(value, buffer, transform); } else if (value instanceof BaseStream) { writeStream(value, buffer, transform); diff --git a/test/unit/primitives_spec.js b/test/unit/primitives_spec.js index 6604517df..07b973597 100644 --- a/test/unit/primitives_spec.js +++ b/test/unit/primitives_spec.js @@ -518,6 +518,8 @@ describe("primitives", function () { }); describe("isDict", function () { + /* eslint-disable no-restricted-syntax */ + it("handles non-dictionaries", function () { const nonDict = {}; expect(isDict(nonDict)).toEqual(false); @@ -535,6 +537,8 @@ describe("primitives", function () { expect(isDict(dict, "Page")).toEqual(true); expect(isDict(dict, "Contents")).toEqual(false); }); + + /* eslint-enable no-restricted-syntax */ }); describe("isRefsEqual", function () {