From 8d66a794446b6368e50e79c7f613a073a51f88dd Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Wed, 26 Aug 2020 22:01:45 +0200 Subject: [PATCH] Added a million IE11 polyfills... --- src/utils/index.js | 75 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 12 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index fca51ab..1cabda6 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,7 +1,7 @@ import I18n from '../i18n'; /** - * 'Deflates' the HTML contained in the given parent node. + * 'Deflates' the HTML contained in the given parent node. * Deflation will completely drop empty text nodes, and replace * multiple spaces, tabs, newlines with a single space. This way, * character offsets in the markup will more closely represent @@ -22,7 +22,7 @@ const deflateNodeList = parents => { // Text node - trim const trimmed = node.textContent.replace(/\s\s+/g, ' '); return [...compacted, document.createTextNode(trimmed)]; - } else { + } else { // Empty text node - discard return compacted; } @@ -33,7 +33,7 @@ const deflateNodeList = parents => { } // Replace original children with deflated - parents.forEach(parent => { + parents.forEach(parent => { const deflatedChildren = deflateOne(parent); parent.innerHTML = ''; deflatedChildren.forEach(node => parent.appendChild(node)); @@ -72,6 +72,27 @@ export const addPolyfills = () => { }; } + if (!String.prototype.startsWith) { + Object.defineProperty(String.prototype, 'startsWith', { + value: function(search, rawPos) { + var pos = rawPos > 0 ? rawPos|0 : 0; + return this.substring(pos, pos + search.length) === search; + } + }); + } + + if (!String.prototype.includes) { + String.prototype.includes = function(search, start) { + 'use strict'; + + if (search instanceof RegExp) { + throw TypeError('first argument must not be a RegExp'); + } + if (start === undefined) { start = 0; } + return this.indexOf(search, start) !== -1; + }; + } + if (!Array.prototype.find) { Object.defineProperty(Array.prototype, 'find', { value: function(predicate) { @@ -79,23 +100,23 @@ export const addPolyfills = () => { if (this == null) { throw TypeError('"this" is null or not defined'); } - + var o = Object(this); - + // 2. Let len be ? ToLength(? Get(O, "length")). var len = o.length >>> 0; - + // 3. If IsCallable(predicate) is false, throw a TypeError exception. if (typeof predicate !== 'function') { throw TypeError('predicate must be a function'); } - + // 4. If thisArg was supplied, let T be thisArg; else let T be undefined. var thisArg = arguments[1]; - + // 5. Let k be 0. var k = 0; - + // 6. Repeat, while k < len while (k < len) { // a. Let Pk be ! ToString(k). @@ -109,14 +130,44 @@ export const addPolyfills = () => { // e. Increase k by 1. k++; } - + // 7. Return undefined. return undefined; }, configurable: true, writable: true }); - + + } + + if (typeof Object.assign !== 'function') { + // Must be writable: true, enumerable: false, configurable: true + Object.defineProperty(Object, "assign", { + value: function assign(target, varArgs) { // .length of function is 2 + 'use strict'; + if (target === null || target === undefined) { + throw new TypeError('Cannot convert undefined or null to object'); + } + + var to = Object(target); + + for (var index = 1; index < arguments.length; index++) { + var nextSource = arguments[index]; + + if (nextSource !== null && nextSource !== undefined) { + for (var nextKey in nextSource) { + // Avoid bugs when hasOwnProperty is shadowed + if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { + to[nextKey] = nextSource[nextKey]; + } + } + } + } + return to; + }, + writable: true, + configurable: true + }); } } @@ -135,4 +186,4 @@ export const setLocale = locale => { console.warn(`Unsupported locale '${l}'. Falling back to default en.`); } } -} \ No newline at end of file +}