UNPKG

preact-render-to-string

Version:

Render JSX to an HTML string, with support for Preact components.

3 lines (2 loc) 9.5 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("preact")):"function"==typeof define&&define.amd?define(["exports","preact"],t):t((e||self).preactRenderToString={},e.preact)}(this,function(e,t){var n="diffed",r="__c",o="__s",i="__c",a="__k",s="__d",c="__s",u=/[\s\n\\/='"\0<>]/,l=/^(xlink|xmlns|xml)([A-Z])/,f=/^(?:accessK|auto[A-Z]|cell|ch|col|cont|cross|dateT|encT|form[A-Z]|frame|hrefL|inputM|maxL|minL|noV|playsI|popoverT|readO|rowS|src[A-Z]|tabI|useM|item[A-Z])/,p=/^ac|^ali|arabic|basel|cap|clipPath$|clipRule$|color|dominant|enable|fill|flood|font|glyph[^R]|horiz|image|letter|lighting|marker[^WUH]|overline|panose|pointe|paint|rendering|shape|stop|strikethrough|stroke|text[^L]|transform|underline|unicode|units|^v[^i]|^w|^xH/,h=new Set(["draggable","spellcheck"]);function d(e){void 0!==e.__g?e.__g|=8:e[s]=!0}function v(e){void 0!==e.__g?e.__g&=-9:e[s]=!1}function g(e){return void 0!==e.__g?!!(8&e.__g):!0===e[s]}var m=/["&<]/;function y(e){if(0===e.length||!1===m.test(e))return e;for(var t=0,n=0,r="",o="";n<e.length;n++){switch(e.charCodeAt(n)){case 34:o="&quot;";break;case 38:o="&amp;";break;case 60:o="&lt;";break;default:continue}n!==t&&(r+=e.slice(t,n)),r+=o,t=n+1}return n!==t&&(r+=e.slice(t,n)),r}var _={},b=new Set(["animation-iteration-count","border-image-outset","border-image-slice","border-image-width","box-flex","box-flex-group","box-ordinal-group","column-count","fill-opacity","flex","flex-grow","flex-negative","flex-order","flex-positive","flex-shrink","flood-opacity","font-weight","grid-column","grid-row","line-clamp","line-height","opacity","order","orphans","stop-opacity","stroke-dasharray","stroke-dashoffset","stroke-miterlimit","stroke-opacity","stroke-width","tab-size","widows","z-index","zoom"]),x=/[A-Z]/g;function k(e){var t="";for(var n in e){var r=e[n];if(null!=r&&""!==r){var o="-"==n[0]?n:_[n]||(_[n]=n.replace(x,"-$&").toLowerCase()),i=";";"number"!=typeof r||o.startsWith("--")||b.has(o)||(i="px;"),t=t+o+":"+r+i}}return t||void 0}function w(){this.__d=!0}function S(e,t){return{__v:e,context:t,props:e.props,setState:w,forceUpdate:w,__d:!0,__h:new Array(0)}}function C(e,t,n){if(!e.s){if(n instanceof A){if(!n.s)return void(n.o=C.bind(null,e,t));1&t&&(t=n.s),n=n.v}if(n&&n.then)return void n.then(C.bind(null,e,t),C.bind(null,e,2));e.s=t,e.v=n;const r=e.o;r&&r(e)}}var A=/*#__PURE__*/function(){function e(){}return e.prototype.then=function(t,n){var r=new e,o=this.s;if(o){var i=1&o?t:n;if(i){try{C(r,1,i(this.v))}catch(e){C(r,2,e)}return r}return this}return this.o=function(e){try{var o=e.v;1&e.s?C(r,1,t?t(o):o):n?C(r,1,n(o)):C(r,2,o)}catch(e){C(r,2,e)}},r},e}();function T(e){return e instanceof A&&1&e.s}function F(e,t,n){for(var r;;){var o=e();if(T(o)&&(o=o.v),!o)return i;if(o.then){r=0;break}var i=n();if(i&&i.then){if(!T(i)){r=1;break}i=i.s}if(t){var a=t();if(a&&a.then&&!T(a)){r=2;break}}}var s=new A,c=C.bind(null,s,2);return(0===r?o.then(l):1===r?i.then(u):a.then(f)).then(void 0,c),s;function u(r){i=r;do{if(t&&(a=t())&&a.then&&!T(a))return void a.then(f).then(void 0,c);if(!(o=e())||T(o)&&!o.v)return void C(s,1,i);if(o.then)return void o.then(l).then(void 0,c);T(i=n())&&(i=i.v)}while(!i||!i.then);i.then(u).then(void 0,c)}function l(e){e?(i=n())&&i.then?i.then(u).then(void 0,c):u(i):C(s,1,i)}function f(){(o=e())?o.then?o.then(l).then(void 0,c):l(o):C(s,1,i)}}function L(e,t){try{var n=e()}catch(e){return t(!0,e)}return n&&n.then?n.then(t.bind(null,!1),t.bind(null,!0)):t(!1,n)}var j,E,D,P,$={},M=[],U=Array.isArray,Z=Object.assign,W="",q="\x3c!--$s--\x3e",z="\x3c!--/$s--\x3e";function H(e){return"string"==typeof e?q+e+z:U(e)?(e.unshift(q),e.push(z),e):e&&"function"==typeof e.then?e.then(H):q+e+z}function N(e,i,s){var c=t.options[o];t.options[o]=!0,j=t.options.__b,E=t.options[n],D=t.options.__r,P=t.options.unmount;var u=t.h(t.Fragment,null);u[a]=[e];try{var l=I(e,i||$,!1,void 0,u,!1,s);return U(l)?l.join(W):l}catch(e){if(e.then)throw new Error('Use "renderToStringAsync" for suspenseful rendering.');throw e}finally{t.options[r]&&t.options[r](e,M),t.options[o]=c,M.length=0}}function B(e,t){var n,r=e.type,o=!0;return e[i]?(o=!1,(n=e[i]).state=n[c]):n=new r(e.props,t),e[i]=n,n.__v=e,n.props=e.props,n.context=t,d(n),null==n.state&&(n.state=$),null==n[c]&&(n[c]=n.state),r.getDerivedStateFromProps?n.state=Z({},n.state,r.getDerivedStateFromProps(n.props,n.state)):o&&n.componentWillMount?(n.componentWillMount(),n.state=n[c]!==n.state?n[c]:n.state):!o&&n.componentWillUpdate&&n.componentWillUpdate(),D&&D(e),n.render(n.props,n.state,t)}function I(e,n,r,o,s,m,_){if(null==e||!0===e||!1===e||e===W)return W;var b=typeof e;if("object"!=b)return"function"==b?W:"string"==b?y(e):e+W;if(U(e)){var x,w=W;s[a]=e;for(var C=e.length,A=0;A<C;A++){var T=e[A];if(null!=T&&"boolean"!=typeof T){var F,L=I(T,n,r,o,s,m,_);"string"==typeof L?w+=L:(x||(x=new Array(C)),w&&x.push(w),w=W,U(L)?(F=x).push.apply(F,L):x.push(L))}}return x?(w&&x.push(w),x):w}if(void 0!==e.constructor)return W;e.__=s,j&&j(e);var M=e.type,q=e.props;if("function"==typeof M){var z,N,R,V=n;if(M===t.Fragment){if("tpl"in q){for(var G=W,J=0;J<q.tpl.length;J++)if(G+=q.tpl[J],q.exprs&&J<q.exprs.length){var Q=q.exprs[J];if(null==Q)continue;"object"!=typeof Q||void 0!==Q.constructor&&!U(Q)?G+=Q:G+=I(Q,n,r,o,e,m,_)}return G}if("UNSTABLE_comment"in q)return"\x3c!--"+y(q.UNSTABLE_comment)+"--\x3e";N=q.children}else{if(null!=(z=M.contextType)){var X=n[z.__c];V=X?X.props.value:z.__}var Y=M.prototype&&"function"==typeof M.prototype.render;if(Y)N=/**#__NOINLINE__**/B(e,V),R=e[i];else{e[i]=R=/**#__NOINLINE__**/S(e,V);for(var ee=0;g(R)&&ee++<25;){v(R),D&&D(e);try{N=M.call(R,q,V)}catch(t){throw m&&t&&"function"==typeof t.then&&(e._suspended=!0),t}}d(R)}if(null!=R.getChildContext&&(n=Z({},n,R.getChildContext())),Y&&t.options.errorBoundaries&&(M.getDerivedStateFromError||R.componentDidCatch)){N=null!=N&&N.type===t.Fragment&&null==N.key&&null==N.props.tpl?N.props.children:N;try{return I(N,n,r,o,e,m,!1)}catch(a){return M.getDerivedStateFromError&&(R[c]=M.getDerivedStateFromError(a)),R.componentDidCatch&&R.componentDidCatch(a,$),g(R)?(N=B(e,n),null!=(R=e[i]).getChildContext&&(n=Z({},n,R.getChildContext())),I(N=null!=N&&N.type===t.Fragment&&null==N.key&&null==N.props.tpl?N.props.children:N,n,r,o,e,m,_)):W}finally{E&&E(e),P&&P(e)}}}N=null!=N&&N.type===t.Fragment&&null==N.key&&null==N.props.tpl?N.props.children:N;try{var te=I(N,n,r,o,e,m,_);return E&&E(e),t.options.unmount&&t.options.unmount(e),e._suspended?H(te):te}catch(i){if(!m&&_&&_.onError){var ne=function t(i){return _.onError(i,e,function(e,i){try{return I(e,n,r,o,i,m,_)}catch(e){return t(e)}})}(i);if(void 0!==ne)return ne;var re=t.options.__e;return re&&re(i,e),W}if(!m)throw i;if(!i||"function"!=typeof i.then)throw i;return i.then(function t(){try{var i=I(N,n,r,o,e,m,_);return e._suspended?H(i):i}catch(e){if(!e||"function"!=typeof e.then)throw e;return e.then(t)}})}}var oe,ie="<"+M,ae=W;for(var se in q){var ce=q[se];if("function"!=typeof(ce=K(ce)?ce.value:ce)||"class"===se||"className"===se){switch(se){case"children":oe=ce;continue;case"key":case"ref":case"__self":case"__source":continue;case"htmlFor":if("for"in q)continue;se="for";break;case"className":if("class"in q)continue;se="class";break;case"defaultChecked":se="checked";break;case"defaultSelected":se="selected";break;case"defaultValue":case"value":switch(se="value",M){case"textarea":oe=ce;continue;case"select":o=ce;continue;case"option":o!=ce||"selected"in q||(ie+=" selected")}break;case"dangerouslySetInnerHTML":ae=ce&&ce.__html;continue;case"style":"object"==typeof ce&&(ce=k(ce));break;case"acceptCharset":se="accept-charset";break;case"httpEquiv":se="http-equiv";break;default:if(l.test(se))se=se.replace(l,"$1:$2").toLowerCase();else{if(u.test(se))continue;"-"!==se[4]&&!h.has(se)||null==ce?r?p.test(se)&&(se="panose1"===se?"panose-1":se.replace(/([A-Z])/g,"-$1").toLowerCase()):f.test(se)&&(se=se.toLowerCase()):ce+=W}}null!=ce&&!1!==ce&&(ie=!0===ce||ce===W?ie+" "+se:ie+" "+se+'="'+("string"==typeof ce?y(ce):ce+W)+'"')}}if(u.test(M))throw new Error(M+" is not a valid HTML tag name in "+ie+">");if(ae||("string"==typeof oe?ae=y(oe):null!=oe&&!1!==oe&&!0!==oe&&(ae=I(oe,n,"svg"===M||"foreignObject"!==M&&r,o,e,m,_))),E&&E(e),P&&P(e),!ae&&O.has(M))return ie+"/>";var ue="</"+M+">",le=ie+">";return U(ae)?[le].concat(ae,[ue]):"string"!=typeof ae?[le,ae,ue]:le+ae+ue}var O=new Set(["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"]),R=N,V=N;function K(e){return null!==e&&"object"==typeof e&&"function"==typeof e.peek&&"value"in e}e.default=N,e.render=R,e.renderToStaticMarkup=V,e.renderToString=N,e.renderToStringAsync=function(e,i){try{var s=t.options[o];t.options[o]=!0,j=t.options.__b,E=t.options[n],D=t.options.__r,P=t.options.unmount;var c=t.h(t.Fragment,null);return c[a]=[e],Promise.resolve(L(function(){return Promise.resolve(I(e,i||$,!1,void 0,c,!0,void 0)).then(function(e){var t,n=function(){if(U(e)){var n=function(){var e=o.join(W);return t=1,e},r=0,o=e,i=F(function(){return!!o.some(function(e){return e&&"function"==typeof e.then})&&r++<25},void 0,function(){return Promise.resolve(Promise.all(o)).then(function(e){o=e.flat()})});return i&&i.then?i.then(n):n()}}();return n&&n.then?n.then(function(n){return t?n:e}):t?n:e})},function(n,i){if(t.options[r]&&t.options[r](e,M),t.options[o]=s,M.length=0,n)throw i;return i}))}catch(e){return Promise.reject(e)}}}); //# sourceMappingURL=index.umd.js.map