UNPKG

preact-render-to-string

Version:

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

3 lines (2 loc) 9.2 kB
var e=require("preact"),t="diffed",n="__c",r="__s",o="__c",i="__k",a="__d",s="__s",c=/[\s\n\\/='"\0<>]/,u=/^(xlink|xmlns|xml)([A-Z])/,l=/^(?: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/,f=new Set(["draggable","spellcheck"]);function h(e){void 0!==e.__g?e.__g|=8:e[a]=!0}function d(e){void 0!==e.__g?e.__g&=-9:e[a]=!1}function v(e){return void 0!==e.__g?!!(8&e.__g):!0===e[a]}var g=/["&<]/;function m(e){if(0===e.length||!1===g.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 _={},y=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"]),b=/[A-Z]/g;function x(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(b,"-$&").toLowerCase()),i=";";"number"!=typeof r||o.startsWith("--")||y.has(o)||(i="px;"),t=t+o+":"+r+i}}return t||void 0}function k(){this.__d=!0}function w(e,t){return{__v:e,context:t,props:e.props,setState:k,forceUpdate:k,__d:!0,__h:new Array(0)}}function C(e,t,n){if(!e.s){if(n instanceof S){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 S=/*#__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 A(e){return e instanceof S&&1&e.s}function F(e,t,n){for(var r;;){var o=e();if(A(o)&&(o=o.v),!o)return i;if(o.then){r=0;break}var i=n();if(i&&i.then){if(!A(i)){r=1;break}i=i.s}if(t){var a=t();if(a&&a.then&&!A(a)){r=2;break}}}var s=new S,c=C.bind(null,s,2);return(0===r?o.then(l):1===r?i.then(u):a.then(p)).then(void 0,c),s;function u(r){i=r;do{if(t&&(a=t())&&a.then&&!A(a))return void a.then(p).then(void 0,c);if(!(o=e())||A(o)&&!o.v)return void C(s,1,i);if(o.then)return void o.then(l).then(void 0,c);A(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 p(){(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 T,E,j,D,P={},$=[],M=Array.isArray,U=Object.assign,Z="",W="\x3c!--$s--\x3e",q="\x3c!--/$s--\x3e";function z(o,a,s){var c=e.options[r];e.options[r]=!0,T=e.options.__b,E=e.options[t],j=e.options.__r,D=e.options.unmount;var u=e.h(e.Fragment,null);u[i]=[o];try{var l=N(o,a||P,!1,void 0,u,!1,s);return M(l)?l.join(Z):l}catch(e){if(e.then)throw new Error('Use "renderToStringAsync" for suspenseful rendering.');throw e}finally{e.options[n]&&e.options[n](o,$),e.options[r]=c,$.length=0}}function H(e,t){var n,r=e.type,i=!0;return e[o]?(i=!1,(n=e[o]).state=n[s]):n=new r(e.props,t),e[o]=n,n.__v=e,n.props=e.props,n.context=t,h(n),null==n.state&&(n.state=P),null==n[s]&&(n[s]=n.state),r.getDerivedStateFromProps?n.state=U({},n.state,r.getDerivedStateFromProps(n.props,n.state)):i&&n.componentWillMount?(n.componentWillMount(),n.state=n[s]!==n.state?n[s]:n.state):!i&&n.componentWillUpdate&&n.componentWillUpdate(),j&&j(e),n.render(n.props,n.state,t)}function N(t,n,r,a,g,_,y){if(null==t||!0===t||!1===t||t===Z)return Z;var b=typeof t;if("object"!=b)return"function"==b?Z:"string"==b?m(t):t+Z;if(M(t)){var k,C=Z;g[i]=t;for(var S=t.length,A=0;A<S;A++){var F=t[A];if(null!=F&&"boolean"!=typeof F){var L,$=N(F,n,r,a,g,_,y);"string"==typeof $?C+=$:(k||(k=new Array(S)),C&&k.push(C),C=Z,M($)?(L=k).push.apply(L,$):k.push($))}}return k?(C&&k.push(C),k):C}if(void 0!==t.constructor)return Z;t.__=g,T&&T(t);var z=t.type,I=t.props;if("function"==typeof z){var O,V,K,G=n;if(z===e.Fragment){if("tpl"in I){for(var J=Z,Q=0;Q<I.tpl.length;Q++)if(J+=I.tpl[Q],I.exprs&&Q<I.exprs.length){var X=I.exprs[Q];if(null==X)continue;"object"!=typeof X||void 0!==X.constructor&&!M(X)?J+=X:J+=N(X,n,r,a,t,_,y)}return J}if("UNSTABLE_comment"in I)return"\x3c!--"+m(I.UNSTABLE_comment)+"--\x3e";V=I.children}else{if(null!=(O=z.contextType)){var Y=n[O.__c];G=Y?Y.props.value:O.__}var ee=z.prototype&&"function"==typeof z.prototype.render;if(ee)V=/**#__NOINLINE__**/H(t,G),K=t[o];else{t[o]=K=/**#__NOINLINE__**/w(t,G);for(var te=0;v(K)&&te++<25;){d(K),j&&j(t);try{V=z.call(K,I,G)}catch(e){throw _&&(t._suspended=!0),e}}h(K)}if(null!=K.getChildContext&&(n=U({},n,K.getChildContext())),ee&&e.options.errorBoundaries&&(z.getDerivedStateFromError||K.componentDidCatch)){V=null!=V&&V.type===e.Fragment&&null==V.key&&null==V.props.tpl?V.props.children:V;try{return N(V,n,r,a,t,_,!1)}catch(i){return z.getDerivedStateFromError&&(K[s]=z.getDerivedStateFromError(i)),K.componentDidCatch&&K.componentDidCatch(i,P),v(K)?(V=H(t,n),null!=(K=t[o]).getChildContext&&(n=U({},n,K.getChildContext())),N(V=null!=V&&V.type===e.Fragment&&null==V.key&&null==V.props.tpl?V.props.children:V,n,r,a,t,_,y)):Z}finally{E&&E(t),D&&D(t)}}}V=null!=V&&V.type===e.Fragment&&null==V.key&&null==V.props.tpl?V.props.children:V;try{var ne=N(V,n,r,a,t,_,y);return E&&E(t),e.options.unmount&&e.options.unmount(t),t._suspended?"string"==typeof ne?W+ne+q:M(ne)?(ne.unshift(W),ne.push(q),ne):ne.then(function(e){return W+e+q}):ne}catch(o){if(!_&&y&&y.onError){var re=y.onError(o,t,function(e,t){return N(e,n,r,a,t,_,y)});if(void 0!==re)return re;var oe=e.options.__e;return oe&&oe(o,t),Z}if(!_)throw o;if(!o||"function"!=typeof o.then)throw o;return o.then(function e(){try{var o=N(V,n,r,a,t,_,y);return t._suspended?W+o+q:o}catch(t){if(!t||"function"!=typeof t.then)throw t;return t.then(e)}})}}var ie,ae="<"+z,se=Z;for(var ce in I){var ue=I[ce];if("function"!=typeof(ue=R(ue)?ue.value:ue)||"class"===ce||"className"===ce){switch(ce){case"children":ie=ue;continue;case"key":case"ref":case"__self":case"__source":continue;case"htmlFor":if("for"in I)continue;ce="for";break;case"className":if("class"in I)continue;ce="class";break;case"defaultChecked":ce="checked";break;case"defaultSelected":ce="selected";break;case"defaultValue":case"value":switch(ce="value",z){case"textarea":ie=ue;continue;case"select":a=ue;continue;case"option":a!=ue||"selected"in I||(ae+=" selected")}break;case"dangerouslySetInnerHTML":se=ue&&ue.__html;continue;case"style":"object"==typeof ue&&(ue=x(ue));break;case"acceptCharset":ce="accept-charset";break;case"httpEquiv":ce="http-equiv";break;default:if(u.test(ce))ce=ce.replace(u,"$1:$2").toLowerCase();else{if(c.test(ce))continue;"-"!==ce[4]&&!f.has(ce)||null==ue?r?p.test(ce)&&(ce="panose1"===ce?"panose-1":ce.replace(/([A-Z])/g,"-$1").toLowerCase()):l.test(ce)&&(ce=ce.toLowerCase()):ue+=Z}}null!=ue&&!1!==ue&&(ae=!0===ue||ue===Z?ae+" "+ce:ae+" "+ce+'="'+("string"==typeof ue?m(ue):ue+Z)+'"')}}if(c.test(z))throw new Error(z+" is not a valid HTML tag name in "+ae+">");if(se||("string"==typeof ie?se=m(ie):null!=ie&&!1!==ie&&!0!==ie&&(se=N(ie,n,"svg"===z||"foreignObject"!==z&&r,a,t,_,y))),E&&E(t),D&&D(t),!se&&B.has(z))return ae+"/>";var le="</"+z+">",pe=ae+">";return M(se)?[pe].concat(se,[le]):"string"!=typeof se?[pe,se,le]:pe+se+le}var B=new Set(["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"]),I=z,O=z;function R(e){return null!==e&&"object"==typeof e&&"function"==typeof e.peek&&"value"in e}exports.default=z,exports.render=I,exports.renderToStaticMarkup=O,exports.renderToString=z,exports.renderToStringAsync=function(o,a){try{var s=e.options[r];e.options[r]=!0,T=e.options.__b,E=e.options[t],j=e.options.__r,D=e.options.unmount;var c=e.h(e.Fragment,null);return c[i]=[o],Promise.resolve(L(function(){return Promise.resolve(N(o,a||P,!1,void 0,c,!0,void 0)).then(function(e){var t,n=function(){if(M(e)){var n=function(){var e=o.join(Z);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(t,i){if(e.options[n]&&e.options[n](o,$),e.options[r]=s,$.length=0,t)throw i;return i}))}catch(e){return Promise.reject(e)}}; //# sourceMappingURL=index.js.map