-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubtle.min.js
More file actions
8 lines (8 loc) · 15.6 KB
/
Copy pathsubtle.min.js
File metadata and controls
8 lines (8 loc) · 15.6 KB
1
2
3
4
5
6
7
8
/*!
* subtle.js
* jQuery without jQuery and some more helpful functions
*
* author: Stefan Benicke <stefan.benicke@gmail.com>
* version: 1.0.0
*/
window.$=function(i,c,s,l){var t,r,o=(EventTarget||Node).prototype,a=NodeList.prototype,e=Element.prototype,n=Object.prototype.toString,f=new WeakMap,p={addEventListener:o.addEventListener,removeEventListener:o.removeEventListener},u=!1,d=function(){var e=!1,t={};Object.defineProperty(t,"passive",{get:function(){return!(e=!0)}});try{c.addEventListener("test",null,t),c.removeEventListener("test",null,t)}catch(e){}return e}();try{r=new EventTarget}catch(e){r=i.createElement("i")}try{i.querySelector(":scope"),u=!0}catch(e){}if(e.matches||(e.matches=e.msMatchesSelector||e.webkitMatchesSelector),e.closest||(e.closest=function(e){var t=this;if(!i.documentElement.contains(t))return null;do{if(t.matches(e))return t;t=t.parentElement||t.parentNode}while(null!==t&&t.nodeType===Node.ELEMENT_NODE);return null}),a.forEach||(a.forEach=Array.prototype.forEach),"function"!=typeof c.CustomEvent&&(c.CustomEvent=function(e,t){var r;return t=t||{bubbles:!1,cancelable:!1,detail:null},(r=i.createEvent("CustomEvent")).initCustomEvent(e,t.bubbles,t.cancelable,t.detail),r}),void 0===c.DOMException){var y={IndexSizeError:{code:1,type:"INDEX_SIZE_ERR"},DOMStringSizeError:{code:2,type:"DOMSTRING_SIZE_ERR"},HierarchyRequestError:{code:3,type:"HIERARCHY_REQUEST_ERR"},WrongDocumentError:{code:4,type:"WRONG_DOCUMENT_ERR"},InvalidCharacterError:{code:5,type:"INVALID_CHARACTER_ERR"},NoDataAllowedError:{code:6,type:"NO_DATA_ALLOWED_ERR"},NoModificationAllowedError:{code:7,type:"NO_MODIFICATION_ALLOWED_ERR"},NotFoundError:{code:8,type:"NOT_FOUND_ERR"},NotSupportedError:{code:9,type:"NOT_SUPPORTED_ERR"},InuseAttributeError:{code:10,type:"INUSE_ATTRIBUTE_ERR"},InvalidStateError:{code:11,type:"INVALID_STATE_ERR"},SyntaxError:{code:12,type:"SYNTAX_ERR"},InvalidModificationError:{code:13,type:"INVALID_MODIFICATION_ERR"},NamespaceError:{code:14,type:"NAMESPACE_ERR"},InvalidAccessError:{code:15,type:"INVALID_ACCESS_ERR"},ValidationError:{code:16,type:"VALIDATION_ERR"},TypeMismatchError:{code:17,type:"TYPE_MISMATCH_ERR"},SecurityError:{code:18,type:"SECURITY_ERR"},NetworkError:{code:19,type:"NETWORK_ERR"},AbortError:{code:20,type:"ABORT_ERR"},URLMismatchError:{code:21,type:"URL_MISMATCH_ERR"},QuotaExceededError:{code:22,type:"QUOTA_EXCEEDED_ERR"},TimeoutError:{code:23,type:"TIMEOUT_ERR"},InvalidNodeTypeError:{code:24,type:"INVALID_NODE_TYPE_ERR"},DataCloneError:{code:25,type:"DATA_CLONE_ERR"}};function h(e,t){if(!(this instanceof h))throw new TypeError("Failed to construct 'DOMException': Please use the 'new' operator, this DOM object constructor cannot be called as a function.");var r=y[t];this.code=r?r.code:0,this.name="String"===s.type(t)?t:"Error",this.message="String"===s.type(e)?e:""}h.prototype=Object.create(Error.prototype),Object.defineProperty(h.prototype,"constructor",{value:h,writable:!0,configurable:!0}),Object.keys(y).forEach(function(e){var t=y[e];h[t.type]=h.prototype[t.type]=t.code}),c.DOMException=h}function E(e){var t=typeof e;return"object"==t?!!e.capture:"boolean"==t&&e}function b(e,t,r){return e.some(function(e){return e.listener===t&&e.useCapture===r})}function m(e,t,r){var n;return"function"==typeof c.Event?n=new Event(e,{bubbles:t,cancelable:r}):(n=i.createEvent("Event")).initEvent(e,t,r),n}return c.on=o.on=function(e,n,o){var a,i,t,c=this;return"object"==typeof(t=o)&&t.once?c.once(e,n,o):("Object"===s.type(e)?s.each(e,function(e,t){c.on(t,e)}):(f.has(c)?a=f.get(c):(a={},f.set(c,a)),i=E(o),e.trim().split(/\s+/).forEach(function(e){var t,r;-1<e.indexOf(".")&&(t=(e=e.split(".")).slice(1),e=e[0]),(r=a[e])||(a[e]=r=[]),b(r,n,i)||(r.push({type:e,listener:n,handler:null,useCapture:i,classNames:t,once:!1}),p.addEventListener.call(c,e,n,d?o:i))})),c)},c.once=o.once=function(e,o,a){var i,c,u=this;return"Object"===s.type(e)?s.each(e,function(e,t){u.once(t,e)}):(f.has(u)?i=f.get(u):(i={},f.set(u,i)),c=E(a),e.trim().split(/\s+/).forEach(function(t){var e,r,n;-1<t.indexOf(".")&&(e=(t=t.split(".")).slice(1),t=t[0]),(n=i[t])||(i[t]=n=[]),b(n,o,c)||(r=function(e){u.off(t,o,a),o.call(u,e)},n.push({type:t,listener:o,handler:r,useCapture:c,classNames:e,once:!0}),p.addEventListener.call(u,t,r,d?a:c))})),u},c.off=o.off=function(e,o,a){var i,c,u=this,t=s.type(e);return f.has(u)?(i=f.get(u),e===l?(s.each(i,function(e,t){s.each(e,function(e){p.removeEventListener.call(u,t,e.handler||e.listener,e.useCapture)})}),f.delete(u)):"Object"===t?s.each(e,function(e,t){u.off(t,e)}):"String"!==t||(c=[],a=E(a),e.trim().split(/\s+/).forEach(function(e){var n,t,r;if(-1<e.indexOf(".")&&(e=e.split("."),n=e.slice(1),e=e[0]),t=function(e,t,r){e&&(Array.isArray(n)&&0<n.length&&!n.every(function(e){return this.includes(e)},e.classNames)||o!==l&&e.listener!==o||e.useCapture===a&&(p.removeEventListener.call(u,e.type,e.handler||e.listener,e.useCapture),r[t]=null,c.push(e.type)))},""!==e){if(!(r=i[e]))return;s.each(r,t)}else s.each(i,function(e){e&&s.each(e,t)})}),0<c.length&&(s.each(c,function(e){var t=i[e];t&&(s.spliceInPlace(t,null),0===t.length&&delete i[e])}),0===Object.keys(i).length&&f.delete(u))),u):u},e.find=function(e){var t,r=/^\s*:scope\b/i;return"String"===s.type(e)&&(u?!1===r.test(e)&&(e=":scope "+e):(t=s.getCSSSelector(this),e=r.test(e)?e.replace(r,t):t+" "+e)),this.querySelectorAll(e||null)},a.on=function(t,r,n){return this.forEach(function(e){e.on(t,r,n)}),this},a.once=function(t,r,n){return this.forEach(function(e){e.once(t,r,n)}),this},a.off=function(t,r,n){return this.forEach(function(e){e.off(t,r,n)}),this},c.trigger=o.trigger=function(e,t){var r;return t!==l?(r=new CustomEvent(e,{bubbles:!0,cancelable:!0,detail:t})).data=r.detail:r=m(e,!0,!0),this.dispatchEvent(r),this},a.trigger=function(t,r){return this.forEach(function(e){e.trigger(t,r)}),this},o.addEventListener=function(e,t,r){var n,o,a,i=this,c=E(r);return c?(i.once(e,t,r),n):(n=p.addEventListener.call(i,e,t,r),f.has(i)?o=f.get(i):(o={},f.set(i,o)),(a=o[e])||(o[e]=a=[]),b(a,t,c)||a.push({type:e,listener:t,handler:null,useCapture:c,classNames:l,once:!1}),n)},o.removeEventListener=function(e,n,t){var r,o,a,i,c=p.removeEventListener.call(this,e,n,t);return f.has(this)&&(o=(r=f.get(this))[e])&&(a=E(t),s.each(o,function(e,t,r){if(e.listener===n&&e.useCapture===a)return r[t]=null,!(i=!0)}),i&&(s.spliceInPlace(o,null),0===o.length&&delete r[e],0===Object.keys(r).length&&f.delete(this))),c},s=function(e,t){var r;return t?0<(r=t.find(e)).length?r[0]:null:i.querySelector(e||null)},c.$$=function(e,t){return t?t.find(e):i.querySelectorAll(e||null)},s.noop=function(){},s.properties=function(t,r,e){var n,o=s.type(e);if("Array"===o)return e.forEach(function(e){r.hasOwnProperty(e)&&(t[e]=r[e])}),t;for(n in r)if(r.hasOwnProperty(n)){if(e&&("RegExp"===o&&!e.test(n)||"Function"===o&&!e.call(r,n)))continue;t[n]=r[n]}return t},s.attributes=function(r,n,e){var t,o,a=s.type(e);if("Array"===a)return e.forEach(function(e){var t;n.hasOwnProperty(e)&&(null===(t=n[e])?r.removeAttribute(e):r.setAttribute(e,t))}),r;for(t in n)if(n.hasOwnProperty(t)){if(e&&("RegExp"===a&&!e.test(t)||"Function"===a&&!e.call(n,t)))continue;null===(o=n[t])?r.removeAttribute(t):r.setAttribute(t,o)}return r},s.style=function(e,r,t){var n,o,a=e.style,i=s.type(t);if("Array"===i)return t.forEach(function(e){var t;r.hasOwnProperty(e)&&(t=r[e],e in a?(null===t&&(t=""),a[e]=t):null===t||""===t?a.removeProperty(e):a.setProperty(e,t))}),e;for(n in r)t&&("RegExp"===i&&!t.test(n)||"Function"===i&&!t.call(r,n))||(o=r[n],n in a?(null===o&&(o=""),a[n]=o):null===o||""===o?a.removeProperty(n):a.setProperty(n,o));return e},s.type=function(e){var t=n.call(e).match(/^\[object\s+(.*?)\]$/)[1]||"";return"Number"===t&&isNaN(e)?"NaN":t},s.each=function(e,t,r){var n,o;if("object"!=typeof e)throw new TypeError("Invalid object: "+e);if("function"!=typeof t)throw new TypeError("Invalid callback: "+t);if(Array.isArray(e)){for(n=0,o=e.length;n<o;n++)if(!1===t.call(r,e[n],n,e))return!1}else for(n in e)if(e.hasOwnProperty(n)&&!1===t.call(r,e[n],n,e))return!1;return!0},s.spliceInPlace=function(e,t){var r,n,o;if(!Array.isArray(e))throw new TypeError("Invalid array: "+e);for(n=0,o=e.length,r=0;r<o;r++)e[r]!==t&&(n<r&&(e[n]=e[r]),n++);return n!==o&&(e.length=n),e},s.extend=function(i,c,e){var t;return"object"!=typeof c||null===c||(t=Object.getOwnPropertyNames(c),"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(c))),!0===e?t.forEach(function(e){var t,r,n,o=c[e],a=s.type(o);if(o!==i&&("Array"!==s.type(c)||"length"!==e)){if((t=Object.getOwnPropertyDescriptor(c,e))&&(t.get||t.set))return i.hasOwnProperty(e)&&delete i[e],void Object.defineProperty(i,e,t);r="Object"===a?s.extend({},o,!0):"Array"===a?s.extend([],o,!0):o,t&&i.hasOwnProperty(e)&&(!(n=Object.getOwnPropertyDescriptor(i,e))||n.writable===t.writable&&n.configurable===t.configurable&&n.enumerable===t.enumerable||delete i[e]),!t||t.writable&&t.configurable&&t.enumerable?i[e]=r:Object.defineProperty(i,e,{value:r,writable:t.writable,configurable:t.configurable,enumerable:t.enumerable})}}):t.forEach(function(e){Object.defineProperty(i,e,Object.getOwnPropertyDescriptor(c,e))})),i},s.merge=function(i,c,u){var e;return"object"!=typeof c||null===c||(e=Object.getOwnPropertyNames(c),"function"==typeof Object.getOwnPropertySymbols&&(e=e.concat(Object.getOwnPropertySymbols(c))),e.forEach(function(e){var t,r,n,o=c[e],a=s.type(o);if(o!==i&&("Array"!==s.type(c)||"length"!==e)){if((t=Object.getOwnPropertyDescriptor(c,e))&&(t.get||t.set))return i.hasOwnProperty(e)&&delete i[e],void Object.defineProperty(i,e,t);if(!0===u)"Object"===a?(r=s.merge({},o,!0),i.hasOwnProperty(e)&&"Object"===s.type(i[e])&&(r=s.merge(i[e],r))):"Array"===a?(r=s.merge([],o,!0),i.hasOwnProperty(e)&&"Array"===s.type(i[e])&&(r=s.merge(i[e],r))):r=o;else{if("Object"===a&&i.hasOwnProperty(e)&&"Object"===s.type(i[e]))return void s.merge(i[e],o);if("Array"===a&&i.hasOwnProperty(e)&&"Array"===s.type(i[e]))return void s.merge(i[e],o)}t&&i.hasOwnProperty(e)&&(!(n=Object.getOwnPropertyDescriptor(i,e))||n.writable===t.writable&&n.configurable===t.configurable&&n.enumerable===t.enumerable||delete i[e]),!t||t.writable&&t.configurable&&t.enumerable?i[e]=!0===u?r:o:Object.defineProperty(i,e,{value:!0===u?r:o,writable:t.writable,configurable:t.configurable,enumerable:t.enumerable})}})),i},s.clone=function(e){return e instanceof Date?new Date(e.getTime()):e instanceof RegExp?new RegExp(e):s.extend(Object.create(Object.getPrototypeOf(e)),e,!0)},s.isPrimitive=function(e){return e!==Object(e)},s.coldCall=function(e){return function(){if("function"==typeof e)return e()}},s.deep=function(e,t){var r,n,o;try{for(n=(r=t.replace(/^\./,"").replace(/\[(["']?)([^\1]+?)\1?\]/g,".$2").split(".")).length,o=0;o<n;)e=e[r[o++]];return e}catch(e){}},s.ready=function(e){return t||(t=!0,t=new Promise(s.ready)),"function"==typeof e&&("loading"!==i.readyState?setTimeout(e):i.once("DOMContentLoaded",s.coldCall(e))),t},s.params=function(e,n){var o="URLSearchParams"===s.type(n),a=o?null:[],i=function(e,r){var t=s.type(e);"Object"!==t&&"Array"!==t?o?n.append(r,e):a.push(encodeURIComponent(r)+"="+encodeURIComponent(e)):s.each(e,function(e,t){i(e,r+"["+t+"]")})};return s.each(e,i),o?n.toString():a.join("&")},s.fetch=function(e,t){if(!e)throw new TypeError("URL parameter is mandatory and cannot be "+e);t===l&&(t={});var n=s.merge({url:new URL(e,c.location.origin),params:null,body:null,method:"GET",headers:{},signal:null},t);if(n.method=n.method.toUpperCase(),"AbortSignal"===s.type(n.signal)&&n.signal.aborted)return Promise.reject(new h("The user aborted a request","AbortError"));"Object"===s.type(n.params)&&(n.url.search+=(""===n.url.search?"?":"&")+s.params(n.params)),"URLSearchParams"===s.type(n.params)&&(n.url.search+=(""===n.url.search?"?":"&")+n.params.toString()),!1===n.cache&&(n.url.search+=(""===n.url.search?"?":"&")+"_="+Date.now());var o=new XMLHttpRequest;o.open(n.method,n.url.href,!0,n.user,n.password),s.each(t,function(e,t){if("upload"===t)o.upload&&"object"==typeof e&&s.extend(o.upload,e);else if(t in o)try{o[t]=e}catch(e){self.console&&console.error(e)}});var r=Object.keys(n.headers).map(function(e){return e.toLowerCase()});for(var a in"POST"!==n.method&&"PUT"!==n.method||("Object"===s.type(n.body)?"json"===n.type?n.body=JSON.stringify(n.body):n.body=s.params(n.body):"HTMLFormElement"===s.type(n.body)&&(n.body=new FormData(n.body)),-1===r.indexOf("content-type")&&-1<["String","URLSearchParams"].indexOf(s.type(n.body))&&("json"===n.type?o.setRequestHeader("Content-type","application/json"):o.setRequestHeader("Content-type","application/x-www-form-urlencoded"))),n.headers)n.headers[a]!==l&&o.setRequestHeader(a,n.headers[a]);return new Promise(function(e,t){var r=s.noop;if("AbortSignal"===s.type(n.signal)){if(n.signal.aborted)return void t(new h("The user aborted a request","AbortError"));n.signal.on("abort",o.abort),r=function(){n.signal&&o&&(n.signal.off(),n.signal=null)}}else n.signal=null;o.addEventListener("load",function(){r(),200<=o.status&&o.status<300||304===o.status?e(o):t(Object.defineProperty(s.extend(new Error(o.statusText),{xhr:o}),"status",{enumerable:!0,get:function(){return this.xhr.status}}))}),o.addEventListener("error",function(){r(),t(s.extend(new DOMExcpetion("Request network error","NetworkError"),{xhr:o}))}),o.addEventListener("timeout",function(){r(),t(s.extend(new h("Request timeout","TimeoutError"),{xhr:o}))}),o.addEventListener("abort",function(){r(),t(s.extend(new h("The user aborted a request","AbortError"),{xhr:o}))}),o.send("POST"===n.method||"PUT"===n.method?n.body:null)})},s.getJSON=function(e,t){var r=s.extend(t||{},{responseType:""});return s.fetch(e,r).then(function(e){return JSON.parse(e.response)})},s.getScript=function(n,e){return!0!==e?Promise.resolve():new Promise(function(e,t){var r=i.createElement("script");s.properties(r,{async:!0,onload:function(){r.parentNode&&r.parentNode.removeChild(r),e()},onerror:function(){r.parentNode&&r.parentNode.removeChild(r),t()},src:n}),i.head.appendChild(r)})},s.defer=function(){var r,n,e=new Promise(function(e,t){r=e,n=t});return e.resolve=r,e.reject=n,e},s.debounced=function(r,n,o){function a(){clearTimeout(c),c=null}function i(e,t){c=null,r.apply(e,t)}function e(){var e=arguments,t=o&&!c;clearTimeout(c),c=o?setTimeout(a,n):setTimeout(i,n,this,e),t&&r.apply(this,e)}var c;return e.cancel=a,e},s.delegated=function(n,o){var a;return function(e){var t,r;a!==l&&i.querySelector(a)===this||(a=s.getCSSSelector(this)),t=a+" "+n,(r=e.target.closest(t))&&(e.delegateTarget=this,o.call(r,e))}},s.getCSSSelector=function(e){var t,r,n,o,a;if("object"!=typeof e||!Node.prototype.isPrototypeOf(e))return!1;if(e===i)return"html";for(n=[],o=e.ownerDocument.documentElement,a=e.ownerDocument.body;e.parentNode;){if(e.id){n.unshift("#"+e.id);break}if(e===o||e===a)n.unshift(e.tagName.toLowerCase());else{for(r=e.tagName.toLowerCase(),t=1;e.previousElementSibling;t++)e=e.previousElementSibling;n.unshift(r+":nth-child("+t+")")}e=e.parentNode}return n.join(" > ")},s.on=function(e,t,r,n){return Array.isArray(e)?e.forEach(function(e){s.on(e,t,r,n)}):(o.isPrototypeOf(e)||a.isPrototypeOf(e))&&e.on(t,r,n),s},s.once=function(e,t,r,n){return Array.isArray(e)?e.forEach(function(e){s.once(e,t,r,n)}):(o.isPrototypeOf(e)||a.isPrototypeOf(e))&&e.once(t,r,n),s},s.off=function(e,t,r,n){return Array.isArray(e)?e.forEach(function(e){s.off(e,t,r,n)}):(o.isPrototypeOf(e)||a.isPrototypeOf(e))&&e.off(t,r,n),s},s.trigger=function(e,t,r){var n;return r!==l?(n=new CustomEvent(t,{bubbles:!0,cancelable:!0,detail:r})).data=n.detail:n=m(t,!0,!0),Array.isArray(e)?e.forEach(function(e){o.isPrototypeOf(e)&&e.dispatchEvent(this)},n):o.isPrototypeOf(e)&&e.dispatchEvent(n),s},s.getListeners=function(e){if(e!==l)return f.has(e)?f.get(e):{}},s.subscribe=o.on.bind(r),s.unsubscribe=o.off.bind(r),s.publish=o.trigger.bind(r),s}(document,this);