extes
Version:
A tiny library that extends native js with some handy tools
83 lines (74 loc) • 2.07 kB
JavaScript
/**
* Author: JCloudYu
* Create: 2019/07/23
**/
const configurable = true, writable = true, enumerable = false;
//@export
(()=>{
if ( typeof Element !== "undefined" ) {
const _ELEMENT_SET_ATTRIBUTE = Element.prototype.setAttribute;
const _ELEMENT_REMOVE_ATTRIBUTE = Element.prototype.removeAttribute;
const _ELEMENT_SET_ATTRIBUTE_NS = Element.prototype.setAttributeNS;
const _ELEMENT_REMOVE_ATTRIBUTE_NS = Element.prototype.removeAttributeNS;
Object.defineProperties(Element.prototype, {
addClass: {
configurable, enumerable, writable,
value: function(...classes) {
const filtered = [];
for( const class_name of classes ) {
if ( class_name === undefined || class_name === null || class_name === '' ) {
continue;
}
filtered.push(class_name);
}
this.classList.add(...filtered);
return this;
}
},
removeClass: {
configurable, enumerable, writable,
value: function(...classes) {
const filtered = [];
for( const class_name of classes ) {
if ( class_name === undefined || class_name === null || class_name === '' ) {
continue;
}
filtered.push(class_name);
}
this.classList.remove(...filtered);
return this;
}
},
setAttribute: {
configurable, enumerable, writable,
value: function(name, value) {
if ( arguments.length < 2 ) { value = ''; }
_ELEMENT_SET_ATTRIBUTE.call(this, name, value);
return this;
}
},
removeAttribute: {
configurable, enumerable, writable,
value: function(...args) {
_ELEMENT_REMOVE_ATTRIBUTE.apply(this, args);
return this;
}
},
setAttributeNS: {
configurable, enumerable, writable,
value: function(...args) {
_ELEMENT_SET_ATTRIBUTE_NS.apply(this, args);
return this;
}
},
removeAttributeNS: {
configurable, enumerable, writable,
value: function(...args) {
_ELEMENT_REMOVE_ATTRIBUTE_NS.apply(this, args);
return this;
}
},
});
}
})();
//@endexport