From 0276385e6e32351d65191a1c772727ef943af61f Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 2 Aug 2019 13:55:37 +0200 Subject: [PATCH] [api-minor] Fix completely broken `getStats` method by returning stats in Objects, rather than in Arrays (PR 11029 follow-up) With the changes to the `StreamType`/`FontType` "enums" in PR 11029, one unfortunate result is that `getStats` now *always* returns empty Arrays. Something that everyone, myself included, apparently missed is that you obviously cannot index an Array with Strings :-) I wrongly assumed that the unit-tests would catch any bugs, but they apparently suffered from the same issue as the code in `src/core/`. Another possible option could perhaps be to use `Set`s, rather than objects, but that will require larger changes since `LoopbackPort` (in `src/display/api.js`) doesn't support them. --- src/core/obj.js | 4 ++-- src/display/api.js | 6 +++--- test/unit/api_spec.js | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/core/obj.js b/src/core/obj.js index 60465c864..4de709ced 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -1046,8 +1046,8 @@ var XRef = (function XRefClosure() { // prepare the XRef cache this.cache = []; this.stats = { - streamTypes: [], - fontTypes: [], + streamTypes: Object.create(null), + fontTypes: Object.create(null), }; } diff --git a/src/display/api.js b/src/display/api.js index bea055001..263a534c6 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -205,10 +205,10 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) { /** * @typedef {Object} PDFDocumentStats - * @property {Array} streamTypes - Used stream types in the document (an item + * @property {Object} streamTypes - Used stream types in the document (an item * is set to true if specific stream ID was used in the document). - * @property {Array} fontTypes - Used font type in the document (an item is set - * to true if specific font ID was used in the document). + * @property {Object} fontTypes - Used font types in the document (an item + * is set to true if specific font ID was used in the document). */ /** diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index cac5fec01..26a0a8f7d 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -926,7 +926,7 @@ describe('api', function() { it('gets document stats', function(done) { var promise = doc.getStats(); promise.then(function (stats) { - expect(stats).toEqual({ streamTypes: [], fontTypes: [], }); + expect(stats).toEqual({ streamTypes: {}, fontTypes: {}, }); done(); }).catch(done.fail); }); @@ -1293,9 +1293,9 @@ describe('api', function() { var promise = page.getOperatorList().then(function () { return pdfDocument.getStats(); }); - var expectedStreamTypes = []; + var expectedStreamTypes = {}; expectedStreamTypes[StreamType.FLATE] = true; - var expectedFontTypes = []; + var expectedFontTypes = {}; expectedFontTypes[FontType.TYPE1] = true; expectedFontTypes[FontType.CIDFONTTYPE2] = true;