From 70d1869fe594a542f5e1df5a3fa0edd88bea1bd2 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 25 Feb 2021 13:11:05 +0100 Subject: [PATCH] Remove the, strictly unnecessary, closure and variable shadowing from `createObjectURL` Note that this particular helper function is, with the exception of the `GENERIC` default viewer and the (unsupported) SVG-backend, mostly unused at this point in time. Hence we should be able to clean-up this helper function slightly. Also, fixes a small inconsistency in the `SVGGraphics` initialization in the viewer, by passing in the `disableCreateObjectURL` compatibility-option. Given that the SVG-backend isn't officially supported/recommended this shouldn't have been an issue, but given that I spotted this it can't hurt to fix it. --- src/shared/util.js | 39 +++++++++++++++++---------------------- web/pdf_page_view.js | 6 +++++- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/shared/util.js b/src/shared/util.js index e0159595a..a5fbd5220 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -940,32 +940,27 @@ function createPromiseCapability() { return capability; } -const createObjectURL = (function createObjectURLClosure() { +function createObjectURL(data, contentType = "", forceDataSchema = false) { + if (URL.createObjectURL && !forceDataSchema) { + return URL.createObjectURL(new Blob([data], { type: contentType })); + } // Blob/createObjectURL is not available, falling back to data schema. const digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - // eslint-disable-next-line no-shadow - return function createObjectURL(data, contentType, forceDataSchema = false) { - if (!forceDataSchema && URL.createObjectURL) { - const blob = new Blob([data], { type: contentType }); - return URL.createObjectURL(blob); - } - - let buffer = `data:${contentType};base64,`; - for (let i = 0, ii = data.length; i < ii; i += 3) { - const b1 = data[i] & 0xff; - const b2 = data[i + 1] & 0xff; - const b3 = data[i + 2] & 0xff; - const d1 = b1 >> 2, - d2 = ((b1 & 3) << 4) | (b2 >> 4); - const d3 = i + 1 < ii ? ((b2 & 0xf) << 2) | (b3 >> 6) : 64; - const d4 = i + 2 < ii ? b3 & 0x3f : 64; - buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4]; - } - return buffer; - }; -})(); + let buffer = `data:${contentType};base64,`; + for (let i = 0, ii = data.length; i < ii; i += 3) { + const b1 = data[i] & 0xff; + const b2 = data[i + 1] & 0xff; + const b3 = data[i + 2] & 0xff; + const d1 = b1 >> 2, + d2 = ((b1 & 3) << 4) | (b2 >> 4); + const d3 = i + 1 < ii ? ((b2 & 0xf) << 2) | (b3 >> 6) : 64; + const d4 = i + 2 < ii ? b3 & 0x3f : 64; + buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4]; + } + return buffer; +} export { AbortException, diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index c479e0d1c..05eacee65 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -701,7 +701,11 @@ class PDFPageView { const actualSizeViewport = this.viewport.clone({ scale: CSS_UNITS }); const promise = pdfPage.getOperatorList().then(opList => { ensureNotCancelled(); - const svgGfx = new SVGGraphics(pdfPage.commonObjs, pdfPage.objs); + const svgGfx = new SVGGraphics( + pdfPage.commonObjs, + pdfPage.objs, + /* forceDataSchema = */ viewerCompatibilityParams.disableCreateObjectURL + ); return svgGfx.getSVG(opList, actualSizeViewport).then(svg => { ensureNotCancelled(); this.svg = svg;