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:
parent
076bb30b6c
commit
5f64621d46
2
external/builder/test-fixtures.js
vendored
2
external/builder/test-fixtures.js
vendored
@ -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) {
|
||||
|
2
external/builder/test-fixtures_esprima.js
vendored
2
external/builder/test-fixtures_esprima.js
vendored
@ -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 = {
|
||||
|
2
external/importL10n/locales.js
vendored
2
external/importL10n/locales.js
vendored
@ -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() {
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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));
|
||||
});
|
||||
|
@ -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";
|
||||
|
@ -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",
|
||||
});
|
||||
}
|
||||
|
@ -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", "");
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user