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