mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
Enable the unicorn/prefer-string-replace-all
ESLint plugin rule
Note that the `replaceAll` method still requires that a *global* regular expression is used, however by using this method it's immediately obvious when looking at the code that all occurrences will be replaced; please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll#parameters Please find additional details at https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-string-replace-all.md
This commit is contained in:
parent
5f64621d46
commit
1fc09f0235
26 changed files with 58 additions and 56 deletions
|
@ -303,7 +303,7 @@ function escapePDFName(str) {
|
|||
// Replace "(", ")", "\n", "\r" and "\" by "\(", "\)", "\\n", "\\r" and "\\"
|
||||
// in order to write it in a PDF file.
|
||||
function escapeString(str) {
|
||||
return str.replace(/([()\\\n\r])/g, match => {
|
||||
return str.replaceAll(/([()\\\n\r])/g, match => {
|
||||
if (match === "\n") {
|
||||
return "\\n";
|
||||
} else if (match === "\r") {
|
||||
|
|
|
@ -1144,7 +1144,7 @@ class PDFDocument {
|
|||
}
|
||||
let fontFamily = descriptor.get("FontFamily");
|
||||
// For example, "Wingdings 3" is not a valid font name in the css specs.
|
||||
fontFamily = fontFamily.replace(/[ ]+(\d)/g, "$1");
|
||||
fontFamily = fontFamily.replaceAll(/[ ]+(\d)/g, "$1");
|
||||
const fontWeight = descriptor.get("FontWeight");
|
||||
|
||||
// Angle is expressed in degrees counterclockwise in PDF
|
||||
|
|
|
@ -4086,7 +4086,7 @@ class PartialEvaluator {
|
|||
}
|
||||
|
||||
// Using base font name as a font name.
|
||||
baseFontName = baseFontName.name.replace(/[,_]/g, "-");
|
||||
baseFontName = baseFontName.name.replaceAll(/[,_]/g, "-");
|
||||
const metrics = this.getBaseFontMetrics(baseFontName);
|
||||
|
||||
// Simulating descriptor flags attribute
|
||||
|
|
|
@ -903,7 +903,7 @@ function createPostTable(properties) {
|
|||
|
||||
function createPostscriptName(name) {
|
||||
// See https://docs.microsoft.com/en-us/typography/opentype/spec/recom#name.
|
||||
return name.replace(/[^\x21-\x7E]|[[\](){}<>/%]/g, "").slice(0, 63);
|
||||
return name.replaceAll(/[^\x21-\x7E]|[[\](){}<>/%]/g, "").slice(0, 63);
|
||||
}
|
||||
|
||||
function createNameTable(name, proto) {
|
||||
|
@ -994,7 +994,7 @@ class Font {
|
|||
// Fallback to checking the font name, in order to improve text-selection,
|
||||
// since the /Flags-entry is often wrong (fixes issue13845.pdf).
|
||||
if (!isSerifFont && !properties.isSimulatedFlags) {
|
||||
const baseName = name.replace(/[,_]/g, "-").split("-")[0],
|
||||
const baseName = name.replaceAll(/[,_]/g, "-").split("-")[0],
|
||||
serifFonts = getSerifFonts();
|
||||
for (const namePart of baseName.split("+")) {
|
||||
if (serifFonts[namePart]) {
|
||||
|
@ -1445,7 +1445,7 @@ class Font {
|
|||
for (let j = 0, jj = nameTable.length; j < jj; j++) {
|
||||
for (let k = 0, kk = nameTable[j].length; k < kk; k++) {
|
||||
const nameEntry =
|
||||
nameTable[j][k] && nameTable[j][k].replace(/\s/g, "");
|
||||
nameTable[j][k] && nameTable[j][k].replaceAll(/\s/g, "");
|
||||
if (!nameEntry) {
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -181,7 +181,7 @@ function type1FontGlyphMapping(properties, builtInEncoding, glyphNames) {
|
|||
}
|
||||
|
||||
function normalizeFontName(name) {
|
||||
return name.replace(/[,_]/g, "-").replace(/\s/g, "");
|
||||
return name.replaceAll(/[,_]/g, "-").replaceAll(/\s/g, "");
|
||||
}
|
||||
|
||||
export {
|
||||
|
|
|
@ -36,12 +36,12 @@ class MetadataParser {
|
|||
// Start by removing any "junk" before the first tag (see issue 10395).
|
||||
return data
|
||||
.replace(/^[^<]+/, "")
|
||||
.replace(/>\\376\\377([^<]+)/g, function (all, codes) {
|
||||
.replaceAll(/>\\376\\377([^<]+)/g, function (all, codes) {
|
||||
const bytes = codes
|
||||
.replace(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
|
||||
.replaceAll(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
|
||||
return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
|
||||
})
|
||||
.replace(/&(amp|apos|gt|lt|quot);/g, function (str, name) {
|
||||
.replaceAll(/&(amp|apos|gt|lt|quot);/g, function (str, name) {
|
||||
switch (name) {
|
||||
case "amp":
|
||||
return "&";
|
||||
|
|
|
@ -99,7 +99,7 @@ class FontFinder {
|
|||
}
|
||||
|
||||
const pattern = /,|-|_| |bolditalic|bold|italic|regular|it/gi;
|
||||
let name = fontName.replace(pattern, "");
|
||||
let name = fontName.replaceAll(pattern, "");
|
||||
font = this.fonts.get(name);
|
||||
if (font) {
|
||||
this.cache.set(fontName, font);
|
||||
|
@ -109,7 +109,7 @@ class FontFinder {
|
|||
|
||||
const maybe = [];
|
||||
for (const [family, pdfFont] of this.fonts.entries()) {
|
||||
if (family.replace(pattern, "").toLowerCase().startsWith(name)) {
|
||||
if (family.replaceAll(pattern, "").toLowerCase().startsWith(name)) {
|
||||
maybe.push(pdfFont);
|
||||
}
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ class FontFinder {
|
|||
if (
|
||||
pdfFont.regular.name &&
|
||||
pdfFont.regular.name
|
||||
.replace(pattern, "")
|
||||
.replaceAll(pattern, "")
|
||||
.toLowerCase()
|
||||
.startsWith(name)
|
||||
) {
|
||||
|
@ -129,9 +129,9 @@ class FontFinder {
|
|||
}
|
||||
|
||||
if (maybe.length === 0) {
|
||||
name = name.replace(/psmt|mt/gi, "");
|
||||
name = name.replaceAll(/psmt|mt/gi, "");
|
||||
for (const [family, pdfFont] of this.fonts.entries()) {
|
||||
if (family.replace(pattern, "").toLowerCase().startsWith(name)) {
|
||||
if (family.replaceAll(pattern, "").toLowerCase().startsWith(name)) {
|
||||
maybe.push(pdfFont);
|
||||
}
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ class FontFinder {
|
|||
if (
|
||||
pdfFont.regular.name &&
|
||||
pdfFont.regular.name
|
||||
.replace(pattern, "")
|
||||
.replaceAll(pattern, "")
|
||||
.toLowerCase()
|
||||
.startsWith(name)
|
||||
) {
|
||||
|
|
|
@ -5731,7 +5731,7 @@ class Text extends ContentObject {
|
|||
|
||||
[$finalize]() {
|
||||
if (typeof this[$content] === "string") {
|
||||
this[$content] = this[$content].replace(/\r\n/g, "\n");
|
||||
this[$content] = this[$content].replaceAll("\r\n", "\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -231,9 +231,9 @@ class XhtmlObject extends XmlObject {
|
|||
|
||||
[$onText](str, richText = false) {
|
||||
if (!richText) {
|
||||
str = str.replace(crlfRegExp, "");
|
||||
str = str.replaceAll(crlfRegExp, "");
|
||||
if (!this.style.includes("xfa-spacerun:yes")) {
|
||||
str = str.replace(spacesRegExp, " ");
|
||||
str = str.replaceAll(spacesRegExp, " ");
|
||||
}
|
||||
} else {
|
||||
this[$richText] = true;
|
||||
|
@ -351,7 +351,7 @@ class XhtmlObject extends XmlObject {
|
|||
let value;
|
||||
if (this[$richText]) {
|
||||
value = this[$content]
|
||||
? this[$content].replace(crlfForRichTextRegExp, "\n")
|
||||
? this[$content].replaceAll(crlfForRichTextRegExp, "\n")
|
||||
: undefined;
|
||||
} else {
|
||||
value = this[$content] || undefined;
|
||||
|
|
|
@ -48,7 +48,7 @@ function isWhitespaceString(s) {
|
|||
|
||||
class XMLParserBase {
|
||||
_resolveEntities(s) {
|
||||
return s.replace(/&([^;]+);/g, (all, entity) => {
|
||||
return s.replaceAll(/&([^;]+);/g, (all, entity) => {
|
||||
if (entity.substring(0, 2) === "#x") {
|
||||
return String.fromCodePoint(parseInt(entity.substring(2), 16));
|
||||
} else if (entity.substring(0, 1) === "#") {
|
||||
|
|
|
@ -152,7 +152,7 @@ function getFilenameFromContentDispositionHeader(contentDisposition) {
|
|||
parts[i] = parts[i].slice(0, quotindex);
|
||||
parts.length = i + 1; // Truncates and stop the iteration.
|
||||
}
|
||||
parts[i] = parts[i].replace(/\\(.)/g, "$1");
|
||||
parts[i] = parts[i].replaceAll(/\\(.)/g, "$1");
|
||||
}
|
||||
value = parts.join('"');
|
||||
}
|
||||
|
@ -194,13 +194,13 @@ function getFilenameFromContentDispositionHeader(contentDisposition) {
|
|||
// encoding = q or b
|
||||
// encoded-text = any printable ASCII character other than ? or space.
|
||||
// ... but Firefox permits ? and space.
|
||||
return value.replace(
|
||||
return value.replaceAll(
|
||||
/=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g,
|
||||
function (matches, charset, encoding, text) {
|
||||
if (encoding === "q" || encoding === "Q") {
|
||||
// RFC 2047 section 4.2.
|
||||
text = text.replaceAll("_", " ");
|
||||
text = text.replace(/=([0-9a-fA-F]{2})/g, function (match, hex) {
|
||||
text = text.replaceAll(/=([0-9a-fA-F]{2})/g, function (match, hex) {
|
||||
return String.fromCharCode(parseInt(hex, 16));
|
||||
});
|
||||
return textdecode(charset, text);
|
||||
|
|
|
@ -60,7 +60,7 @@ class AForm {
|
|||
if (!actions) {
|
||||
actions = [];
|
||||
this._dateActionsCache.set(cFormat, actions);
|
||||
cFormat.replace(
|
||||
cFormat.replaceAll(
|
||||
/(d+)|(m+)|(y+)|(H+)|(M+)|(s+)/g,
|
||||
function (match, d, m, y, H, M, s) {
|
||||
if (d) {
|
||||
|
|
|
@ -65,7 +65,7 @@ class Util extends PDFObject {
|
|||
const ZERO = 4;
|
||||
const HASH = 8;
|
||||
let i = 0;
|
||||
return args[0].replace(
|
||||
return args[0].replaceAll(
|
||||
pattern,
|
||||
function (match, nDecSep, cFlags, nWidth, nPrecision, cConvChar) {
|
||||
// cConvChar must be one of d, f, s, x
|
||||
|
@ -287,7 +287,7 @@ class Util extends PDFObject {
|
|||
|
||||
const patterns =
|
||||
/(mmmm|mmm|mm|m|dddd|ddd|dd|d|yyyy|yy|HH|H|hh|h|MM|M|ss|s|tt|t|\\.)/g;
|
||||
return cFormat.replace(patterns, function (match, pattern) {
|
||||
return cFormat.replaceAll(patterns, function (match, pattern) {
|
||||
if (pattern in handlers) {
|
||||
return handlers[pattern](data);
|
||||
}
|
||||
|
@ -523,12 +523,12 @@ class Util extends PDFObject {
|
|||
};
|
||||
|
||||
// escape the string
|
||||
const escapedFormat = cFormat.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
|
||||
const escapedFormat = cFormat.replaceAll(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
|
||||
const patterns =
|
||||
/(mmmm|mmm|mm|m|dddd|ddd|dd|d|yyyy|yy|HH|H|hh|h|MM|M|ss|s|tt|t)/g;
|
||||
const actions = [];
|
||||
|
||||
const re = escapedFormat.replace(
|
||||
const re = escapedFormat.replaceAll(
|
||||
patterns,
|
||||
function (match, patternElement) {
|
||||
const { pattern, action } = handlers[patternElement];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue