Skip to content

Instantly share code, notes, and snippets.

@Mouvedia
Last active June 25, 2019 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mouvedia/765ea3a0f9204e475fd2 to your computer and use it in GitHub Desktop.
Save Mouvedia/765ea3a0f9204e475fd2 to your computer and use it in GitHub Desktop.
/*
dependencies
isArray
defineProperties
TODO
defineProperties > defineProperty
http://stackoverflow.com/a/17935125/248058
isString
clear
typeof this.className === 'object' ? this.setAttribute('class', '') : this.className = '';
http://stackoverflow.com/a/7167553/248058
regex
*/
var element = window.Element || window.HTMLElement,
elementPrototype = (element && element.prototype) || window['[[DOMElement.prototype]]'];
elementPrototype.hasClass = function(className) {
var classes = typeof this.className === 'object' ? this.className.baseVal : this.className
, classList;
String.parseAsClass(className);
if (!classes)
return false;
if (classes === className)
return true;
if (this.classList)
return this.classList.contains(className);
classList = classes.split(/[ \t\n\f\r\u200b]+/);
for (var i = 0, len = classList.length; i < len; ++i) {
if (classList[i] === className)
return true;
}
return false;
};
elementPrototype.hasClasses = function(classes) {
var i
, arg = arguments
, len = arg.length;
if (!len)
throw new TypeError('The first argument is required.');
if (len === 1) {
arg = Array.isArray(classes) ? classes : classes.split(/[ \t\n\f\r\u200b]+/);
len = arg.length;
}
for (i = 0; i < len; ++i) {
if (arg[i] && !this.hasClass(arg[i]))
return false;
}
return true;
};
elementPrototype.addClass = function(className, bypass) {
if (bypass || !this.hasClass(className)) {
if (this.classList)
this.classList.add(className);
else if (typeof this.className === 'object')
this.setAttribute('class', this.className.baseVal ? this.className.baseVal + ' ' + className : className);
else
this.className += (this.className && ' ') + className;
}
return this;
};
elementPrototype.addClasses = function(classes) {
var i
, arg = arguments
, len = arg.length;
if (!len)
throw new TypeError('The first argument is required.');
if (len === 1) {
arg = Array.isArray(classes) ? classes : classes.split(/[ \t\n\f\r\u200b]+/);
len = arg.length;
}
for (i = 0; i < len; ++i) {
if (arg[i])
this.addClass(arg[i]);
}
return this;
};
elementPrototype.delClass = function(className, bypass) {
var classes = typeof this.className === 'object' ? this.className.baseVal : this.className
, tokenSet = ''
, classList;
if (!bypass)
String.parseAsClass(className);
if (classes) {
if (this.classList)
this.classList.remove(className);
else {
classList = classes.split(/[ \t\n\f\r\u200b]+/);
for (var i = 0, len = classList.length; i < len; ++i) {
if (classList[i] && classList[i] !== className)
tokenSet += (tokenSet && ' ') + classList[i];
}
if (tokenSet !== classes)
this.className.baseVal ? this.setAttribute('class', tokenSet) : this.className = tokenSet;
}
}
return this;
};
elementPrototype.delClasses = function(classes) {
var i
, arg = arguments
, len = arg.length;
if (!len)
throw new TypeError('The first argument is required.');
if (len === 1) {
arg = Array.isArray(classes) ? classes : classes.split(/[ \t\n\f\r\u200b]+/);
len = arg.length;
}
for (i = 0; i < len; ++i) {
if (arg[i])
this.delClass(arg[i]);
}
return this;
};
elementPrototype.toggleClass = function(className, force) {
var hasClass = this.hasClass(className);
if (typeof force !== 'undefined' && !hasClass === !force)
return force;
this[hasClass ? 'delClass' : 'addClass'](className, true);
return !hasClass;
};
elementPrototype.replaceClass = function(oldClass, newClass) {
String.parseAsClass(oldClass);
if (this.hasClass(newClass) || oldClass === newClass)
return this;
this.delClass(oldClass, true);
this.addClass(newClass, true);
return this;
};
Object.defineProperties && Object.defineProperties(elementPrototype, {
'hasClass': { enumerable: false },
'hasClasses': { enumerable: false },
'addClass': { enumerable: false },
'addClasses': { enumerable: false },
'delClass': { enumerable: false },
'delClasses': { enumerable: false },
'toggleClass': { enumerable: false },
'replaceClass': { enumerable: false }
});
// http://blog.stchur.com/2008/01/24/accessing-the-native-domelements-prototype-in-safari-2/
document.getElementsByTagName('html')[0];
document.createElement("iframe");
// https://gist.github.com/jonathantneal/5ca2cf0d5b0c39c2450d
/*@cc_on@if(@_jscript_version<5.8)
if (!self.document.querySelector) {
(self.HTMLElement = self.Element = function HTMLElement() {}).prototype = self.document.createElement('HTMLElement');
self.HTMLElement.prototype.attachEvent('onpropertychange', function (event) {
for (var name = event.propertyName, value = prototype[event.propertyName], all = self.HTMLElement.document.all, index = 0; element = all[index]; ++index) {
if (element !== self.HTMLElement) {
element[name] = value;
}
}
});
}
@end@*/
//multiple
https://github.com/eligrey/classList.js/blob/master/classList.js#L217 toggle
https://github.com/eligrey/classList.js/blob/master/classList.js#L196 add/remove
https://gist.github.com/termi/3952026
http://stackoverflow.com/a/23316665/248058
//defineProperty
http://johndyer.name/native-browser-get-set-properties-in-javascript/
// private get
https://github.com/Leaflet/Leaflet/blob/fb6b4776c6a2b5c3d5d8d5e6dff0678e989fb325/src/dom/DomUtil.js#L127
//SVG
setAttribute('attribute', 'value', 0) // https://msdn.microsoft.com/en-us/library/ms536739(v=vs.85).aspx
https://bugs.webkit.org/show_bug.cgi?id=20651
https://bugs.webkit.org/show_bug.cgi?id=22357
http://stackoverflow.com/a/28453739/248058
//isArray
http://web.mit.edu/jwalden/www/isArray.html
https://gist.github.com/atk/1034882
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment