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 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 * Get flattened children from the children prop\n * @param {Array} accumulator\n * @param {any} children A `props.children` opaque object.\n * @returns {Array} accumulator\n * @private\n */\nexport function getChildren(accumulator, children) {\n\tif (Array.isArray(children)) {\n\t\tchildren.reduce(getChildren, accumulator);\n\t}\n\telse if (children!=null && children!==false) {\n\t\taccumulator.push(children);\n\t}\n\treturn accumulator;\n}\n","import { encodeEntities, indent, isLargeString, styleObjToCss, assign, getChildren } from './util';\nimport { ENABLE_PRETTY } from '../env';\nimport { options, Fragment } from 'preact';\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.type,\n\t\tprops = vnode.props,\n\t\tisComponent = false;\n\tcontext = context || {};\n\topts = opts || {};\n\n\tlet pretty = ENABLE_PRETTY && opts.pretty,\n\t\tindentChar = pretty && 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 if (nodeName===Fragment) {\n\t\t\tlet rendered = '';\n\t\t\tlet children = [];\n\t\t\tgetChildren(children, vnode.props.children);\n\n\t\t\tfor (let i = 0; i < children.length; i++) {\n\t\t\t\trendered += renderToString(children[i], context, opts, opts.shallowHighOrder!==false, isSvgMode);\n\t\t\t}\n\t\t\treturn rendered;\n\t\t}\n\t\telse {\n\t\t\tlet rendered;\n\t\t\t\n\t\t\tlet c = vnode.__c = { __v: vnode, context, props: vnode.props };\n\t\t\tif (options.render) options.render(vnode);\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.call(vnode.__c, props, context);\n\t\t\t}\n\t\t\telse {\n\t\t\t\t// class-based components\n\t\t\t\t// c = new nodeName(props, context);\n\t\t\t\tc = vnode.__c = new nodeName(props, context);\n\t\t\t\tc.__v = vnode;\n\t\t\t\t// turn off stateful re-rendering:\n\t\t\t\tc._dirty = c.__d = true;\n\t\t\t\tc.props = props;\n\t\t\t\tc.context = context;\n\t\t\t\tif (nodeName.getDerivedStateFromProps) c.state = assign(assign({}, c.state), nodeName.getDerivedStateFromProps(c.props, c.state));\n\t\t\t\telse if (c.componentWillMount) c.componentWillMount();\n\t\t\t\trendered = c.render(c.props, c.state, c.context);\n\t\t\t}\n\t\t\t\n\t\t\tif (c.getChildContext) {\n\t\t\t\tcontext = assign(assign({}, context), c.getChildContext());\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 (props) {\n\t\tlet attrs = Object.keys(props);\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 = props[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 (props.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\tif (pretty) {\n\t\tlet sub = s.replace(/^\\n\\s*/, ' ');\n\t\tif (sub!==s && !~sub.indexOf('\\n')) s = sub;\n\t\telse if (pretty && ~s.indexOf('\\n')) s += '\\n';\n\t}\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\n\tlet children;\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 (props && getChildren(children = [], props.children).length) {\n\t\tlet hasLarge = pretty && ~s.indexOf('\\n');\n\t\tfor (let i=0; i<children.length; i++) {\n\t\t\tlet child = 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 (pretty && !hasLarge && 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","encodeEntities","s","String","replace","JS_TO_CSS","styleObjToCss","let","str","prop","val","toLowerCase","test","undefined","assign","obj","props","i","getChildren","accumulator","children","Array","isArray","reduce","push","SHALLOW","shallow","UNNAMED","VOID_ELEMENTS","renderToString","render","vnode","context","opts","inner","isSvgMode","component","nodeName","type","isComponent","renderRootComponent","Fragment","rendered","length","shallowHighOrder","c","__c","__v","options","prototype","_dirty","__d","getDerivedStateFromProps","state","componentWillMount","call","getChildContext","displayName","Function","name","toString","match","index","getFallbackComponentName","html","attrs","Object","keys","sortAttributes","sort","v","allAttributes","class","hooked","attributeHook","__html","xml","isVoid","pieces","child","ret","join","substring","shallowRender"],"mappings":"wNACOA,IAAMC,EAAqB,yDAEvBC,WAAiBC,UAAKC,OAAOD,GACtCE,QAAQ,KAAM,SACdA,QAAQ,KAAM,QACdA,QAAQ,KAAM,QACdA,QAAQ,KAAM,WAMVC,KAGC,SAASC,EAAcJ,GAC7BK,IAAIC,EAAM,GACV,IAAKD,IAAIE,KAAQP,EAAG,CACnBK,IAAIG,EAAMR,EAAEO,GACH,MAALC,IACCF,IAAKA,GAAO,KAEhBA,GAAOH,EAAUI,KAAUJ,EAAUI,GAAQA,EAAKL,QAAQ,WAAW,OAAOO,eAC5EH,GAAO,KACPA,GAAOE,EACU,iBAANA,IAAkD,IAAhCV,EAAmBY,KAAKH,KACpDD,GAAO,MAERA,GAAO,KAGT,OAAOA,QAAOK,EAUR,SAASC,EAAOC,EAAKC,GAC3B,IAAKT,IAAIU,KAAKD,EAAOD,EAAIE,GAAKD,EAAMC,GACpC,OAAOF,EAUD,SAASG,EAAYC,EAAaC,GAOxC,OANIC,MAAMC,QAAQF,GACjBA,EAASG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