From 7b8794b37e3e4bfc25474d17289faea98918e841 Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Mon, 3 Jan 2022 14:11:12 +0100
Subject: [PATCH] [api-minor] Move `removeNullCharacters` into the viewer

This helper function has never been used in e.g. the worker-thread, hence its placement in `src/shared/util.js` led to a *small* amount of unnecessary duplication.
After the previous patches this helper function is now *only* used in the viewer, hence it no longer seems necessary to expose it through the official API.

*Please note:* It seems somewhat unlikely that third-party users were relying *directly* on this helper function, which is why it's not being exported as part of the viewer components. (If necessary, we can always change this later on.)
---
 src/pdf.js                 |  2 --
 src/shared/util.js         | 18 ------------------
 test/unit/ui_utils_spec.js | 25 +++++++++++++++++++++++++
 test/unit/util_spec.js     | 25 -------------------------
 web/base_tree_viewer.js    |  2 +-
 web/pdf_link_service.js    |  3 +--
 web/ui_utils.js            | 19 +++++++++++++++++++
 7 files changed, 46 insertions(+), 48 deletions(-)

diff --git a/src/pdf.js b/src/pdf.js
index a69baeba9..d89ac3af8 100644
--- a/src/pdf.js
+++ b/src/pdf.js
@@ -30,7 +30,6 @@ import {
   OPS,
   PasswordResponses,
   PermissionFlag,
-  removeNullCharacters,
   shadow,
   UnexpectedResponseException,
   UNSUPPORTED_FEATURES,
@@ -129,7 +128,6 @@ export {
   PDFWorker,
   PermissionFlag,
   PixelsPerInch,
-  removeNullCharacters,
   RenderingCancelledException,
   renderTextLayer,
   shadow,
diff --git a/src/shared/util.js b/src/shared/util.js
index 7211254dd..214624236 100644
--- a/src/shared/util.js
+++ b/src/shared/util.js
@@ -575,23 +575,6 @@ class AbortException extends BaseException {
   }
 }
 
-const NullCharactersRegExp = /\x00+/g;
-const InvisibleCharactersRegExp = /[\x01-\x1F]/g;
-
-/**
- * @param {string} str
- */
-function removeNullCharacters(str, replaceInvisible = false) {
-  if (typeof str !== "string") {
-    warn("The argument for removeNullCharacters must be a string.");
-    return str;
-  }
-  if (replaceInvisible) {
-    str = str.replace(InvisibleCharactersRegExp, " ");
-  }
-  return str.replace(NullCharactersRegExp, "");
-}
-
 function bytesToString(bytes) {
   assert(
     bytes !== null && typeof bytes === "object" && bytes.length !== undefined,
@@ -1185,7 +1168,6 @@ export {
   PasswordException,
   PasswordResponses,
   PermissionFlag,
-  removeNullCharacters,
   RenderingIntentFlag,
   setVerbosityLevel,
   shadow,
diff --git a/test/unit/ui_utils_spec.js b/test/unit/ui_utils_spec.js
index d262f1079..c83e2f056 100644
--- a/test/unit/ui_utils_spec.js
+++ b/test/unit/ui_utils_spec.js
@@ -21,6 +21,7 @@ import {
   isPortraitOrientation,
   isValidRotation,
   parseQueryString,
+  removeNullCharacters,
 } from "../../web/ui_utils.js";
 
 describe("ui_utils", function () {
@@ -139,6 +140,30 @@ describe("ui_utils", function () {
     });
   });
 
+  describe("removeNullCharacters", function () {
+    it("should not modify string without null characters", function () {
+      const str = "string without null chars";
+      expect(removeNullCharacters(str)).toEqual("string without null chars");
+    });
+
+    it("should modify string with null characters", function () {
+      const str = "string\x00With\x00Null\x00Chars";
+      expect(removeNullCharacters(str)).toEqual("stringWithNullChars");
+    });
+
+    it("should modify string with non-displayable characters", function () {
+      const str = Array.from(Array(32).keys())
+        .map(x => String.fromCharCode(x) + "a")
+        .join("");
+      // \x00 is replaced by an empty string.
+      const expected =
+        "a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a";
+      expect(removeNullCharacters(str, /* replaceInvisible */ true)).toEqual(
+        expected
+      );
+    });
+  });
+
   describe("getPageSizeInches", function () {
     it("gets page size (in inches)", function () {
       const page = {
diff --git a/test/unit/util_spec.js b/test/unit/util_spec.js
index b86d287c4..941e2542c 100644
--- a/test/unit/util_spec.js
+++ b/test/unit/util_spec.js
@@ -25,7 +25,6 @@ import {
   isNum,
   isSameOrigin,
   isString,
-  removeNullCharacters,
   string32,
   stringToBytes,
   stringToPDFString,
@@ -175,30 +174,6 @@ describe("util", function () {
     });
   });
 
-  describe("removeNullCharacters", function () {
-    it("should not modify string without null characters", function () {
-      const str = "string without null chars";
-      expect(removeNullCharacters(str)).toEqual("string without null chars");
-    });
-
-    it("should modify string with null characters", function () {
-      const str = "string\x00With\x00Null\x00Chars";
-      expect(removeNullCharacters(str)).toEqual("stringWithNullChars");
-    });
-
-    it("should modify string with non-displayable characters", function () {
-      const str = Array.from(Array(32).keys())
-        .map(x => String.fromCharCode(x) + "a")
-        .join("");
-      // \x00 is replaced by an empty string.
-      const expected =
-        "a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a";
-      expect(removeNullCharacters(str, /* replaceInvisible */ true)).toEqual(
-        expected
-      );
-    });
-  });
-
   describe("ReadableStream", function () {
     it("should return an Object", function () {
       const readable = new ReadableStream();
diff --git a/web/base_tree_viewer.js b/web/base_tree_viewer.js
index 7a4f1e030..7a3168128 100644
--- a/web/base_tree_viewer.js
+++ b/web/base_tree_viewer.js
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-import { removeNullCharacters } from "pdfjs-lib";
+import { removeNullCharacters } from "./ui_utils.js";
 
 const TREEITEM_OFFSET_TOP = -100; // px
 const TREEITEM_SELECTED_CLASS = "selected";
diff --git a/web/pdf_link_service.js b/web/pdf_link_service.js
index 0913993de..6e2a5f989 100644
--- a/web/pdf_link_service.js
+++ b/web/pdf_link_service.js
@@ -16,8 +16,7 @@
 /** @typedef {import("./event_utils").EventBus} EventBus */
 /** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
 
-import { parseQueryString } from "./ui_utils.js";
-import { removeNullCharacters } from "pdfjs-lib";
+import { parseQueryString, removeNullCharacters } from "./ui_utils.js";
 
 const DEFAULT_LINK_REL = "noopener noreferrer nofollow";
 
diff --git a/web/ui_utils.js b/web/ui_utils.js
index d446cda3d..54593573a 100644
--- a/web/ui_utils.js
+++ b/web/ui_utils.js
@@ -200,6 +200,24 @@ function parseQueryString(query) {
   return params;
 }
 
+const NullCharactersRegExp = /\x00/g;
+const InvisibleCharactersRegExp = /[\x01-\x1F]/g;
+
+/**
+ * @param {string} str
+ * @param {boolean} [replaceInvisible]
+ */
+function removeNullCharacters(str, replaceInvisible = false) {
+  if (typeof str !== "string") {
+    console.error(`The argument must be a string.`);
+    return str;
+  }
+  if (replaceInvisible) {
+    str = str.replace(InvisibleCharactersRegExp, " ");
+  }
+  return str.replace(NullCharactersRegExp, "");
+}
+
 /**
  * Use binary search to find the index of the first item in a given array which
  * passes a given condition. The items are expected to be sorted in the sense
@@ -838,6 +856,7 @@ export {
   parseQueryString,
   PresentationModeState,
   ProgressBar,
+  removeNullCharacters,
   RendererType,
   RenderingStates,
   roundToDivide,