min-dom.min.js 4.2 KB

1
  1. !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.MinDom={})}(this,function(e){"use strict";function n(e,t){if(i)return e.indexOf(t);for(var r=0;r<e.length;++r)if(e[r]===t)return r;return-1}var i=[].indexOf,t=/\s+/,o=Object.prototype.toString;function r(e){if(!e||!e.nodeType)throw new Error("A DOM element reference is required");this.el=e,this.list=e.classList}r.prototype.add=function(e){if(this.list)return this.list.add(e),this;var t=this.array();return~n(t,e)||t.push(e),this.el.className=t.join(" "),this},r.prototype.remove=function(e){if("[object RegExp]"==o.call(e))return this.removeMatching(e);if(this.list)return this.list.remove(e),this;var t=this.array(),r=n(t,e);return~r&&t.splice(r,1),this.el.className=t.join(" "),this},r.prototype.removeMatching=function(e){for(var t=this.array(),r=0;r<t.length;r++)e.test(t[r])&&this.remove(t[r]);return this},r.prototype.toggle=function(e,t){return this.list?void 0!==t&&t===this.list.toggle(e,t)||this.list.toggle(e):void 0!==t?t?this.add(e):this.remove(e):this.has(e)?this.remove(e):this.add(e),this},r.prototype.array=function(){var e=(this.el.getAttribute("class")||"").replace(/^\s+|\s+$/g,"").split(t);return""===e[0]&&e.shift(),e},r.prototype.has=r.prototype.contains=function(e){return this.list?this.list.contains(e):!!~n(this.array(),e)};var l="undefined"!=typeof Element?Element.prototype:{},s=l.matches||l.matchesSelector||l.webkitMatchesSelector||l.mozMatchesSelector||l.msMatchesSelector||l.oMatchesSelector,a=function(e,t){if(!e||1!==e.nodeType)return!1;if(s)return s.call(e,t);for(var r=e.parentNode.querySelectorAll(t),n=0;n<r.length;n++)if(r[n]==e)return!0;return!1};function d(e,t,r){for(var n=r?e:e.parentNode;n&&n.nodeType!==document.DOCUMENT_NODE&&n.nodeType!==document.DOCUMENT_FRAGMENT_NODE;){if(a(n,t))return n;n=n.parentNode}return a(n,t)?n:null}var u=window.addEventListener?"addEventListener":"attachEvent",c=window.removeEventListener?"removeEventListener":"detachEvent",f="addEventListener"!=u?"on":"",h={bind:function(e,t,r,n){return e[u](f+t,r,n||!1),r},unbind:function(e,t,r,n){return e[c](f+t,r,n||!1),r}},p=["focus","blur"];function v(e,t){if("string"!=typeof e)throw new TypeError("String expected");t=t||document;var r=/<([\w:]+)/.exec(e);if(!r)return t.createTextNode(e);e=e.replace(/^\s+|\s+$/g,"");var n=r[1];if("body"==n){return(i=t.createElement("html")).innerHTML=e,i.removeChild(i.lastChild)}var i,o=b[n]||b._default,l=o[0],s=o[1],a=o[2];for((i=t.createElement("div")).innerHTML=s+e+a;l--;)i=i.lastChild;if(i.firstChild==i.lastChild)return i.removeChild(i.firstChild);for(var d=t.createDocumentFragment();i.firstChild;)d.appendChild(i.removeChild(i.firstChild));return d}var m,y={bind:function(r,n,e,i,t){return-1!==p.indexOf(e)&&(t=!0),h.bind(r,e,function(e){var t=e.target||e.srcElement;e.delegateTarget=d(t,n,!0),e.delegateTarget&&i.call(r,e)},t)},unbind:function(e,t,r,n){return-1!==p.indexOf(t)&&(n=!0),h.unbind(e,t,r,n)}},g=!1;"undefined"!=typeof document&&((m=document.createElement("div")).innerHTML=' <link/><table></table><a href="/a">a</a><input type="checkbox"/>',g=!m.getElementsByTagName("link").length,m=void 0);var b={legend:[1,"<fieldset>","</fieldset>"],tr:[2,"<table><tbody>","</tbody></table>"],col:[2,"<table><tbody></tbody><colgroup>","</colgroup></table>"],_default:g?[1,"X<div>","</div>"]:[0,"",""]};b.td=b.th=[3,"<table><tbody><tr>","</tr></tbody></table>"],b.option=b.optgroup=[1,'<select multiple="multiple">',"</select>"],b.thead=b.tbody=b.colgroup=b.caption=b.tfoot=[1,"<table>","</table>"],b.polyline=b.ellipse=b.polygon=b.circle=b.text=b.line=b.path=b.rect=b.g=[1,'<svg xmlns="http://www.w3.org/2000/svg" version="1.1">',"</svg>"],e.attr=function(e,t,r){return 2==arguments.length?e.getAttribute(t):null===r?e.removeAttribute(t):(e.setAttribute(t,r),e)},e.classes=function(e){return new r(e)},e.clear=function(e){for(var t;e.childNodes.length;)t=e.childNodes[0],e.removeChild(t);return e},e.closest=d,e.delegate=y,e.domify=v,e.event=h,e.matches=a,e.query=function(e,t){return(t=t||document).querySelector(e)},e.queryAll=function(e,t){return(t=t||document).querySelectorAll(e)},e.remove=function(e){e.parentNode&&e.parentNode.removeChild(e)},Object.defineProperty(e,"__esModule",{value:!0})});