Use String.prototype.replaceAll() where appropriate

This fairly new method allows replacing *multiple* occurrences within a string without having to use regular expressions.

Please refer to:
 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll#browser_compatibility
This commit is contained in:
Jonas Jenwald 2023-03-22 15:31:10 +01:00
parent 076bb30b6c
commit 5f64621d46
10 changed files with 15 additions and 15 deletions

View File

@ -21,7 +21,7 @@ files.forEach(function (expectationFilename) {
.readFileSync(expectationFilename)
.toString()
.trim()
.replace(/__filename/g, fs.realpathSync(inFilename));
.replaceAll("__filename", fs.realpathSync(inFilename));
const outLines = [];
const outFilename = function (line) {

View File

@ -21,7 +21,7 @@ files.forEach(function (expectationFilename) {
.readFileSync(expectationFilename)
.toString()
.trim()
.replace(/__filename/g, fs.realpathSync(inFilename));
.replaceAll("__filename", fs.realpathSync(inFilename));
const input = fs.readFileSync(inFilename).toString();
const defines = {

View File

@ -27,7 +27,7 @@ const DEFAULT_LOCALE = "en-US";
const EXCLUDE_LANG_CODES = ["ca-valencia", "ja-JP-mac"];
function normalizeText(s) {
return s.replace(/\r\n?/g, "\n").replace(/\uFEFF/g, "");
return s.replace(/\r\n?/g, "\n").replaceAll("\uFEFF", "");
}
function downloadLanguageCodes() {

View File

@ -989,7 +989,7 @@ class Catalog {
if (javaScript === null) {
javaScript = new Map();
}
js = stringToPDFString(js).replace(/\u0000/g, "");
js = stringToPDFString(js).replaceAll("\x00", "");
javaScript.set(name, js);
}

View File

@ -341,7 +341,7 @@ function _collectJS(entry, xref, list, parents) {
} else if (typeof js === "string") {
code = js;
}
code = code && stringToPDFString(code).replace(/\u0000/g, "");
code = code && stringToPDFString(code).replaceAll("\x00", "");
if (code) {
list.push(code);
}

View File

@ -68,9 +68,9 @@ class FileSpec {
if (!this._filename && this.root) {
const filename = pickPlatformItem(this.root) || "unnamed";
this._filename = stringToPDFString(filename)
.replace(/\\\\/g, "\\")
.replace(/\\\//g, "/")
.replace(/\\/g, "/");
.replaceAll("\\\\", "\\")
.replaceAll("\\/", "/")
.replaceAll("\\", "/");
}
return this._filename;
}

View File

@ -199,7 +199,7 @@ function getFilenameFromContentDispositionHeader(contentDisposition) {
function (matches, charset, encoding, text) {
if (encoding === "q" || encoding === "Q") {
// RFC 2047 section 4.2.
text = text.replace(/_/g, " ");
text = text.replaceAll("_", " ");
text = text.replace(/=([0-9a-fA-F]{2})/g, function (match, hex) {
return String.fromCharCode(parseInt(hex, 16));
});

View File

@ -157,9 +157,10 @@ describe("ui_utils", function () {
});
it("should modify string with non-displayable characters", function () {
const str = Array.from(Array(32).keys())
.map(x => String.fromCharCode(x) + "a")
.join("");
const str = Array.from(
Array(32).keys(),
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";

View File

@ -352,7 +352,7 @@ class PDFLinkService {
if (params.has("search")) {
this.eventBus.dispatch("findfromurlhash", {
source: this,
query: params.get("search").replace(/"/g, ""),
query: params.get("search").replaceAll('"', ""),
phraseSearch: params.get("phrase") === "true",
});
}

View File

@ -211,7 +211,6 @@ function parseQueryString(query) {
return params;
}
const NullCharactersRegExp = /\x00/g;
const InvisibleCharactersRegExp = /[\x01-\x1F]/g;
/**
@ -226,7 +225,7 @@ function removeNullCharacters(str, replaceInvisible = false) {
if (replaceInvisible) {
str = str.replace(InvisibleCharactersRegExp, " ");
}
return str.replace(NullCharactersRegExp, "");
return str.replaceAll("\x00", "");
}
/**