UNPKG

malevic

Version:

Malevič.js - minimalistic reactive UI library

2 lines (1 loc) 5.77 kB
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(((n="undefined"!=typeof globalThis?globalThis:n||self).Malevic=n.Malevic||{},n.Malevic.String={}))}(this,(function(n){"use strict";function t(){var n=[];return{add:function(t){return n.push(t),this},apply:function(t){for(var r,e,i=new Set,o=n.length-1;o>=0;o--)if(e=n[o],!i.has(e)){if(null!=(r=e(t)))return r;i.add(e)}return null},delete:function(t){for(var r=n.length-1;r>=0;r--)if(n[r]===t){n.splice(r,1);break}return this},empty:function(){return 0===n.length}}}function r(n,t,r){t.filter((function(t){var r=t[0];return n[r]})).forEach((function(t){var e=t[0],i=t[1];return n[e].forEach((function(n){return r(i,n)}))}))}function e(n){var t={add:function(r,e){return r[n]||(r[n]=[]),r[n].push(e),t}};return t}function i(n){return null!=n&&"object"==typeof n}function o(n){return i(n)&&null!=n.type&&null==n.nodeType}function c(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var r=[],e=function(n){n&&("string"==typeof n?r.push(n):Array.isArray(n)?n.forEach(e):"object"==typeof n&&r.push.apply(r,Object.keys(n).filter((function(t){return Boolean(n[t])}))))};return n.forEach(e),r.join(" ")}function u(n){return n.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#039;")}var a=Symbol(),l=t();function f(n,t){if(!l.empty()){var r=l.apply({attr:n,value:t});if(null!=r)return r}var e;return"class"===n&&i(t)?u(Array.isArray(t)?c.apply(void 0,t):c(t)):"style"===n&&i(t)?u((e=t,Object.keys(e).filter((function(n){return null!=e[n]})).map((function(n){return"".concat(n,": ").concat(e[n],";")})).join(" "))):!0===t?"":u(String(t))}var s=Symbol(),p=t(),h=new Set(["key","oncreate","onupdate","onrender","onremove"]);var y=function(n,t){return y=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,t){n.__proto__=t}||function(n,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(n[r]=t[r])},y(n,t)};function d(n,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=n}y(n,t),n.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}"function"==typeof SuppressedError&&SuppressedError;var v=Symbol(),g=t();var m=new Set(["area","base","br","col","embed","hr","img","input","link","menuitem","meta","param","source","track","wbr"]),b=null;function w(n){var t=n.type,r=n.props,e=n.children,i=b;b={};var o=t.apply(void 0,function(n,t,r){if(r||2===arguments.length)for(var e,i=0,o=t.length;i<o;i++)!e&&i in t||(e||(e=Array.prototype.slice.call(t,0,i)),e[i]=t[i]);return n.concat(e||Array.prototype.slice.call(t))}([r],e,!1));return b=i,o}var S=[[a,l],[s,p],[v,g]],x=function(){};function E(n,t){return"".padEnd(n.length*t,n)}var j=function(n){function t(t){var r=n.call(this)||this;return r.children=[],r.tag=t.type,r.attrs=new Map,Object.entries(t.props).filter((function(n){return!function(n,t){if(!p.empty()){var r=p.apply({attr:n,value:t});if(null!=r)return r}return h.has(n)||n.startsWith("on")||null==t||!1===t}(n[0],n[1])})).forEach((function(n){var t=n[0],e=n[1];return r.attrs.set(t,f(t,e))})),r.isVoid=function(n){if(!g.empty()){var t=g.apply(n);if(null!=t)return t}return m.has(n)}(r.tag),r}return d(t,n),t.prototype.stringify=function(n){var t=n.indent,r=n.depth,e=n.xmlSelfClosing,i=[],o=E(t,r),c=Array.from(this.attrs.entries()).map((function(n){var t=n[0],r=n[1];return""===r?t:"".concat(t,'="').concat(r,'"')})).join(" "),u=e&&0===this.children.length,a="".concat(o,"<").concat(this.tag).concat(c?" ".concat(c):"").concat(u?"/>":">");if(this.isVoid||u)i.push(a);else{var l="</".concat(this.tag,">");0===this.children.length?i.push("".concat(a).concat(l)):1===this.children.length&&this.children[0]instanceof A&&!this.children[0].text.includes("\n")?i.push("".concat(a).concat(this.children[0].stringify({indent:t,depth:0,xmlSelfClosing:e})).concat(l)):(i.push(a),this.children.forEach((function(n){return i.push(n.stringify({indent:t,depth:r+1,xmlSelfClosing:e}))})),i.push("".concat(o).concat(l)))}return i.join("\n")},t}(x),A=function(n){function t(t){var r=n.call(this)||this;return r.text=function(n){return u(n)}(t),r}return d(t,n),t.prototype.stringify=function(n){var t=E(n.indent,n.depth);return"".concat(t).concat(this.text.replace(/\n/g,"\n".concat(t)))},t}(x),O=function(n){function t(t){var r=n.call(this)||this;return r.text=u(t),r}return d(t,n),t.prototype.stringify=function(n){var t=n.indent,r=n.depth;return"".concat(E(t,r),"\x3c!--").concat(this.text,"--\x3e")},t}(x);function _(n,t){if(o(c=n)&&"string"==typeof c.type){var e=new j(n);t.children.push(e),n.children.forEach((function(n){return _(n,e)}))}else if(function(n){return o(n)&&"function"==typeof n.type}(n)){if(n.type===Array)n.children.forEach((function(n){return _(n,t)}));else r(n.type,S,(function(n,t){return n.add(t)})),_(w(n),t),function(n,t){r(n,t,(function(n,t){return n.delete(t)}))}(n.type,S)}else if("string"==typeof n){var i=new A(n);t.children.push(i)}else if(null==n){i=new O("");t.children.push(i)}else{if(!Array.isArray(n))throw new Error("Unable to stringify spec");n.forEach((function(n){return _(n,t)}))}var c}var k={stringifyAttribute:e(a),skipAttribute:e(s),isVoidTag:e(v)};n.escapeHTML=u,n.isStringifying=function(){return null!=b},n.plugins=k,n.stringify=function(n,t){var r=void 0===t?{}:t,e=r.indent,i=void 0===e?" ":e,c=r.depth,u=void 0===c?0:c,a=r.xmlSelfClosing,l=void 0!==a&&a;if(o(n)){var f=function(n){var t=new j({type:"div",props:{},children:[]});return _(n,t),t.children}(n);return f.map((function(n){return n.stringify({indent:i,depth:u,xmlSelfClosing:l})})).join("\n")}throw new Error("Not a spec")}}));