UNPKG

preact-render-to-string

Version:

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

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