Skip to content

Commit

Permalink
add classList support for IE 8 & 9. #2151
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyjhol committed Dec 11, 2018
1 parent 9bb511a commit 5496ce7
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions js/bootstrap-select.js
@@ -1,6 +1,57 @@
(function ($) {
'use strict';

// Polyfill for browsers with no classList support
// Remove in v2
if (!('classList' in document.createElement('_'))) {
(function (view) {
if (!('Element' in view)) return;

var classListProp = 'classList',
protoProp = 'prototype',
elemCtrProto = view.Element[protoProp],
objCtr = Object,
classListGetter = function () {
var $elem = $(this);

return {
add: function (classes) {
return $elem.addClass(classes);
},
remove: function (classes) {
return $elem.removeClass(classes);
},
toggle: function (classes, force) {
return $elem.toggleClass(classes, force);
},
contains: function (classes) {
return $elem.hasClass(classes);
}
}
};

if (objCtr.defineProperty) {
var classListPropDesc = {
get: classListGetter,
enumerable: true,
configurable: true
};
try {
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
} catch (ex) { // IE 8 doesn't support enumerable:true
// adding undefined to fight this issue https://github.com/eligrey/classList.js/issues/36
// modernie IE8-MSW7 machine has IE8 8.0.6001.18702 and is affected
if (ex.number === undefined || ex.number === -0x7FF5EC54) {
classListPropDesc.enumerable = false;
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
}
}
} else if (objCtr[protoProp].__defineGetter__) {
elemCtrProto.__defineGetter__(classListProp, classListGetter);
}
}(window));
}

var testElement = document.createElement('_');

testElement.classList.toggle('c3', false);
Expand All @@ -19,6 +70,8 @@
};
}

testElement = null;

// shallow array comparison
function isEqual (array1, array2) {
return array1.length === array2.length && array1.every(function (element, index) {
Expand Down

0 comments on commit 5496ce7

Please sign in to comment.