UNPKG

preact-render-to-string

Version:

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

3 lines (2 loc) 9.31 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:y[n]||(y[n]=n.replace(b,"-$&").toLowerCase()),i=";";"number"!=typeof r||o.startsWith("--")||_.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(e){return"string"==typeof e?W+e+q:M(e)?(e.unshift(W),e.push(q),e):e&&"function"==typeof e.then?e.then(z):W+e+q}function H(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=B(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 N(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 B(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,$=B(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 W=t.type,q=t.props;if("function"==typeof W){var H,O,R,K=n;if(W===e.Fragment){if("tpl"in q){for(var G=Z,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&&!M(Q)?G+=Q:G+=B(Q,n,r,a,t,y,_)}return G}if("UNSTABLE_comment"in q)return"\x3c!--"+m(q.UNSTABLE_comment)+"--\x3e";O=q.children}else{if(null!=(H=W.contextType)){var X=n[H.__c];K=X?X.props.value:H.__}var Y=W.prototype&&"function"==typeof W.prototype.render;if(Y)O=/**#__NOINLINE__**/N(t,K),R=t[o];else{t[o]=R=/**#__NOINLINE__**/w(t,K);for(var ee=0;v(R)&&ee++<25;){d(R),j&&j(t);try{O=W.call(R,q,K)}catch(e){throw y&&e&&"function"==typeof e.then&&(t._suspended=!0),e}}h(R)}if(null!=R.getChildContext&&(n=U({},n,R.getChildContext())),Y&&e.options.errorBoundaries&&(W.getDerivedStateFromError||R.componentDidCatch)){O=null!=O&&O.type===e.Fragment&&null==O.key&&null==O.props.tpl?O.props.children:O;try{return B(O,n,r,a,t,y,!1)}catch(i){return W.getDerivedStateFromError&&(R[s]=W.getDerivedStateFromError(i)),R.componentDidCatch&&R.componentDidCatch(i,P),v(R)?(O=N(t,n),null!=(R=t[o]).getChildContext&&(n=U({},n,R.getChildContext())),B(O=null!=O&&O.type===e.Fragment&&null==O.key&&null==O.props.tpl?O.props.children:O,n,r,a,t,y,_)):Z}finally{E&&E(t),D&&D(t)}}}O=null!=O&&O.type===e.Fragment&&null==O.key&&null==O.props.tpl?O.props.children:O;try{var te=B(O,n,r,a,t,y,_);return E&&E(t),e.options.unmount&&e.options.unmount(t),t._suspended?z(te):te}catch(o){if(!y&&_&&_.onError){var ne=function e(o){return _.onError(o,t,function(t,o){try{return B(t,n,r,a,o,y,_)}catch(t){return e(t)}})}(o);if(void 0!==ne)return ne;var re=e.options.__e;return re&&re(o,t),Z}if(!y)throw o;if(!o||"function"!=typeof o.then)throw o;return o.then(function e(){try{var o=B(O,n,r,a,t,y,_);return t._suspended?z(o):o}catch(t){if(!t||"function"!=typeof t.then)throw t;return t.then(e)}})}}var oe,ie="<"+W,ae=Z;for(var se in q){var ce=q[se];if("function"!=typeof(ce=V(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",W){case"textarea":oe=ce;continue;case"select":a=ce;continue;case"option":a!=ce||"selected"in q||(ie+=" selected")}break;case"dangerouslySetInnerHTML":ae=ce&&ce.__html;continue;case"style":"object"==typeof ce&&(ce=x(ce));break;case"acceptCharset":se="accept-charset";break;case"httpEquiv":se="http-equiv";break;default:if(u.test(se))se=se.replace(u,"$1:$2").toLowerCase();else{if(c.test(se))continue;"-"!==se[4]&&!f.has(se)||null==ce?r?p.test(se)&&(se="panose1"===se?"panose-1":se.replace(/([A-Z])/g,"-$1").toLowerCase()):l.test(se)&&(se=se.toLowerCase()):ce+=Z}}null!=ce&&!1!==ce&&(ie=!0===ce||ce===Z?ie+" "+se:ie+" "+se+'="'+("string"==typeof ce?m(ce):ce+Z)+'"')}}if(c.test(W))throw new Error(W+" is not a valid HTML tag name in "+ie+">");if(ae||("string"==typeof oe?ae=m(oe):null!=oe&&!1!==oe&&!0!==oe&&(ae=B(oe,n,"svg"===W||"foreignObject"!==W&&r,a,t,y,_))),E&&E(t),D&&D(t),!ae&&I.has(W))return ie+"/>";var ue="</"+W+">",le=ie+">";return M(ae)?[le].concat(ae,[ue]):"string"!=typeof ae?[le,ae,ue]:le+ae+ue}var I=new Set(["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"]),O=H,R=H;function V(e){return null!==e&&"object"==typeof e&&"function"==typeof e.peek&&"value"in e}exports.default=H,exports.render=O,exports.renderToStaticMarkup=R,exports.renderToString=H,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(B(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