From 9adc7859c82b3a7a87b7971d82315fcd7795ac4c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 16 Nov 2022 12:18:13 +0100 Subject: [PATCH] Move the `escapeString` helper function into the worker-thread Given that this helper function is only used on the worker-thread, there's no reason to duplicate it in both of the `pdf.js` and `pdf.worker.js` files. --- src/core/annotation.js | 2 +- src/core/core_utils.js | 14 ++++++++++++++ src/core/writer.js | 9 +++++++-- src/shared/util.js | 15 --------------- test/unit/core_utils_spec.js | 9 +++++++++ test/unit/util_spec.js | 9 --------- 6 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index 4ee0ab1e3..353537110 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -23,7 +23,6 @@ import { AnnotationType, assert, BASELINE_FACTOR, - escapeString, FeatureTest, getModificationDate, IDENTITY_MATRIX, @@ -39,6 +38,7 @@ import { } from "../shared/util.js"; import { collectActions, + escapeString, getInheritableProperty, getRotationMatrix, isAscii, diff --git a/src/core/core_utils.js b/src/core/core_utils.js index e8be16014..a4c23b7c2 100644 --- a/src/core/core_utils.js +++ b/src/core/core_utils.js @@ -313,6 +313,19 @@ function escapePDFName(str) { return buffer.join(""); } +// Replace "(", ")", "\n", "\r" and "\" by "\(", "\)", "\\n", "\\r" and "\\" +// in order to write it in a PDF file. +function escapeString(str) { + return str.replace(/([()\\\n\r])/g, match => { + if (match === "\n") { + return "\\n"; + } else if (match === "\r") { + return "\\r"; + } + return `\\${match}`; + }); +} + function _collectJS(entry, xref, list, parents) { if (!entry) { return; @@ -621,6 +634,7 @@ export { DocStats, encodeToXmlString, escapePDFName, + escapeString, getArrayLookupTableFactory, getInheritableProperty, getLookupTableFactory, diff --git a/src/core/writer.js b/src/core/writer.js index 1de23297b..51626f157 100644 --- a/src/core/writer.js +++ b/src/core/writer.js @@ -13,9 +13,14 @@ * limitations under the License. */ -import { bytesToString, escapeString, warn } from "../shared/util.js"; +import { bytesToString, warn } from "../shared/util.js"; import { Dict, Name, Ref } from "./primitives.js"; -import { escapePDFName, numberToString, parseXFAPath } from "./core_utils.js"; +import { + escapePDFName, + escapeString, + numberToString, + parseXFAPath, +} from "./core_utils.js"; import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js"; import { BaseStream } from "./base_stream.js"; import { calculateMD5 } from "./crypto.js"; diff --git a/src/shared/util.js b/src/shared/util.js index c6497750b..6394e383c 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -1037,20 +1037,6 @@ function stringToPDFString(str) { return strBuf.join(""); } -function escapeString(str) { - // replace "(", ")", "\n", "\r" and "\" - // by "\(", "\)", "\\n", "\\r" and "\\" - // in order to write it in a PDF file. - return str.replace(/([()\\\n\r])/g, match => { - if (match === "\n") { - return "\\n"; - } else if (match === "\r") { - return "\\r"; - } - return `\\${match}`; - }); -} - function stringToUTF8String(str) { return decodeURIComponent(escape(str)); } @@ -1151,7 +1137,6 @@ export { createPromiseCapability, createValidAbsoluteUrl, DocumentActionEventType, - escapeString, FeatureTest, FONT_IDENTITY_MATRIX, FontType, diff --git a/test/unit/core_utils_spec.js b/test/unit/core_utils_spec.js index 010929d99..7723e6f66 100644 --- a/test/unit/core_utils_spec.js +++ b/test/unit/core_utils_spec.js @@ -17,6 +17,7 @@ import { Dict, Ref } from "../../src/core/primitives.js"; import { encodeToXmlString, escapePDFName, + escapeString, getInheritableProperty, isAscii, isWhiteSpace, @@ -223,6 +224,14 @@ describe("core_utils", function () { }); }); + describe("escapeString", function () { + it("should escape (, ), \\n, \\r, and \\", function () { + expect(escapeString("((a\\a))\n(b(b\\b)\rb)")).toEqual( + "\\(\\(a\\\\a\\)\\)\\n\\(b\\(b\\\\b\\)\\rb\\)" + ); + }); + }); + describe("encodeToXmlString", function () { it("should get a correctly encoded string with some entities", function () { const str = "\"\u0397ell😂' & "; diff --git a/test/unit/util_spec.js b/test/unit/util_spec.js index ed3726fde..43ee82883 100644 --- a/test/unit/util_spec.js +++ b/test/unit/util_spec.js @@ -17,7 +17,6 @@ import { bytesToString, createPromiseCapability, createValidAbsoluteUrl, - escapeString, getModificationDate, isArrayBuffer, string32, @@ -244,14 +243,6 @@ describe("util", function () { }); }); - describe("escapeString", function () { - it("should escape (, ), \\n, \\r, and \\", function () { - expect(escapeString("((a\\a))\n(b(b\\b)\rb)")).toEqual( - "\\(\\(a\\\\a\\)\\)\\n\\(b\\(b\\\\b\\)\\rb\\)" - ); - }); - }); - describe("getModificationDate", function () { it("should get a correctly formatted date", function () { const date = new Date(Date.UTC(3141, 5, 9, 2, 6, 53));