[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, OPS,
PasswordResponses, PasswordResponses,
PermissionFlag, PermissionFlag,
removeNullCharacters,
shadow, shadow,
UnexpectedResponseException, UnexpectedResponseException,
UNSUPPORTED_FEATURES, UNSUPPORTED_FEATURES,
@ -129,7 +128,6 @@ export {
PDFWorker, PDFWorker,
PermissionFlag, PermissionFlag,
PixelsPerInch, PixelsPerInch,
removeNullCharacters,
RenderingCancelledException, RenderingCancelledException,
renderTextLayer, renderTextLayer,
shadow, 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) { function bytesToString(bytes) {
assert( assert(
bytes !== null && typeof bytes === "object" && bytes.length !== undefined, bytes !== null && typeof bytes === "object" && bytes.length !== undefined,
@ -1185,7 +1168,6 @@ export {
PasswordException, PasswordException,
PasswordResponses, PasswordResponses,
PermissionFlag, PermissionFlag,
removeNullCharacters,
RenderingIntentFlag, RenderingIntentFlag,
setVerbosityLevel, setVerbosityLevel,
shadow, shadow,

View File

@ -21,6 +21,7 @@ import {
isPortraitOrientation, isPortraitOrientation,
isValidRotation, isValidRotation,
parseQueryString, parseQueryString,
removeNullCharacters,
} from "../../web/ui_utils.js"; } from "../../web/ui_utils.js";
describe("ui_utils", function () { 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 () { describe("getPageSizeInches", function () {
it("gets page size (in inches)", function () { it("gets page size (in inches)", function () {
const page = { const page = {

View File

@ -25,7 +25,6 @@ import {
isNum, isNum,
isSameOrigin, isSameOrigin,
isString, isString,
removeNullCharacters,
string32, string32,
stringToBytes, stringToBytes,
stringToPDFString, 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 () { describe("ReadableStream", function () {
it("should return an Object", function () { it("should return an Object", function () {
const readable = new ReadableStream(); const readable = new ReadableStream();

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { removeNullCharacters } from "pdfjs-lib"; import { removeNullCharacters } from "./ui_utils.js";
const TREEITEM_OFFSET_TOP = -100; // px const TREEITEM_OFFSET_TOP = -100; // px
const TREEITEM_SELECTED_CLASS = "selected"; const TREEITEM_SELECTED_CLASS = "selected";

View File

@ -16,8 +16,7 @@
/** @typedef {import("./event_utils").EventBus} EventBus */ /** @typedef {import("./event_utils").EventBus} EventBus */
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */ /** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
import { parseQueryString } from "./ui_utils.js"; import { parseQueryString, removeNullCharacters } from "./ui_utils.js";
import { removeNullCharacters } from "pdfjs-lib";
const DEFAULT_LINK_REL = "noopener noreferrer nofollow"; const DEFAULT_LINK_REL = "noopener noreferrer nofollow";

View File

@ -200,6 +200,24 @@ function parseQueryString(query) {
return params; 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 * 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 * passes a given condition. The items are expected to be sorted in the sense
@ -838,6 +856,7 @@ export {
parseQueryString, parseQueryString,
PresentationModeState, PresentationModeState,
ProgressBar, ProgressBar,
removeNullCharacters,
RendererType, RendererType,
RenderingStates, RenderingStates,
roundToDivide, roundToDivide,