diff --git a/js/_init.js b/js/_init.js index 89de4ae..f844ed7 100644 --- a/js/_init.js +++ b/js/_init.js @@ -6,13 +6,6 @@ document.onreadystatechange = () => { // The document has finished loading and the document has been parsed // but sub-resources such as images, stylesheets and frames are still loading. if (document.readyState === 'interactive') { - // Function to make IE9+ support forEach - // untested (CBD Free not working) - // https://stackoverflow.com/a/50917053/6850747 - if (window.NodeList && !NodeList.prototype.forEach) { - NodeList.prototype.forEach = Array.prototype.forEach; - } - document.querySelectorAll('.label[data-for]').forEach((label) => { const labelInstance = new Label({ instanceElement: label diff --git a/js/_keyboard-helpers.js b/js/_keyboard-helpers.js index e3db275..6fa15cf 100644 --- a/js/_keyboard-helpers.js +++ b/js/_keyboard-helpers.js @@ -244,6 +244,75 @@ class KeyboardHelpers { return keyboardNavigableElementIndex; } + /** + * @function initPolyfills + * @memberof KeyboardHelpers + */ + initPolyfills() { + // Polyfill for forEach + if (window.NodeList && !NodeList.prototype.forEach) { + NodeList.prototype.forEach = Array.prototype.forEach; + } + + // Polyfill for forEach + if (window.HTMLCollection && !HTMLCollection.prototype.forEach) { + HTMLCollection.prototype.forEach = Array.prototype.forEach; + } + + // Polyfill for includes + // https://www.cluemediator.com/object-doesnt-support-property-or-method-includes-in-ie + if (!Array.prototype.includes) { + Object.defineProperty(Array.prototype, 'includes', { + value: function (searchElement, fromIndex) { + + if (this == null) { + throw new TypeError('"this" is null or not defined'); + } + + // 1. Let O be ? ToObject(this value). + var o = Object(this); + + // 2. Let len be ? ToLength(? Get(O, "length")). + var len = o.length >>> 0; + + // 3. If len is 0, return false. + if (len === 0) { + return false; + } + + // 4. Let n be ? ToInteger(fromIndex). + // (If fromIndex is undefined, this step produces the value 0.) + var n = fromIndex | 0; + + // 5. If n ≥ 0, then + // a. Let k be n. + // 6. Else n < 0, + // a. Let k be len + n. + // b. If k < 0, let k be 0. + var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); + + function sameValueZero(x, y) { + return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); + } + + // 7. Repeat, while k < len + while (k < len) { + // a. Let elementK be the result of ? Get(O, ! ToString(k)). + // b. If SameValueZero(searchElement, elementK) is true, return true. + if (sameValueZero(o[k], searchElement)) { + return true; + } + // c. Increase k by 1. + k++; + } + + // 8. Return false + return false; + } + }); + } + } + /** * @function isComponentElement * @summary Determine whether the element is the instanceElement @@ -578,6 +647,8 @@ class KeyboardHelpers { * @memberof KeyboardHelpers */ init() { + this.initPolyfills(); + this.instanceId = this.instanceElement.getAttribute('id'); this.registerKeyboardActions();