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