min-dom
Version:
A minimal dom utility toolbelt
2 lines (1 loc) • 4.39 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).MinDom={})}(this,(function(t){"use strict";function e(t,e){return e.forEach((function(e){e&&"string"!=typeof e&&!Array.isArray(e)&&Object.keys(e).forEach((function(n){if("default"!==n&&!(n in t)){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}}))})),Object.freeze(t)}const n=Object.prototype.toString,r=Object.prototype.hasOwnProperty;function o(t,e){return r.call(t,e)}function i(t,e){let r,i;if(void 0===t)return;const c=function(t){return"[object Array]"===n.call(t)}(t)?u:l;for(let n in t)if(o(t,n)&&(r=t[n],i=e(r,c(n)),!1===i))return r}function l(t){return t}function u(t){return Number(t)}const c=Object.prototype.toString;function a(t){if(!t||!t.nodeType)throw new Error("A DOM element reference is required");this.el=t,this.list=t.classList}function s(t,e,n){var r=n?t:t.parentNode;return r&&"function"==typeof r.closest&&r.closest(e)||null}a.prototype.add=function(t){return this.list.add(t),this},a.prototype.remove=function(t){return"[object RegExp]"==c.call(t)?this.removeMatching(t):(this.list.remove(t),this)},a.prototype.removeMatching=function(t){const e=this.array();for(let n=0;n<e.length;n++)t.test(e[n])&&this.remove(e[n]);return this},a.prototype.toggle=function(t,e){return void 0!==e?e!==this.list.toggle(t,e)&&this.list.toggle(t):this.list.toggle(t),this},a.prototype.array=function(){return Array.from(this.list)},a.prototype.has=a.prototype.contains=function(t){return this.list.contains(t)};var d,f,p,h={};function y(){d=window.addEventListener?"addEventListener":"attachEvent",f=window.removeEventListener?"removeEventListener":"detachEvent",p="addEventListener"!==d?"on":""}var b=e({__proto__:null,bind:h.bind=function(t,e,n,r){return d||y(),t[d](p+e,n,r||!1),n},unbind:h.unbind=function(t,e,n,r){return f||y(),t[f](p+e,n,r||!1),n},default:h},[h]),g=["focus","blur"];var v,m={bind:function(t,e,n,r,o){return-1!==g.indexOf(n)&&(o=!0),b.bind(t,n,(function(n){var o=n.target||n.srcElement;n.delegateTarget=s(o,e,!0),n.delegateTarget&&r.call(t,n)}),o)},unbind:function(t,e,n,r){return-1!==g.indexOf(e)&&(r=!0),b.unbind(t,e,n,r)}},w=function(t,e){if("string"!=typeof t)throw new TypeError("String expected");e||(e=document);var n=/<([\w:]+)/.exec(t);if(!n)return e.createTextNode(t);t=t.replace(/^\s+|\s+$/g,"");var r=n[1];if("body"==r){return(o=e.createElement("html")).innerHTML=t,o.removeChild(o.lastChild)}var o,i=Object.prototype.hasOwnProperty.call(O,r)?O[r]:O._default,l=i[0],u=i[1],c=i[2];(o=e.createElement("div")).innerHTML=u+t+c;for(;l--;)o=o.lastChild;if(o.firstChild==o.lastChild)return o.removeChild(o.firstChild);var a=e.createDocumentFragment();for(;o.firstChild;)a.appendChild(o.removeChild(o.firstChild));return a},E=!1;"undefined"!=typeof document&&((v=document.createElement("div")).innerHTML=' <link/><table></table><a href="/a">a</a><input type="checkbox"/>',E=!v.getElementsByTagName("link").length,v=void 0);var O={legend:[1,"<fieldset>","</fieldset>"],tr:[2,"<table><tbody>","</tbody></table>"],col:[2,"<table><tbody></tbody><colgroup>","</colgroup></table>"],_default:E?[1,"X<div>","</div>"]:[0,"",""]};O.td=O.th=[3,"<table><tbody><tr>","</tr></tbody></table>"],O.option=O.optgroup=[1,'<select multiple="multiple">',"</select>"],O.thead=O.tbody=O.colgroup=O.caption=O.tfoot=[1,"<table>","</table>"],O.polyline=O.ellipse=O.polygon=O.circle=O.text=O.line=O.path=O.rect=O.g=[1,'<svg xmlns="http://www.w3.org/2000/svg" version="1.1">',"</svg>"];var C=w;t.assignStyle=function(t,...e){const n=t.style;return i(e,(function(t){t&&i(t,(function(t,e){n[e]=t}))})),t},t.attr=function(t,e,n){return 2==arguments.length?t.getAttribute(e):null===n?t.removeAttribute(e):(t.setAttribute(e,n),t)},t.classes=function(t){return new a(t)},t.clear=function(t){for(var e;e=t.firstChild;)t.removeChild(e);return t},t.closest=s,t.delegate=m,t.domify=C,t.event=b,t.matches=function(t,e){return t&&"function"==typeof t.matches&&t.matches(e)||!1},t.query=function(t,e){return(e=e||document).querySelector(t)},t.queryAll=function(t,e){return(e=e||document).querySelectorAll(t)},t.remove=function(t){t.parentNode&&t.parentNode.removeChild(t)},Object.defineProperty(t,"__esModule",{value:!0})}));