Skip to content

Commit

Permalink
Add polyfills for IE11
Browse files Browse the repository at this point in the history
  • Loading branch information
dotherightthing committed Jan 6, 2021
1 parent 8b13a2b commit 0c56c00
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
7 changes: 0 additions & 7 deletions js/_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions js/_keyboard-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -578,6 +647,8 @@ class KeyboardHelpers {
* @memberof KeyboardHelpers
*/
init() {
this.initPolyfills();

this.instanceId = this.instanceElement.getAttribute('id');

this.registerKeyboardActions();
Expand Down

0 comments on commit 0c56c00

Please sign in to comment.