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