UNPKG

preact-render-to-string

Version:

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

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