UNPKG

14.5 kBSource Map (JSON)View Raw
1{"version":3,"file":"index.js","sources":["../src/util.js","../src/index.js"],"sourcesContent":["// DOM properties that should NOT have \"px\" added when numeric\nexport const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|ows|mnc|ntw|ine[ch]|zoo|^ord/i;\n\nexport const objectKeys = Object.keys || (obj => {\n\tlet keys = [];\n\tfor (let i in obj) if (obj.hasOwnProperty(i)) keys.push(i);\n\treturn keys;\n});\n\nexport let encodeEntities = s => String(s)\n\t.replace(/&/g, '&amp;')\n\t.replace(/</g, '&lt;')\n\t.replace(/>/g, '&gt;')\n\t.replace(/\"/g, '&quot;');\n\nexport let indent = (s, char) => String(s).replace(/(\\n+)/g, '$1' + (char || '\\t'));\n\nexport let isLargeString = (s, length, ignoreLines) => (String(s).length>(length || 40) || (!ignoreLines && String(s).indexOf('\\n')!==-1) || String(s).indexOf('<')!==-1);\n\nconst JS_TO_CSS = {};\n\n// Convert an Object style to a CSSText string\nexport function styleObjToCss(s) {\n\tlet str = '';\n\tfor (let prop in s) {\n\t\tlet val = s[prop];\n\t\tif (val!=null) {\n\t\t\tif (str) str += ' ';\n\t\t\t// str += jsToCss(prop);\n\t\t\tstr += JS_TO_CSS[prop] || (JS_TO_CSS[prop] = prop.replace(/([A-Z])/g,'-$1').toLowerCase());\n\t\t\tstr += ': ';\n\t\t\tstr += val;\n\t\t\tif (typeof val==='number' && IS_NON_DIMENSIONAL.test(prop)===false) {\n\t\t\t\tstr += 'px';\n\t\t\t}\n\t\t\tstr += ';';\n\t\t}\n\t}\n\treturn str || undefined;\n}\n\n/**\n * Copy all properties from `props` onto `obj`.\n * @param {object} obj Object onto which properties should be copied.\n * @param {object} props Object from which to copy properties.\n * @returns {object}\n * @private\n */\nexport function assign(obj, props) {\n\tfor (let i in props) obj[i] = props[i];\n\treturn obj;\n}\n\n/**\n * Reconstruct Component-style `props` from a VNode.\n * Ensures default/fallback values from `defaultProps`:\n * Own-properties of `defaultProps` not present in `vnode.attributes` are added.\n * @param {import('preact').VNode} vnode The VNode to get props for\n * @returns {object} The props to use for this VNode\n */\nexport function getNodeProps(vnode) {\n\tlet props = assign({}, vnode.attributes);\n\tprops.children = vnode.children;\n\n\tlet defaultProps = vnode.nodeName.defaultProps;\n\tif (defaultProps!==undefined) {\n\t\tfor (let i in defaultProps) {\n\t\t\tif (props[i]===undefined) {\n\t\t\t\tprops[i] = defaultProps[i];\n\t\t\t}\n\t\t}\n\t}\n\n\treturn props;\n}\n","import { objectKeys, encodeEntities, indent, isLargeString, styleObjToCss, assign, getNodeProps } from './util';\n\nconst SHALLOW = { shallow: true };\n\n// components without names, kept as a hash for later comparison to return consistent UnnamedComponentXX names.\nconst UNNAMED = [];\n\nconst VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;\n\n\n/** Render Preact JSX + Components to an HTML string.\n *\t@name render\n *\t@function\n *\t@param {VNode} vnode\tJSX VNode to render.\n *\t@param {Object} [context={}]\tOptionally pass an initial context object through the render path.\n *\t@param {Object} [options={}]\tRendering options\n *\t@param {Boolean} [options.shallow=false]\tIf `true`, renders nested Components as HTML elements (`<Foo a=\"b\" />`).\n *\t@param {Boolean} [options.xml=false]\t\tIf `true`, uses self-closing tags for elements without children.\n *\t@param {Boolean} [options.pretty=false]\t\tIf `true`, adds whitespace for readability\n */\nrenderToString.render = renderToString;\n\n\n/** Only render elements, leaving Components inline as `<ComponentName ... />`.\n *\tThis method is just a convenience alias for `render(vnode, context, { shallow:true })`\n *\t@name shallow\n *\t@function\n *\t@param {VNode} vnode\tJSX VNode to render.\n *\t@param {Object} [context={}]\tOptionally pass an initial context object through the render path.\n */\nlet shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW);\n\n\n/** The default export is an alias of `render()`. */\nfunction renderToString(vnode, context, opts, inner, isSvgMode) {\n\tif (vnode==null || typeof vnode==='boolean') {\n\t\treturn '';\n\t}\n\n\tlet nodeName = vnode.nodeName,\n\t\tattributes = vnode.attributes,\n\t\tisComponent = false;\n\tcontext = context || {};\n\topts = opts || {};\n\n\tlet pretty = opts.pretty,\n\t\tindentChar = typeof pretty==='string' ? pretty : '\\t';\n\n\t// #text nodes\n\tif (typeof vnode!=='object' && !nodeName) {\n\t\treturn encodeEntities(vnode);\n\t}\n\n\t// components\n\tif (typeof nodeName==='function') {\n\t\tisComponent = true;\n\t\tif (opts.shallow && (inner || opts.renderRootComponent===false)) {\n\t\t\tnodeName = getComponentName(nodeName);\n\t\t}\n\t\telse {\n\t\t\tlet props = getNodeProps(vnode),\n\t\t\t\trendered;\n\n\t\t\tif (!nodeName.prototype || typeof nodeName.prototype.render!=='function') {\n\t\t\t\t// stateless functional components\n\t\t\t\trendered = nodeName(props, context);\n\t\t\t}\n\t\t\telse {\n\t\t\t\t// class-based components\n\t\t\t\tlet c = new nodeName(props, context);\n\t\t\t\t// turn off stateful re-rendering:\n\t\t\t\tc._disable = c.__x = true;\n\t\t\t\tc.props = props;\n\t\t\t\tc.context = context;\n\t\t\t\tif (c.componentWillMount) c.componentWillMount();\n\t\t\t\trendered = c.render(c.props, c.state, c.context);\n\n\t\t\t\tif (c.getChildContext) {\n\t\t\t\t\tcontext = assign(assign({}, context), c.getChildContext());\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn renderToString(rendered, context, opts, opts.shallowHighOrder!==false);\n\t\t}\n\t}\n\n\t// render JSX to HTML\n\tlet s = '', html;\n\n\tif (attributes) {\n\t\tlet attrs = objectKeys(attributes);\n\n\t\t// allow sorting lexicographically for more determinism (useful for tests, such as via preact-jsx-chai)\n\t\tif (opts && opts.sortAttributes===true) attrs.sort();\n\n\t\tfor (let i=0; i<attrs.length; i++) {\n\t\t\tlet name = attrs[i],\n\t\t\t\tv = attributes[name];\n\t\t\tif (name==='children') continue;\n\n\t\t\tif (name.match(/[\\s\\n\\\\/='\"\\0<>]/)) continue;\n\n\t\t\tif (!(opts && opts.allAttributes) && (name==='key' || name==='ref')) continue;\n\n\t\t\tif (name==='className') {\n\t\t\t\tif (attributes.class) continue;\n\t\t\t\tname = 'class';\n\t\t\t}\n\t\t\telse if (isSvgMode && name.match(/^xlink:?./)) {\n\t\t\t\tname = name.toLowerCase().replace(/^xlink:?/, 'xlink:');\n\t\t\t}\n\n\t\t\tif (name==='style' && v && typeof v==='object') {\n\t\t\t\tv = styleObjToCss(v);\n\t\t\t}\n\n\t\t\tlet hooked = opts.attributeHook && opts.attributeHook(name, v, context, opts, isComponent);\n\t\t\tif (hooked || hooked==='') {\n\t\t\t\ts += hooked;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (name==='dangerouslySetInnerHTML') {\n\t\t\t\thtml = v && v.__html;\n\t\t\t}\n\t\t\telse if ((v || v===0 || v==='') && typeof v!=='function') {\n\t\t\t\tif (v===true || v==='') {\n\t\t\t\t\tv = name;\n\t\t\t\t\t// in non-xml mode, allow boolean attributes\n\t\t\t\t\tif (!opts || !opts.xml) {\n\t\t\t\t\t\ts += ' ' + name;\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\ts += ` ${name}=\"${encodeEntities(v)}\"`;\n\t\t\t}\n\t\t}\n\t}\n\n\t// account for >1 multiline attribute\n\tlet sub = s.replace(/^\\n\\s*/, ' ');\n\tif (sub!==s && !~sub.indexOf('\\n')) s = sub;\n\telse if (pretty && ~s.indexOf('\\n')) s += '\\n';\n\n\ts = `<${nodeName}${s}>`;\n\tif (String(nodeName).match(/[\\s\\n\\\\/='\"\\0<>]/)) throw s;\n\n\tlet isVoid = String(nodeName).match(VOID_ELEMENTS);\n\tif (isVoid) s = s.replace(/>$/, ' />');\n\n\tlet pieces = [];\n\tif (html) {\n\t\t// if multiline, indent.\n\t\tif (pretty && isLargeString(html)) {\n\t\t\thtml = '\\n' + indentChar + indent(html, indentChar);\n\t\t}\n\t\ts += html;\n\t}\n\telse if (vnode.children) {\n\t\tlet hasLarge = ~s.indexOf('\\n');\n\t\tfor (let i=0; i<vnode.children.length; i++) {\n\t\t\tlet child = vnode.children[i];\n\t\t\tif (child!=null && child!==false) {\n\t\t\t\tlet childSvgMode = nodeName==='svg' ? true : nodeName==='foreignObject' ? false : isSvgMode,\n\t\t\t\t\tret = renderToString(child, context, opts, true, childSvgMode);\n\t\t\t\tif (!hasLarge && pretty && isLargeString(ret)) hasLarge = true;\n\t\t\t\tif (ret) pieces.push(ret);\n\t\t\t}\n\t\t}\n\t\tif (pretty && hasLarge) {\n\t\t\tfor (let i=pieces.length; i--; ) {\n\t\t\t\tpieces[i] = '\\n' + indentChar + indent(pieces[i], indentChar);\n\t\t\t}\n\t\t}\n\t}\n\n\tif (pieces.length) {\n\t\ts += pieces.join('');\n\t}\n\telse if (opts && opts.xml) {\n\t\treturn s.substring(0, s.length-1) + ' />';\n\t}\n\n\tif (!isVoid) {\n\t\tif (pretty && ~s.indexOf('\\n')) s += '\\n';\n\t\ts += `</${nodeName}>`;\n\t}\n\n\treturn s;\n}\n\nfunction getComponentName(component) {\n\treturn component.displayName || component!==Function && component.name || getFallbackComponentName(component);\n}\n\nfunction getFallbackComponentName(component) {\n\tlet str = Function.prototype.toString.call(component),\n\t\tname = (str.match(/^\\s*function\\s+([^( ]+)/) || '')[1];\n\tif (!name) {\n\t\t// search for an existing indexed name for the given component:\n\t\tlet index = -1;\n\t\tfor (let i=UNNAMED.length; i--; ) {\n\t\t\tif (UNNAMED[i]===component) {\n\t\t\t\tindex = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\t// not found, create a new indexed name:\n\t\tif (index<0) {\n\t\t\tindex = UNNAMED.push(component) - 1;\n\t\t}\n\t\tname = `UnnamedComponent${index}`;\n\t}\n\treturn name;\n}\nrenderToString.shallowRender = shallowRender;\n\nexport default renderToString;\n\nexport {\n\trenderToString as render,\n\trenderToString,\n\tshallowRender\n};\n"],"names":["const","IS_NON_DIMENSIONAL","objectKeys","Object","keys","obj","let","i","hasOwnProperty","push","encodeEntities","s","String","replace","indent","char","isLargeString","length","ignoreLines","indexOf","JS_TO_CSS","styleObjToCss","str","prop","val","toLowerCase","test","undefined","assign","props","SHALLOW","shallow","UNNAMED","VOID_ELEMENTS","renderToString","render","vnode","context","opts","inner","isSvgMode","nodeName","attributes","isComponent","component","pretty","indentChar","renderRootComponent","rendered","children","defaultProps","getNodeProps","prototype","c","_disable","__x","componentWillMount","state","getChildContext","shallowHighOrder","displayName","Function","name","toString","call","match","index","getFallbackComponentName","html","attrs","sortAttributes","sort","v","allAttributes","class","hooked","attributeHook","__html","xml","sub","isVoid","pieces","hasLarge","child","ret","join","substring","shallowRender"],"mappings":"mLACOA,IAAMC,EAAqB,yDAErBC,EAAaC,OAAOC,eAASC,GACzCC,IAAIF,KACJ,IAAKE,IAAIC,KAAKF,EAASA,EAAIG,eAAeD,IAAIH,EAAKK,KAAKF,GACxD,OAAOH,GAGGM,WAAiBC,UAAKC,OAAOD,GACtCE,QAAQ,KAAM,SACdA,QAAQ,KAAM,QACdA,QAAQ,KAAM,QACdA,QAAQ,KAAM,WAELC,WAAUH,EAAGI,UAASH,OAAOD,GAAGE,QAAQ,SAAU,MAAQE,GAAQ,QAElEC,WAAiBL,EAAGM,EAAQC,UAAiBN,OAAOD,GAAGM,QAAQA,GAAU,MAASC,IAA0C,IAA3BN,OAAOD,GAAGQ,QAAQ,QAAyC,IAA1BP,OAAOD,GAAGQ,QAAQ,MAEzJC,KAGC,SAASC,EAAcV,GAC7BL,IAAIgB,EAAM,GACV,IAAKhB,IAAIiB,KAAQZ,EAAG,CACnBL,IAAIkB,EAAMb,EAAEY,GACH,MAALC,IACCF,IAAKA,GAAO,KAEhBA,GAAOF,EAAUG,KAAUH,EAAUG,GAAQA,EAAKV,QAAQ,WAAW,OAAOY,eAC5EH,GAAO,KACPA,GAAOE,EACU,iBAANA,IAAkD,IAAhCvB,EAAmByB,KAAKH,KACpDD,GAAO,MAERA,GAAO,KAGT,OAAOA,QAAOK,EAUR,SAASC,EAAOvB,EAAKwB,GAC3B,IAA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