Skip to content

Instantly share code, notes, and snippets.

@DigiTec
Last active December 23, 2015 03:39
Show Gist options
  • Save DigiTec/6575336 to your computer and use it in GitHub Desktop.
Save DigiTec/6575336 to your computer and use it in GitHub Desktop.
This is a short polyfill to add classList support to SVG in Internet Explorer. It takes advantage of the duck typing of property and method implementations to be able to simply move the existing property descriptor from HTMLElement up to Element. Note that FIreFox already puts their implementation on Element. And Chrome uses instance properties …
"use strict";
// IE supports duck typed classList so move it from HTMLElement to Element to support SVG.
// FireFox already has classList on Element so make sure not to tweak anything there.
// Chrome supports classList on every element, but as an instance property so no need to move anything there.
(function _initClassListPolyFill() {
if (!Element.prototype.hasOwnProperty("classList") && HTMLElement.prototype.hasOwnProperty("classList")) {
Object.defineProperty(Element.prototype, "classList", Object.getOwnPropertyDescriptor(HTMLElement.prototype, "classList"));
}
if (DOMTokenList) {
(function _initDOMTokenListPolyFill() {
function _argumentsToClasses(args) {
var classes = {};
Array.prototype.forEach.call(args, function _processArgs(arg) {
if (Array.isArray(arg)) {
arg.forEach(function _processArrayOfArgs(arrayArg) {
classes[arrayArg] = null;
});
}
else {
classes[arg] = null;
}
});
return Object.freeze(classes);
}
Object.defineProperties(DOMTokenList.prototype, {
addAll: {
value: function addAll() {
var classes = _argumentsToClasses(arguments)
for (var i in classes) {
this.add(i);
}
}
},
removeAll: {
value: function removeAll() {
var classes = _argumentsToClasses(arguments)
for (var i in classes) {
this.remove(i);
}
}
},
containsAny: {
value: function containsAny() {
var classes = _argumentsToClasses(arguments);
for (var i in classes) {
if (this.contains(i)) {
return true;
}
}
}
},
containsAll: {
value: function containsAll() {
var classes = _argumentsToClasses(arguments);
for (var i in classes) {
if (!this.contains(i)) {
return false;
}
}
return true;
}
}
});
})();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment