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) .readFileSync(expectationFilename)
.toString() .toString()
.trim() .trim()
.replace(/__filename/g, fs.realpathSync(inFilename)); .replaceAll("__filename", fs.realpathSync(inFilename));
const outLines = []; const outLines = [];
const outFilename = function (line) { const outFilename = function (line) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -199,7 +199,7 @@ function getFilenameFromContentDispositionHeader(contentDisposition) {
function (matches, charset, encoding, text) { function (matches, charset, encoding, text) {
if (encoding === "q" || encoding === "Q") { if (encoding === "q" || encoding === "Q") {
// RFC 2047 section 4.2. // RFC 2047 section 4.2.
text = text.replace(/_/g, " "); text = text.replaceAll("_", " ");
text = text.replace(/=([0-9a-fA-F]{2})/g, function (match, hex) { text = text.replace(/=([0-9a-fA-F]{2})/g, function (match, hex) {
return String.fromCharCode(parseInt(hex, 16)); 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 () { it("should modify string with non-displayable characters", function () {
const str = Array.from(Array(32).keys()) const str = Array.from(
.map(x => String.fromCharCode(x) + "a") Array(32).keys(),
.join(""); x => String.fromCharCode(x) + "a"
).join("");
// \x00 is replaced by an empty string. // \x00 is replaced by an empty string.
const expected = 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"; "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")) { if (params.has("search")) {
this.eventBus.dispatch("findfromurlhash", { this.eventBus.dispatch("findfromurlhash", {
source: this, source: this,
query: params.get("search").replace(/"/g, ""), query: params.get("search").replaceAll('"', ""),
phraseSearch: params.get("phrase") === "true", phraseSearch: params.get("phrase") === "true",
}); });
} }

View File

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