tiny-svg.min.js 5.7 KB

1
  1. !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TinySVG={})}(this,(function(e){"use strict";function t(e,t){if(e.ownerDocument!==t.ownerDocument)try{return t.ownerDocument.importNode(e,!0)}catch(e){}return e}function r(e,r){return r.appendChild(t(e,r))}var n={"alignment-baseline":1,"baseline-shift":1,clip:1,"clip-path":1,"clip-rule":1,color:1,"color-interpolation":1,"color-interpolation-filters":1,"color-profile":1,"color-rendering":1,cursor:1,direction:1,display:1,"dominant-baseline":1,"enable-background":1,fill:1,"fill-opacity":1,"fill-rule":1,filter:1,"flood-color":1,"flood-opacity":1,font:1,"font-family":1,"font-size":2,"font-size-adjust":1,"font-stretch":1,"font-style":1,"font-variant":1,"font-weight":1,"glyph-orientation-horizontal":1,"glyph-orientation-vertical":1,"image-rendering":1,kerning:1,"letter-spacing":1,"lighting-color":1,marker:1,"marker-end":1,"marker-mid":1,"marker-start":1,mask:1,opacity:1,overflow:1,"pointer-events":1,"shape-rendering":1,"stop-color":1,"stop-opacity":1,stroke:1,"stroke-dasharray":1,"stroke-dashoffset":1,"stroke-linecap":1,"stroke-linejoin":1,"stroke-miterlimit":1,"stroke-opacity":1,"stroke-width":2,"text-anchor":1,"text-decoration":1,"text-rendering":1,"unicode-bidi":1,visibility:1,"word-spacing":1,"writing-mode":1};function o(e,t,r){var o=t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),i=n[o];i?(2===i&&"number"==typeof r&&(r=String(r)+"px"),e.style[o]=r):e.setAttributeNS(null,t,r)}function i(e,t,r){if("string"==typeof t){if(void 0===r)return function(e,t){return n[t]?e.style[t]:e.getAttributeNS(null,t)}(e,t);o(e,t,r)}else!function(e,t){var r,n,i=Object.keys(t);for(r=0;n=i[r];r++)o(e,n,t[n])}(e,t);return e}const a=Object.prototype.toString;function s(e){if(!e||!e.nodeType)throw new Error("A DOM element reference is required");this.el=e,this.list=e.classList}function c(e){var t=e.parentNode;return t&&t.removeChild(e),e}function l(e){for(var t;t=e.firstChild;)c(t);return e}s.prototype.add=function(e){return this.list.add(e),this},s.prototype.remove=function(e){return"[object RegExp]"==a.call(e)?this.removeMatching(e):(this.list.remove(e),this)},s.prototype.removeMatching=function(e){const t=this.array();for(let r=0;r<t.length;r++)e.test(t[r])&&this.remove(t[r]);return this},s.prototype.toggle=function(e,t){return void 0!==t?t!==this.list.toggle(e,t)&&this.list.toggle(e):this.list.toggle(e),this},s.prototype.array=function(){return Array.from(this.list)},s.prototype.has=s.prototype.contains=function(e){return this.list.contains(e)};var u="http://www.w3.org/2000/svg",f='<svg xmlns="'+u+'"';function p(e){var t=!1;"<svg"===e.substring(0,4)?-1===e.indexOf(u)&&(e=f+e.substring(4)):(e=f+">"+e+"</svg>",t=!0);var r=function(e){var t;return(t=new DOMParser).async=!1,t.parseFromString(e,"text/xml")}(e);if(!t)return r;for(var n=document.createDocumentFragment(),o=r.firstChild;o.firstChild;)n.appendChild(o.firstChild);return n}function d(e,t){var r;return"<"===e.charAt(0)?(r=p(e).firstChild,r=document.importNode(r,!0)):r=document.createElementNS(u,e),t&&i(r,t),r}var h=null;function m(){return null===h&&(h=d("svg")),h}function g(e,t){var r,n,o=Object.keys(t);for(r=0;n=o[r];r++)e[n]=t[n];return e}var v=/([&<>]{1})/g,y=/([\n\r"]{1})/g,b={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"'"};function w(e,t){return e.replace(t,(function(e,t){return b[t]||t}))}function x(e,t){var r,n,o,i,a;switch(e.nodeType){case 3:t.push(w(e.textContent,v));break;case 1:if(t.push("<",e.tagName),e.hasAttributes())for(r=0,n=(o=e.attributes).length;r<n;++r)i=o.item(r),t.push(" ",i.name,'="',w(i.value,y),'"');if(e.hasChildNodes()){for(t.push(">"),r=0,n=(a=e.childNodes).length;r<n;++r)x(a.item(r),t);t.push("</",e.tagName,">")}else t.push("/>");break;case 8:t.push("\x3c!--",w(e.nodeValue,v),"--\x3e");break;case 4:t.push("<![CDATA[",e.nodeValue,"]]>");break;default:throw new Error("unable to handle node "+e.nodeType)}return t}function k(e,t){var n=p(t);if(l(e),t){(function(e){return"#document-fragment"===e.nodeName})(n)||(n=n.documentElement);for(var o,i=(o=n.childNodes,Array.prototype.slice.call(o)),a=0;a<i.length;a++)r(i[a],e)}}function S(e,r){return r.insertBefore(t(e,r),r.firstChild||null)}function C(e,t){return t instanceof SVGMatrix?e.createSVGTransformFromMatrix(t):t}e.append=function(e,t){return r(t,e),e},e.appendTo=r,e.attr=i,e.classes=function(e){return new s(e)},e.clear=l,e.clone=function(e){return e.cloneNode(!0)},e.create=d,e.createMatrix=function(e,t,r,n,o,i){var a=m().createSVGMatrix();switch(arguments.length){case 0:return a;case 1:return g(a,e);case 6:return g(a,{a:e,b:t,c:r,d:n,e:o,f:i})}},e.createPoint=function(e,t){var r=m().createSVGPoint();switch(arguments.length){case 0:return r;case 2:e={x:e,y:t}}return g(r,e)},e.createTransform=function(e){return e?m().createSVGTransformFromMatrix(e):m().createSVGTransform()},e.innerSVG=function(e,t){if(void 0!==t){try{k(e,t)}catch(e){throw new Error("error parsing SVG: "+e.message)}return e}return function(e){for(var t=e.firstChild,r=[];t;)x(t,r),t=t.nextSibling;return r.join("")}(e)},e.off=function(e,t,r,n){e.removeEventListener(t,r,n)},e.on=function(e,t,r,n){e.addEventListener(t,r,n)},e.prepend=function(e,t){return S(t,e),e},e.prependTo=S,e.remove=c,e.replace=function(e,r){return e.parentNode.replaceChild(t(r,e),e),r},e.select=function(e,t){return e.querySelector(t)},e.selectAll=function(e,t){var r=e.querySelectorAll(t);return[].map.call(r,(function(e){return e}))},e.transform=function(e,t){var r=e.transform.baseVal;return t&&(Array.isArray(t)||(t=[t]),function(e,t){var r,n;for(e.clear(),r=0;n=t[r];r++)e.appendItem(C(e,n))}(r,t)),r.consolidate()},Object.defineProperty(e,"__esModule",{value:!0})}));