[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.)
This commit is contained in:
Jonas Jenwald 2022-01-03 14:11:12 +01:00
parent 00aa9811e6
commit 7b8794b37e
7 changed files with 46 additions and 48 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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 = {

View File

@ -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();

View File

@ -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";

View File

@ -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";

View File

@ -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,