UNPKG

inferno-server

Version:

Provides methods to render HTML strings from Inferno elements

2 lines (1 loc) 15.1 kB
!function(e,r){"object"===typeof exports&&"undefined"!==typeof module?r(exports,require("inferno"),require("stream")):"function"===typeof define&&define.amd?define(["exports","inferno","stream"],r):r(((e="undefined"!==typeof globalThis?globalThis:e||self).Inferno=e.Inferno||{},e.Inferno.Server=e.Inferno.Server||{}),e.Inferno,e.stream)}(this,function(e,r,t){"use strict";var n=Array.isArray;function o(e){var r=typeof e;return"string"===r||"number"===r}function i(e){return void 0===e||null===e}function u(e){return null===e||!1===e||!0===e||void 0===e}function s(e){return"function"===typeof e}function a(e){return"string"===typeof e}function c(e){return"number"===typeof e}function l(e){return null===e}function f(e){return void 0===e}function d(e){throw e||(e="a runtime error occured! Use Inferno in development environment to find the error."),new Error("Inferno Error: "+e)}function h(){return h=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},h.apply(null,arguments)}var p=/["'&<>]/;function v(e){if(!p.test(e))return e;for(var r="",t="",n=0,o=0;o<e.length;++o){switch(e.charCodeAt(o)){case 34:t="&quot;";break;case 39:t="&#039;";break;case 38:t="&amp;";break;case 60:t="&lt;";break;case 62:t="&gt;";break;default:continue}o>n&&(r+=e.slice(n,o)),r+=t,n=o+1}return r+e.slice(n,o)}var y=":A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD",g=new RegExp("^["+y+"]["+y+"\\-.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040]*$"),m={},b={};function T(e){if(void 0!==b[e])return!0;if(void 0!==m[e])return!1;if(g.test(e))return b[e]=!0,!0;return m[e]=!0,!1}var x=new Set(["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"]);function O(e,r,t){if(e.constructor.getDerivedStateFromProps)return h({},t,e.constructor.getDerivedStateFromProps(r,t));return t}function S(e,t){var n=e.props||r.EMPTY_OBJ;return 32768&e.flags?e.type.render(n,e.ref,t):e.type(n,t)}function P(e){if(a(e))return v(e);var r="";for(var t in e){var n=e[t];o(n)&&(r+=t+":"+n+";")}return r}function Q(e){var r=P(e);if(r)return' style="'+r+'"';return""}function k(){return k=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},k.apply(null,arguments)}function F(e,t,o){var f=e.flags,h=e.type,p=e.props||r.EMPTY_OBJ,y=e.children;if(0!==(14&f)){if(4&f){var g,m=new h(p,o),b=Boolean(h.getDerivedStateFromProps);if(m.$BS=!1,m.$SSR=!0,s(m.getChildContext)&&(g=m.getChildContext()),g=i(g)?o:k({},o,g),m.props===r.EMPTY_OBJ&&(m.props=p),m.context=o,!b&&s(m.componentWillMount)){m.$BR=!0,m.componentWillMount(),m.$BR=!1;var P=m.$PS;if(P){var w=m.state;if(null===w)m.state=P;else for(var j in P)w[j]=P[j];m.$PSS=!1,m.$PS=null}}b&&(m.state=O(m,p,m.state));var _=m.render(p,m.state,m.context);if(u(_))return"\x3c!--!--\x3e";if(a(_))return v(_);if(c(_))return _+"";return F(_,e,g)}var N=S(e,o);if(u(N))return"\x3c!--!--\x3e";if(a(N))return v(N);if(c(N))return N+"";return F(N,e,o)}if(0!==(481&f)){var C,B="<"+h,M=x.has(h),$=e.className;if(a($)?B+=' class="'+v($)+'"':c($)&&(B+=' class="'+$+'"'),!l(p)){for(var E in p){var D=p[E];switch(E){case"dangerouslySetInnerHTML":C=D.__html;break;case"style":i(p.style)||(B+=Q(p.style));break;case"children":case"className":break;case"defaultValue":p.value||(B+=' value="'+(a(D)?v(D):D)+'"');break;case"defaultChecked":p.checked||!0!==D||(B+=' checked="'+D+'"');break;default:T(E)&&(a(D)?B+=" "+E+'="'+v(D)+'"':c(D)?B+=" "+E+'="'+D+'"':!0===D&&(B+=" "+E))}}"option"===h&&"undefined"!==typeof p.value&&p.value===t.props.value&&(B+=" selected")}if(M)B+=">";else{B+=">";var R=e.childFlags;if(2===R)B+=F(y,e,o);else if(12&R)for(var I=0,V=y.length;I<V;++I)B+=F(y[I],e,o);else 16===R?B+=""===y?" ":v(y):C&&(B+=C);M||(B+="</"+h+">")}if(String(h).match(/[\s\n/='"\0<>]/))throw B;return B}if(0!==(16&f))return""===y?" ":v(y);if(n(e)||0!==(8192&f)){var A=e.childFlags;if(2===A||n(e)&&0===e.length)return"\x3c!--!--\x3e";if(12&A||n(e)){for(var G=n(e)?e:y,W="",J=0,Y=G.length;J<Y;++J)W+=F(G[J],e,o);return W}}else d();return""}function w(e){return F(e,{},{})}function j(){return j=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},j.apply(null,arguments)}function _(e){var r=e.$PS;if(r){var t=e.state;e.state=null===t?r:j({},t,r),e.$PS=null}e.$BR=!1}function N(){return N=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},N.apply(null,arguments)}function C(e,r){return C=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,r){return e.__proto__=r,e},C(e,r)}var B=function(e){function t(r){var t;return(t=e.call(this)||this).collector=[1/0],t.promises=[],t.pushQueue=t.pushQueue.bind(t),r&&t.renderVNodeToQueue(r,null,null),t}var o,h;h=e,(o=t).prototype=Object.create(h.prototype),o.prototype.constructor=o,C(o,h);var p=t.prototype;return p._read=function(){setTimeout(this.pushQueue,0)},p.addToQueue=function(e,r){if(i(r))"string"===typeof e&&this.collector.length-1===0?this.push(e):"string"===typeof e&&"string"===typeof this.collector[this.collector.length-2]?this.collector[this.collector.length-2]+=e:this.collector.splice(-1,0,e);else{var t=this.promises[r].length-1;"string"===typeof this.promises[r][t]&&"string"===typeof e?this.promises[r][t]+=e:this.promises[r].push(e)}},p.pushQueue=function(){var e=this,r=this.collector[0];"string"===typeof r?(this.push(r),this.collector.shift()):r&&("object"===typeof r||s(r))&&s(r.then)?(r.then(function(r){var t;(t=e.collector).splice.apply(t,[0,1].concat(e.promises[r])),e.promises[r]=null,setTimeout(e.pushQueue,0)}),this.collector[0]=null):r===1/0&&this.emit("end")},p.renderVNodeToQueue=function(e,t,o){var h=this,p=e.flags,y=e.type,g=e.props||r.EMPTY_OBJ,m=e.children;if((14&p)>0)if(4&p){var b,P=new y(g,t),k=Boolean(y.getDerivedStateFromProps);if(P.$BS=!1,P.$SSR=!0,f(P.getChildContext)||(b=P.getChildContext()),i(b)||(t=N({},t,b)),P.props===r.EMPTY_OBJ&&(P.props=g),P.context=t,!k&&s(P.componentWillMount)&&(P.$BR=!0,P.componentWillMount(),_(P)),s(P.getInitialProps)){var F=P.getInitialProps(P.props,P.context);if(F){if(Promise.resolve(F)===F){var w=this.promises.push([])-1;return void this.addToQueue(F.then(function(e){"object"===typeof e&&(P.props=N({},P.props,e));var r=P.render(P.props,P.state,P.context);return u(r)?h.addToQueue("\x3c!--!--\x3e",w):a(r)?h.addToQueue(v(r),w):c(r)?h.addToQueue(r+"",w):h.renderVNodeToQueue(r,P.context,w),setTimeout(h.pushQueue,0),w}),o)}P.props=N({},P.props,F)}}k&&(P.state=O(P,g,P.state));var j=P.render(P.props,P.state,P.context);u(j)?this.addToQueue("\x3c!--!--\x3e",o):a(j)?this.addToQueue(v(j),o):c(j)?this.addToQueue(j+"",o):this.renderVNodeToQueue(j,t,o)}else{var C=S(e,t);u(C)?this.addToQueue("\x3c!--!--\x3e",o):a(C)?this.addToQueue(v(C),o):c(C)?this.addToQueue(C+"",o):this.renderVNodeToQueue(C,t,o)}else if((481&p)>0){var B,M="<"+y,$=x.has(y),E=e.className;if(a(E)?M+=' class="'+v(E)+'"':c(E)&&(M+=' class="'+E+'"'),!l(g))for(var D in g){var R=g[D];switch(D){case"dangerouslySetInnerHTML":B=R.__html;break;case"style":i(g.style)||(M+=Q(g.style));break;case"children":case"className":break;case"defaultValue":g.value||(M+=' value="'+(a(R)?v(R):R)+'"');break;case"defaultChecked":g.checked||!0!==R||(M+=' checked="'+R+'"');break;default:T(D)&&(a(R)?M+=" "+D+'="'+v(R)+'"':c(R)?M+=" "+D+'="'+R+'"':!0===R&&(M+=" "+D))}}if(M+=">",String(y).match(/[\s\n/='"\0<>]/))throw M;if($)this.addToQueue(M,o);else{var I=e.childFlags;if(2===I)return this.addToQueue(M,o),this.renderVNodeToQueue(m,t,o),void this.addToQueue("</"+y+">",o);if(16===I)return this.addToQueue(M,o),this.addToQueue(""===m?" ":v(m+""),o),void this.addToQueue("</"+y+">",o);if(12&I){this.addToQueue(M,o);for(var V=0,A=m.length;V<A;++V)this.renderVNodeToQueue(m[V],t,o);return void this.addToQueue("</"+y+">",o)}if(B)return void this.addToQueue(M+B+"</"+y+">",o);$||this.addToQueue(M+"</"+y+">",o)}}else if((16&p)>0)this.addToQueue(""===m?" ":v(m),o);else if(n(e)||0!==(8192&p)){var G=e.childFlags;if(2===G||n(e)&&0===e.length)this.addToQueue("\x3c!--!--\x3e",o);else if(12&G||n(e))for(var W=n(e)?e:e.children,J=0,Y=W.length;J<Y;++J)this.renderVNodeToQueue(W[J],t,o)}else d()},t}(t.Readable);function M(e){return new B(e)}function $(){return $=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},$.apply(null,arguments)}function E(){/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */var e,r,t="function"==typeof Symbol?Symbol:{},n=t.iterator||"@@iterator",o=t.toStringTag||"@@toStringTag";function i(t,n,o,i){var a=n&&n.prototype instanceof s?n:s,c=Object.create(a.prototype);return D(c,"_invoke",function(t,n,o){var i,s,a,c=0,l=o||[],f=!1,d={p:0,n:0,v:e,a:h,f:h.bind(e,4),d:function(r,t){return i=r,s=0,a=e,d.n=t,u}};function h(t,n){for(s=t,a=n,r=0;!f&&c&&!o&&r<l.length;r++){var o,i=l[r],h=d.p,p=i[2];t>3?(o=p===n)&&(a=i[(s=i[4])?5:(s=3,3)],i[4]=i[5]=e):i[0]<=h&&((o=t<2&&h<i[1])?(s=0,d.v=n,d.n=i[1]):h<p&&(o=t<3||i[0]>n||n>p)&&(i[4]=t,i[5]=n,d.n=p,s=0))}if(o||t>1)return u;throw f=!0,n}return function(o,l,p){if(c>1)throw TypeError("Generator is already running");for(f&&1===l&&h(l,p),s=l,a=p;(r=s<2?e:a)||!f;){i||(s?s<3?(s>1&&(d.n=-1),h(s,a)):d.n=a:d.v=a);try{if(c=2,i){if(s||(o="next"),r=i[o]){if(!(r=r.call(i,a)))throw TypeError("iterator result is not an object");if(!r.done)return r;a=r.value,s<2&&(s=0)}else 1===s&&(r=i.return)&&r.call(i),s<2&&(a=TypeError("The iterator does not provide a '"+o+"' method"),s=1);i=e}else if((r=(f=d.n<0)?a:t.call(n,d))!==u)break}catch(r){i=e,s=1,a=r}finally{c=1}}return{value:r,done:f}}}(t,o,i),!0),c}var u={};function s(){}function a(){}function c(){}r=Object.getPrototypeOf;var l=[][n]?r(r([][n]())):(D(r={},n,function(){return this}),r),f=c.prototype=s.prototype=Object.create(l);function d(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,c):(e.__proto__=c,D(e,o,"GeneratorFunction")),e.prototype=Object.create(f),e}return a.prototype=c,D(f,"constructor",c),D(c,"constructor",a),a.displayName="GeneratorFunction",D(c,o,"GeneratorFunction"),D(f),D(f,o,"Generator"),D(f,n,function(){return this}),D(f,"toString",function(){return"[object Generator]"}),(E=function(){return{w:i,m:d}})()}function D(e,r,t,n){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}D=function(e,r,t,n){function i(r,t){D(e,r,function(e){return this._invoke(r,t,e)})}r?o?o(e,r,{value:t,enumerable:!n,configurable:!n,writable:!n}):e[r]=t:(i("next",0),i("throw",1),i("return",2))},D(e,r,t,n)}function R(e,r,t,n,o,i,u){try{var s=e[i](u),a=s.value}catch(e){return void t(e)}s.done?r(a):Promise.resolve(a).then(n,o)}function I(e){return function(){var r=this,t=arguments;return new Promise(function(n,o){var i=e.apply(r,t);function u(e){R(i,n,o,u,s,"next",e)}function s(e){R(i,n,o,u,s,"throw",e)}u(void 0)})}}function V(e,r){return V=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,r){return e.__proto__=r,e},V(e,r)}var A=Promise.resolve(),G=function(e){function r(r){var t;return(t=e.call(this)||this).initNode=void 0,t.started=!1,t.initNode=r,t}var t,o;o=e,(t=r).prototype=Object.create(o.prototype),t.prototype.constructor=t,V(t,o);var f=r.prototype;return f._read=function(){var e=this;if(this.started)return;this.started=!0,A.then(function(){return e.renderNode(e.initNode,null)}).then(function(){e.push(null)}).catch(function(r){e.emit("error",r)})},f.renderNode=function(e,r){var t=e.flags;if((14&t)>0)return this.renderComponent(e,r,4&t);if((481&t)>0)return this.renderElement(e,r);if(n(e)||0!==(8192&t))return this.renderArrayOrFragment(e,r);this.renderText(e)},f.renderArrayOrFragment=function(e,r){var t=this,o=e.childFlags;if(2===o||n(e)&&0===e.length)return this.push("\x3c!--!--\x3e");if(12&o||n(e))return(n(e)?e:e.children).reduce(function(){var e=I(E().m(function e(n,o){return E().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,n.then(I(E().m(function e(){return E().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,Promise.resolve(t.renderNode(o,r)).then(function(){return!!(16&o.flags)});case 1:return e.a(2,e.v)}},e)})));case 1:return e.a(2,e.v)}},e)}));return function(r,t){return e.apply(this,arguments)}}(),Promise.resolve(!1))},f.renderComponent=function(e,r,t){var n=this,o=e.type,l=e.props;if(!t){var f=S(e,r);if(u(f))return this.push("\x3c!--!--\x3e");if(a(f))return this.push(v(f));if(c(f))return this.push(f+"");return this.renderNode(f,r)}var d,h=new o(l,r),p=Boolean(o.getDerivedStateFromProps);return h.$BS=!1,h.$SSR=!0,s(h.getChildContext)&&(d=h.getChildContext()),i(d)||(r=$({},r,d)),h.context=r,h.$BR=!0,Promise.resolve(!p&&(null==h.componentWillMount?void 0:h.componentWillMount())).then(function(){_(h),p&&(h.state=O(h,l,h.state));var e=h.render(h.props,h.state,h.context);if(u(e))return n.push("\x3c!--!--\x3e");if(a(e))return n.push(v(e));if(c(e))return n.push(e+"");return n.renderNode(e,r)})},f.renderChildren=function(e,r,t){var n=this;if(2===t)return this.renderNode(e,r);if(16===t)return this.push(""===e?" ":v(e+""));if(12&t)return e.reduce(function(){var e=I(E().m(function e(t,o){return E().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,t.then(I(E().m(function e(){return E().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,Promise.resolve(n.renderNode(o,r)).then(function(){return!!(16&o.flags)});case 1:return e.a(2,e.v)}},e)})));case 1:return e.a(2,e.v)}},e)}));return function(r,t){return e.apply(this,arguments)}}(),Promise.resolve(!1))},f.renderText=function(e){this.push(""===e.children?" ":v(e.children))},f.renderElement=function(e,r){var t,n=this,o=e.type,u=e.props,s="<"+o,f=x.has(o),d=e.className;if(a(d)?s+=' class="'+v(d)+'"':c(d)&&(s+=' class="'+d+'"'),!l(u))for(var h in u){var p=u[h];switch(h){case"dangerouslySetInnerHTML":t=p.__html;break;case"style":i(u.style)||(s+=Q(u.style));break;case"children":case"className":break;case"defaultValue":u.value||(s+=' value="'+(a(p)?v(p):p)+'"');break;case"defaultChecked":u.checked||!0!==p||(s+=' checked="'+p+'"');break;default:if(T(h)){a(p)?s+=" "+h+'="'+v(p)+'"':c(p)?s+=" "+h+'="'+p+'"':!0===p&&(s+=" "+h);break}}}if(s+=">",this.push(s),String(o).match(/[\s\n/='"\0<>]/))throw s;if(f)return;if(t)return this.push(t),void this.push("</"+o+">");var y=e.childFlags;if(1===y)return void this.push("</"+o+">");return Promise.resolve(this.renderChildren(e.children,r,y)).then(function(){n.push("</"+o+">")})},r}(t.Readable);function W(e){return new G(e)}e.RenderQueueStream=B,e.RenderStream=G,e.renderToStaticMarkup=w,e.renderToString=w,e.streamAsStaticMarkup=W,e.streamAsString=W,e.streamQueueAsStaticMarkup=M,e.streamQueueAsString=M});