Path: blob/main/projects/cupcake2048/js/classlist_polyfill.js
4626 views
(function () {1if (typeof window.Element === "undefined" ||2"classList" in document.documentElement) {3return;4}56var prototype = Array.prototype,7push = prototype.push,8splice = prototype.splice,9join = prototype.join;1011function DOMTokenList(el) {12this.el = el;13// The className needs to be trimmed and split on whitespace14// to retrieve a list of classes.15var classes = el.className.replace(/^\s+|\s+$/g, '').split(/\s+/);16for (var i = 0; i < classes.length; i++) {17push.call(this, classes[i]);18}19}2021DOMTokenList.prototype = {22add: function (token) {23if (this.contains(token)) return;24push.call(this, token);25this.el.className = this.toString();26},27contains: function (token) {28return this.el.className.indexOf(token) != -1;29},30item: function (index) {31return this[index] || null;32},33remove: function (token) {34if (!this.contains(token)) return;35for (var i = 0; i < this.length; i++) {36if (this[i] == token) break;37}38splice.call(this, i, 1);39this.el.className = this.toString();40},41toString: function () {42return join.call(this, ' ');43},44toggle: function (token) {45if (!this.contains(token)) {46this.add(token);47} else {48this.remove(token);49}5051return this.contains(token);52}53};5455window.DOMTokenList = DOMTokenList;5657function defineElementGetter(obj, prop, getter) {58if (Object.defineProperty) {59Object.defineProperty(obj, prop, {60get: getter61});62} else {63obj.__defineGetter__(prop, getter);64}65}6667defineElementGetter(HTMLElement.prototype, 'classList', function () {68return new DOMTokenList(this);69});70})();717273