Skip to content

A platform for community discussion. Free, open, simple.

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

David Taylorc219740274f3
diff --git a/app/assets/javascripts/browser-detect.js b/app/assets/javascripts/browser-detect.js
index d8d0eda4819..3b93c144997 100644
--- a/app/assets/javascripts/browser-detect.js
+++ b/app/assets/javascripts/browser-detect.js
@@ -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
diff --git a/app/assets/javascripts/polyfills.js b/app/assets/javascripts/polyfills.js
index 93dc3b309ca..381b769e394 100644
--- a/app/assets/javascripts/polyfills.js
+++ b/app/assets/javascripts/polyfills.js
@@ -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 () {