preact-render-to-string
Version:
Render JSX to an HTML string, with support for Preact components.
3 lines (2 loc) • 9.71 kB
JavaScript
var e=require("preact"),t="__s",r="__c",n="__d",o="__s",i=/[\s\n\\/='"\0<>]/,a=/^(xlink|xmlns|xml)([A-Z])/,s=/^(?: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])/,c=/^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/,l=new Set(["draggable","spellcheck"]);function u(e){void 0!==e.__g?e.__g|=8:e[n]=!0}function p(e){void 0!==e.__g?e.__g&=-9:e[n]=!1}function f(e){return void 0!==e.__g?!!(8&e.__g):!0===e[n]}var d=/["&<]/;function h(e){if(0===e.length||!1===d.test(e))return e;for(var t=0,r=0,n="",o="";r<e.length;r++){switch(e.charCodeAt(r)){case 34:o=""";break;case 38:o="&";break;case 60:o="<";break;default:continue}r!==t&&(n+=e.slice(t,r)),n+=o,t=r+1}return r!==t&&(n+=e.slice(t,r)),n}var v={},m=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"]),g=/[A-Z]/g;function _(e){var t="";for(var r in e){var n=e[r];if(null!=n&&""!==n){var o="-"==r[0]?r:v[r]||(v[r]=r.replace(g,"-$&").toLowerCase()),i=";";"number"!=typeof n||o.startsWith("--")||m.has(o)||(i="px;"),t=t+o+":"+n+i}}return t||void 0}function y(){this.__d=!0}function b(e,t){return{__v:e,context:t,props:e.props,setState:y,forceUpdate:y,__d:!0,__h:new Array(0)}}var x,k,w,C,S=function(){var e=this;this.promise=new Promise(function(t,r){e.resolve=t,e.reject=r})},E={},A=[],L=Array.isArray,P=Object.assign,T="",j="\x3c!--$s--\x3e",F="\x3c!--/$s--\x3e";function W(e){return"string"==typeof e?j+e+F:L(e)?(e.unshift(j),e.push(F),e):e&&"function"==typeof e.then?e.then(W):j+e+F}function D(r,n,o){var i=e.options[t];e.options[t]=!0,x=e.options.__b,k=e.options.diffed,w=e.options.__r,C=e.options.unmount;var a=e.h(e.Fragment,null);a.__k=[r];try{var s=$(r,n||E,!1,void 0,a,!1,o);return L(s)?s.join(T):s}catch(e){if(e.then)throw new Error('Use "renderToStringAsync" for suspenseful rendering.');throw e}finally{e.options.__c&&e.options.__c(r,A),e.options[t]=i,A.length=0}}function N(e,t){var n,i=e.type,a=!0;return e[r]?(a=!1,(n=e[r]).state=n[o]):n=new i(e.props,t),e[r]=n,n.__v=e,n.props=e.props,n.context=t,u(n),null==n.state&&(n.state=E),null==n[o]&&(n[o]=n.state),i.getDerivedStateFromProps?n.state=P({},n.state,i.getDerivedStateFromProps(n.props,n.state)):a&&n.componentWillMount?(n.componentWillMount(),n.state=n[o]!==n.state?n[o]:n.state):!a&&n.componentWillUpdate&&n.componentWillUpdate(),w&&w(e),n.render(n.props,n.state,t)}function $(t,n,d,v,m,g,y){if(null==t||!0===t||!1===t||t===T)return T;var S=typeof t;if("object"!=S)return"function"==S?T:"string"==S?h(t):t+T;if(L(t)){var A,j=T;m.__k=t;for(var F=t.length,D=0;D<F;D++){var Z=t[D];if(null!=Z&&"boolean"!=typeof Z){var q,H=$(Z,n,d,v,m,g,y);"string"==typeof H?j+=H:(A||(A=new Array(F)),j&&A.push(j),j=T,L(H)?(q=A).push.apply(q,H):A.push(H))}}return A?(j&&A.push(j),A):j}if(void 0!==t.constructor)return T;t.__=m,x&&x(t);var I=t.type,O=t.props;if("function"==typeof I){var R,z,B,V=n;if(I===e.Fragment){if("tpl"in O){for(var K=T,Y=0;Y<O.tpl.length;Y++)if(K+=O.tpl[Y],O.exprs&&Y<O.exprs.length){var G=O.exprs[Y];if(null==G)continue;"object"!=typeof G||void 0!==G.constructor&&!L(G)?K+=G:K+=$(G,n,d,v,t,g,y)}return K}if("UNSTABLE_comment"in O)return"\x3c!--"+h(O.UNSTABLE_comment)+"--\x3e";z=O.children}else{if(null!=(R=I.contextType)){var J=n[R.__c];V=J?J.props.value:R.__}var Q=I.prototype&&"function"==typeof I.prototype.render;if(Q)z=/**#__NOINLINE__**/N(t,V),B=t[r];else{t[r]=B=/**#__NOINLINE__**/b(t,V);for(var X=0;f(B)&&X++<25;){p(B),w&&w(t);try{z=I.call(B,O,V)}catch(e){throw g&&e&&"function"==typeof e.then&&(t._suspended=!0),e}}u(B)}if(null!=B.getChildContext&&(n=P({},n,B.getChildContext())),Q&&e.options.errorBoundaries&&(I.getDerivedStateFromError||B.componentDidCatch)){z=null!=z&&z.type===e.Fragment&&null==z.key&&null==z.props.tpl?z.props.children:z;try{return $(z,n,d,v,t,g,!1)}catch(i){return I.getDerivedStateFromError&&(B[o]=I.getDerivedStateFromError(i)),B.componentDidCatch&&B.componentDidCatch(i,E),f(B)?(z=N(t,n),null!=(B=t[r]).getChildContext&&(n=P({},n,B.getChildContext())),$(z=null!=z&&z.type===e.Fragment&&null==z.key&&null==z.props.tpl?z.props.children:z,n,d,v,t,g,y)):T}finally{k&&k(t),C&&C(t)}}}z=null!=z&&z.type===e.Fragment&&null==z.key&&null==z.props.tpl?z.props.children:z;try{var ee=$(z,n,d,v,t,g,y);return k&&k(t),e.options.unmount&&e.options.unmount(t),t._suspended?W(ee):ee}catch(r){if(!g&&y&&y.onError){var te=function e(r){return y.onError(r,t,function(t,r){try{return $(t,n,d,v,r,g,y)}catch(t){return e(t)}})}(r);if(void 0!==te)return te;var re=e.options.__e;return re&&re(r,t),T}if(!g)throw r;if(!r||"function"!=typeof r.then)throw r;return r.then(function e(){try{var r=$(z,n,d,v,t,g,y);return t._suspended?W(r):r}catch(t){if(!t||"function"!=typeof t.then)throw t;return t.then(e)}})}}var ne,oe="<"+I,ie=T;for(var ae in O){var se=O[ae];if("function"!=typeof(se=U(se)?se.value:se)||"class"===ae||"className"===ae){switch(ae){case"children":ne=se;continue;case"key":case"ref":case"__self":case"__source":continue;case"htmlFor":if("for"in O)continue;ae="for";break;case"className":if("class"in O)continue;ae="class";break;case"defaultChecked":ae="checked";break;case"defaultSelected":ae="selected";break;case"defaultValue":case"value":switch(ae="value",I){case"textarea":ne=se;continue;case"select":v=se;continue;case"option":v!=se||"selected"in O||(oe+=" selected")}break;case"dangerouslySetInnerHTML":ie=se&&se.__html;continue;case"style":"object"==typeof se&&(se=_(se));break;case"acceptCharset":ae="accept-charset";break;case"httpEquiv":ae="http-equiv";break;default:if(a.test(ae))ae=ae.replace(a,"$1:$2").toLowerCase();else{if(i.test(ae))continue;"-"!==ae[4]&&!l.has(ae)||null==se?d?c.test(ae)&&(ae="panose1"===ae?"panose-1":ae.replace(/([A-Z])/g,"-$1").toLowerCase()):s.test(ae)&&(ae=ae.toLowerCase()):se+=T}}null!=se&&!1!==se&&(oe=!0===se||se===T?oe+" "+ae:oe+" "+ae+'="'+("string"==typeof se?h(se):se+T)+'"')}}if(i.test(I))throw new Error(I+" is not a valid HTML tag name in "+oe+">");if(ie||("string"==typeof ne?ie=h(ne):null!=ne&&!1!==ne&&!0!==ne&&(ie=$(ne,n,"svg"===I||"foreignObject"!==I&&d,v,t,g,y))),k&&k(t),C&&C(t),!ie&&M.has(I))return oe+"/>";var ce="</"+I+">",le=oe+">";return L(ie)?[le].concat(ie,[ce]):"string"!=typeof ie?[le,ie,ce]:le+ie+ce}var M=new Set(["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"]);function U(e){return null!==e&&"object"==typeof e&&"function"==typeof e.peek&&"value"in e}function Z(e,t){return'<preact-island hidden data-target="'+e+'">'+t+"</preact-island>"}var q=function e(t){try{var r=function(){if(t.suspended.length>0){var r=[].concat(t.suspended);return Promise.resolve(Promise.all(t.suspended.map(function(e){return e.promise}))).then(function(){return t.suspended=t.suspended.filter(function(e){return!r.includes(e)}),Promise.resolve(e(t)).then(function(){})})}}();return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},H=function(e,t){var r=t.context,n=t.onWrite,o=t.abortSignal;try{r=r||{};var i={start:Date.now(),abortSignal:o,onWrite:n,onError:I,suspended:[]},a=D(e,r,i),s=i.suspended.length,c=function(){if(s>0){var e=a.lastIndexOf("</body>"),t=a.trimStart().startsWith("<html"),r=-1!==e?a.slice(0,e):a;return n((t?"<!DOCTYPE html>":"")+r),n("<div hidden>"),n('<script>(function(){class e extends HTMLElement{connectedCallback(){var e=this;if(!e.isConnected)return;let t=this.getAttribute("data-target");if(t){for(var r,a,i=document.createNodeIterator(document,128);i.nextNode();){let e=i.referenceNode;if(e.data=="preact-island:"+t?r=e:e.data=="/preact-island:"+t&&(a=e),r&&a)break}r&&a&&r.parentNode!==document&&requestAnimationFrame((()=>{for(var t=a.previousSibling;t!=r&&t&&t!=r;)a.parentNode.removeChild(t),t=a.previousSibling;for(i=r;e.firstChild;)r=e.firstChild,e.removeChild(r),i.after(r),i=r;e.parentNode.removeChild(e)}))}}}customElements.define("preact-island",e);}())<\/script>'),Promise.resolve(q(i)).then(function(){n("</div>"),-1!==e&&n(a.slice(e))})}n(a)}();return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(e){return Promise.reject(e)}};function I(e,t,n){var o=this;if(e&&e.then){for(;t=t.__;){var i=t[r];if(i&&i.__c)break}if(t){var a=t.__v,s=this.suspended.find(function(e){return e.id===a}),c=new S,l=this.abortSignal;l&&(l.aborted?c.resolve():l.addEventListener("abort",c.resolve));var u=e.then(function(){if(!l||!l.aborted){var e=n(t.props.children,t);e&&o.onWrite(Z(a,e))}},this.onError);this.suspended.push({id:a,vnode:t,promise:Promise.race([u,c.promise])});var p=n(t.props.fallback);return s?"":"\x3c!--preact-island:"+a+"--\x3e"+p+"\x3c!--/preact-island:"+a+"--\x3e"}}}exports.renderToReadableStream=function(e,t){var r=new S,n=new TextEncoder("utf-8"),o=new ReadableStream({start:function(o){H(e,{context:t,onError:function(e){r.reject(e),o.abort(e)},onWrite:function(e){o.enqueue(n.encode(e))}}).then(function(){o.close(),r.resolve()}).catch(function(e){o.error(e),r.reject(e)})}});return o.allReady=r.promise,o};
//# sourceMappingURL=index.js.map