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