DEV: Add polyfill for `String.prototype.replaceAll` (#16301)

This commit is contained in:
David Taylor 2022-03-28 17:18:56 +01:00 committed by GitHub
parent 720e1ca9e7
commit c219740274
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

View File

@ -1,9 +1,4 @@
if (
!window.WeakMap ||
!window.Promise ||
typeof globalThis === "undefined" ||
!String.prototype.replaceAll
) {
if (!window.WeakMap || !window.Promise || typeof globalThis === "undefined") {
window.unsupportedBrowser = true;
} else {
// Some implementations of `WeakMap.prototype.has` do not accept false

View File

@ -1,5 +1,27 @@
/* eslint-disable */
// Needed for iOS <= 13.3
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function (
pattern,
replacementStringOrFunction
) {
let regex;
if (
Object.prototype.toString.call(pattern).toLowerCase() ===
"[object regexp]"
) {
regex = pattern;
} else {
const escapedStr = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
regex = new RegExp(escapedStr, "g");
}
return this.replace(regex, replacementStringOrFunction);
};
}
// Needed for Safari 15.2 and below
// from: https://github.com/iamdustan/smoothscroll
(function () {