From 8a4466139bdc592cbabf0b9a4963eb4b11295bb8 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 24 Jul 2018 14:37:43 +0200 Subject: [PATCH] Simplify the `DocumentInfoValidators` definition With this file now being a proper (ES6) module, it's no longer (technically) necessary for this structure to be lazily initialized. Considering its size, and simplicity, I therefore cannot see the harm in letting `DocumentInfoValidators` just be simple Object instead. While I'm not aware of any bugs caused by the current code, it cannot hurt to add an `isDict` check in `PDFDocument.documentInfo` (since the current code assumes that `infoDict` being defined implies it also being a Dictionary). Finally, the patch also converts a couple of `var` to `let`/`const`. --- src/core/document.js | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index 6832ef2d6..dfc46df98 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -377,22 +377,16 @@ var PDFDocument = (function PDFDocumentClosure() { return true; /* found */ } - var DocumentInfoValidators = { - get entries() { - // Lazily build this since all the validation functions below are not - // defined until after this file loads. - return shadow(this, 'entries', { - Title: isString, - Author: isString, - Subject: isString, - Keywords: isString, - Creator: isString, - Producer: isString, - CreationDate: isString, - ModDate: isString, - Trapped: isName, - }); - }, + const DocumentInfoValidators = { + Title: isString, + Author: isString, + Subject: isString, + Keywords: isString, + Creator: isString, + Producer: isString, + CreationDate: isString, + ModDate: isString, + Trapped: isName, }; PDFDocument.prototype = { @@ -555,14 +549,13 @@ var PDFDocument = (function PDFDocumentClosure() { } info('The document information dictionary is invalid.'); } - if (infoDict) { - var validEntries = DocumentInfoValidators.entries; + if (isDict(infoDict)) { // Only fill the document info with valid entries from the spec. - for (var key in validEntries) { + for (let key in DocumentInfoValidators) { if (infoDict.has(key)) { - var value = infoDict.get(key); + const value = infoDict.get(key); // Make sure the value conforms to the spec. - if (validEntries[key](value)) { + if (DocumentInfoValidators[key](value)) { docInfo[key] = (typeof value !== 'string' ? value : stringToPDFString(value)); } else {