preact-render-to-string
Version:
Render JSX to an HTML string, with support for Preact components.
3 lines (2 loc) • 10.8 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("preact")):"function"==typeof define&&define.amd?define(["exports","preact"],e):e((t||self).preactRenderToString={},t.preact)}(this,function(t,e){if("function"!=typeof Symbol){var n=0;Symbol=function(t){return"@@"+t+ ++n},Symbol.for=function(t){return"@@"+t}}var r=/^(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/,o=/[\s\n\\/='"\0<>]/,i=/^xlink:?./,a=/["&<]/;function c(t){if(0===t.length||!1===a.test(t))return t;for(var e=0,n=0,r="",o="";n<t.length;n++){switch(t.charCodeAt(n)){case 34:o=""";break;case 38:o="&";break;case 60:o="<";break;default:continue}n!==e&&(r+=t.slice(e,n)),r+=o,e=n+1}return n!==e&&(r+=t.slice(e,n)),r}var s=function(t,e){return String(t).replace(/(\n+)/g,"$1"+(e||"\t"))},l=function(t,e,n){return String(t).length>(e||40)||!n&&-1!==String(t).indexOf("\n")||-1!==String(t).indexOf("<")},f={},u=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"]),p=/[A-Z]/g;function d(t){var e="";for(var n in t){var r=t[n];if(null!=r&&""!==r){var o="-"==n[0]?n:f[n]||(f[n]=n.replace(p,"-$&").toLowerCase()),i=";";"number"!=typeof r||o.startsWith("--")||u.has(o)||(i="px;"),e=e+o+":"+r+i}}return e||void 0}function g(t,e){return Array.isArray(e)?e.reduce(g,t):null!=e&&!1!==e&&t.push(e),t}function y(){this.__d=!0}function b(t,e){return{__v:t,context:e,props:t.props,setState:y,forceUpdate:y,__d:!0,__h:[]}}var m=[],h=[];function v(t,n,r,o){var i=e.options.__s;e.options.__s=!0;try{return _(t,n||{},r,o)}finally{e.options.__c&&e.options.__c(t,h),e.options.__s=i,h.length=0}}function _(t,n,a,f,u,p){if(null==t||"boolean"==typeof t)return"";if("object"!=typeof t)return"function"==typeof t?"":c(t+"");var y=a.pretty,m=y&&"string"==typeof y?y:"\t";if(Array.isArray(t)){for(var h="",v=0;v<t.length;v++)y&&v>0&&(h+="\n"),h+=_(t[v],n,a,f,u,p);return h}if(void 0!==t.constructor)return"";e.options.__b&&e.options.__b(t);var j,S=t.type,k=t.props,w=!1;if("function"==typeof S){if(w=!0,!a.shallow||!f&&!1!==a.renderRootComponent){if(S===e.Fragment){var O=[];return g(O,t.props.children),_(O,n,a,!1!==a.shallowHighOrder,u,p)}var A,F=t.__c=b(t,n),C=e.options.__r,E=n,M=S.contextType;if(null!=M){var H=n[M.__c];E=H?H.props.value:M.__}if(S.prototype&&"function"==typeof S.prototype.render)(F=t.__c=new S(k,E)).__v=t,F._dirty=F.__d=!0,F.props=k,null==F.state&&(F.state={}),null==F._nextState&&null==F.__s&&(F._nextState=F.__s=F.state),F.context=E,S.getDerivedStateFromProps?F.state=Object.assign({},F.state,S.getDerivedStateFromProps(F.props,F.state)):F.componentWillMount&&(F.componentWillMount(),F.state=F._nextState!==F.state?F._nextState:F.__s!==F.state?F.__s:F.state),C&&C(t),A=F.render(F.props,F.state,F.context);else for(var N=0;F.__d&&N++<25;)F.__d=!1,C&&C(t),A=S.call(t.__c,k,E);F.getChildContext&&(n=Object.assign({},n,F.getChildContext()));var D=_(A,n,a,!1!==a.shallowHighOrder,u,p);return e.options.diffed&&e.options.diffed(t),D}S=(j=S).displayName||j!==Function&&j.name||x(j)}var I,T,W="<"+S;if(k){var $=Object.keys(k);a&&!0===a.sortAttributes&&$.sort();for(var U=0;U<$.length;U++){var L=$[U],P=k[L];if("children"!==L){if(!o.test(L)&&(a&&a.allAttributes||"key"!==L&&"ref"!==L&&"__self"!==L&&"__source"!==L)){if("defaultValue"===L)L="value";else if("defaultChecked"===L)L="checked";else if("defaultSelected"===L)L="selected";else if("className"===L){if(void 0!==k.class)continue;L="class"}else u&&i.test(L)&&(L=L.toLowerCase().replace(/^xlink:?/,"xlink:"));if("htmlFor"===L){if(k.for)continue;L="for"}"style"===L&&P&&"object"==typeof P&&(P=d(P)),"a"===L[0]&&"r"===L[1]&&"boolean"==typeof P&&(P=String(P));var R=a.attributeHook&&a.attributeHook(L,P,n,a,w);if(R||""===R)W+=R;else if("dangerouslySetInnerHTML"===L)T=P&&P.__html;else if("textarea"===S&&"value"===L)I=P;else if((P||0===P||""===P)&&"function"!=typeof P){if(!(!0!==P&&""!==P||(P=L,a&&a.xml))){W=W+" "+L;continue}if("value"===L){if("select"===S){p=P;continue}"option"===S&&p==P&&void 0===k.selected&&(W+=" selected")}W=W+" "+L+'="'+c(P+"")+'"'}}}else I=P}}if(y){var z=W.replace(/\n\s*/," ");z===W||~z.indexOf("\n")?y&&~W.indexOf("\n")&&(W+="\n"):W=z}if(W+=">",o.test(S))throw new Error(S+" is not a valid HTML tag name in "+W);var J,q=r.test(S)||a.voidElements&&a.voidElements.test(S),V=[];if(T)y&&l(T)&&(T="\n"+m+s(T,m)),W+=T;else if(null!=I&&g(J=[],I).length){for(var B=y&&~W.indexOf("\n"),G=!1,Z=0;Z<J.length;Z++){var K=J[Z];if(null!=K&&!1!==K){var Q=_(K,n,a,!0,"svg"===S||"foreignObject"!==S&&u,p);if(y&&!B&&l(Q)&&(B=!0),Q)if(y){var X=Q.length>0&&"<"!=Q[0];G&&X?V[V.length-1]+=Q:V.push(Q),G=X}else V.push(Q)}}if(y&&B)for(var Y=V.length;Y--;)V[Y]="\n"+m+s(V[Y],m)}if(e.options.diffed&&e.options.diffed(t),V.length||T)W+=V.join("");else if(a&&a.xml)return W.substring(0,W.length-1)+" />";return!q||J||T?(y&&~W.indexOf("\n")&&(W+="\n"),W=W+"</"+S+">"):W=W.replace(/>$/," />"),W}function x(t){var e=(Function.prototype.toString.call(t).match(/^\s*function\s+([^( ]+)/)||"")[1];if(!e){for(var n=-1,r=m.length;r--;)if(m[r]===t){n=r;break}n<0&&(n=m.push(t)-1),e="UnnamedComponent"+n}return e}const j=/(\\|\"|\')/g;var S=function(t){return t.replace(j,"\\$1")};const k=Object.prototype.toString,w=Date.prototype.toISOString,O=Error.prototype.toString,A=RegExp.prototype.toString,F=Symbol.prototype.toString,C=/^Symbol\((.*)\)(.*)$/,E=/\n/gi,M=Object.getOwnPropertySymbols||(t=>[]);function H(t){return"[object Array]"===t||"[object ArrayBuffer]"===t||"[object DataView]"===t||"[object Float32Array]"===t||"[object Float64Array]"===t||"[object Int8Array]"===t||"[object Int16Array]"===t||"[object Int32Array]"===t||"[object Uint8Array]"===t||"[object Uint8ClampedArray]"===t||"[object Uint16Array]"===t||"[object Uint32Array]"===t}function N(t){return t!=+t?"NaN":0===t&&1/t<0?"-0":""+t}function D(t){return""===t.name?"[Function anonymous]":"[Function "+t.name+"]"}function I(t){return F.call(t).replace(C,"Symbol($1)")}function T(t){return"["+O.call(t)+"]"}function W(t){if(!0===t||!1===t)return""+t;if(void 0===t)return"undefined";if(null===t)return"null";const e=typeof t;if("number"===e)return N(t);if("string"===e)return'"'+S(t)+'"';if("function"===e)return D(t);if("symbol"===e)return I(t);const n=k.call(t);return"[object WeakMap]"===n?"WeakMap {}":"[object WeakSet]"===n?"WeakSet {}":"[object Function]"===n||"[object GeneratorFunction]"===n?D(t,min):"[object Symbol]"===n?I(t):"[object Date]"===n?w.call(t):"[object Error]"===n?T(t):"[object RegExp]"===n?A.call(t):"[object Arguments]"===n&&0===t.length?"Arguments []":H(n)&&0===t.length?t.constructor.name+" []":t instanceof Error&&T(t)}function $(t,e,n,r,o,i,a,c,s,l){let f="";if(t.length){f+=o;const u=n+e;for(let n=0;n<t.length;n++)f+=u+V(t[n],e,u,r,o,i,a,c,s,l),n<t.length-1&&(f+=","+r);f+=o+n}return"["+f+"]"}function U(t,e,n,r,o,i,a,c,s,l){return(l?"":"Arguments ")+$(t,e,n,r,o,i,a,c,s,l)}function L(t,e,n,r,o,i,a,c,s,l){return(l?"":t.constructor.name+" ")+$(t,e,n,r,o,i,a,c,s,l)}function P(t,e,n,r,o,i,a,c,s,l){let f="Map {";const u=t.entries();let p=u.next();if(!p.done){f+=o;const t=n+e;for(;!p.done;)f+=t+V(p.value[0],e,t,r,o,i,a,c,s,l)+" => "+V(p.value[1],e,t,r,o,i,a,c,s,l),p=u.next(),p.done||(f+=","+r);f+=o+n}return f+"}"}function R(t,e,n,r,o,i,a,c,s,l){let f=(l?"":t.constructor?t.constructor.name+" ":"Object ")+"{",u=Object.keys(t).sort();const p=M(t);if(p.length&&(u=u.filter(t=>!("symbol"==typeof t||"[object Symbol]"===k.call(t))).concat(p)),u.length){f+=o;const p=n+e;for(let n=0;n<u.length;n++){const d=u[n];f+=p+V(d,e,p,r,o,i,a,c,s,l)+": "+V(t[d],e,p,r,o,i,a,c,s,l),n<u.length-1&&(f+=","+r)}f+=o+n}return f+"}"}function z(t,e,n,r,o,i,a,c,s,l){let f="Set {";const u=t.entries();let p=u.next();if(!p.done){f+=o;const t=n+e;for(;!p.done;)f+=t+V(p.value[1],e,t,r,o,i,a,c,s,l),p=u.next(),p.done||(f+=","+r);f+=o+n}return f+"}"}function J(t,e,n,r,o,i,a,c,s,l){if((i=i.slice()).indexOf(t)>-1)return"[Circular]";i.push(t);const f=++c>a;if(!f&&t.toJSON&&"function"==typeof t.toJSON)return V(t.toJSON(),e,n,r,o,i,a,c,s,l);const u=k.call(t);return"[object Arguments]"===u?f?"[Arguments]":U(t,e,n,r,o,i,a,c,s,l):H(u)?f?"[Array]":L(t,e,n,r,o,i,a,c,s,l):"[object Map]"===u?f?"[Map]":P(t,e,n,r,o,i,a,c,s,l):"[object Set]"===u?f?"[Set]":z(t,e,n,r,o,i,a,c,s,l):"object"==typeof t?f?"[Object]":R(t,e,n,r,o,i,a,c,s,l):void 0}function q(t,e,n,r,o,i,a,c,s,l){let f,u=!1;for(let e=0;e<s.length;e++)if(f=s[e],f.test(t)){u=!0;break}return!!u&&f.print(t,function(t){return V(t,e,n,r,o,i,a,c,s,l)},function(t){const r=n+e;return r+t.replace(E,"\n"+r)},{edgeSpacing:o,spacing:r})}function V(t,e,n,r,o,i,a,c,s,l){return W(t)||q(t,e,n,r,o,i,a,c,s,l)||J(t,e,n,r,o,i,a,c,s,l)}const B={indent:2,min:!1,maxDepth:Infinity,plugins:[]};function G(t){if(Object.keys(t).forEach(t=>{if(!B.hasOwnProperty(t))throw new Error("prettyFormat: Invalid option: "+t)}),t.min&&void 0!==t.indent&&0!==t.indent)throw new Error("prettyFormat: Cannot run with min option and indent")}function Z(t){const e={};return Object.keys(B).forEach(n=>e[n]=t.hasOwnProperty(n)?t[n]:B[n]),e.min&&(e.indent=0),e}function K(t){return new Array(t+1).join(" ")}var Q=function(t,e){let n,r;e?(G(e),e=Z(e)):e=B;const o=e.min?" ":"\n",i=e.min?"":"\n";if(e&&e.plugins.length){n=K(e.indent),r=[];var a=q(t,n,"",o,i,r,e.maxDepth,0,e.plugins,e.min);if(a)return a}return W(t)||(n||(n=K(e.indent)),r||(r=[]),J(t,n,"",o,i,r,e.maxDepth,0,e.plugins,e.min))},X={test:function(t){return t&&"object"==typeof t&&"type"in t&&"props"in t&&"key"in t},print:function(t,e,n){return v(t,X.context,X.opts,!0)}},Y={plugins:[X]},tt={attributeHook:function(t,e,n,r,o){var i=typeof e;if("dangerouslySetInnerHTML"===t)return!1;if(null==e||"function"===i&&!r.functions)return"";if(r.skipFalseAttributes&&!o&&(!1===e||("class"===t||"style"===t)&&""===e))return"";var a="string"==typeof r.pretty?r.pretty:"\t";return"string"!==i?("function"!==i||r.functionNames?(X.context=n,X.opts=r,~(e=Q(e,Y)).indexOf("\n")&&(e=s("\n"+e,a)+"\n")):e="Function",s("\n"+t+"={"+e+"}",a)):"\n"+a+t+'="'+c(e)+'"'},jsx:!0,xml:!1,functions:!0,functionNames:!0,skipFalseAttributes:!0,pretty:" "};function et(t,e,n){var r=Object.assign({},tt,n||{});return r.jsx||(r.attributeHook=null),v(t,e,r)}var nt={shallow:!0};t.default=et,t.render=et,t.shallowRender=function(t,e,n){return et(t,e,Object.assign({},nt,n||{}))}});
//# sourceMappingURL=jsx.umd.js.map