|
| 1 | +import { parseText } from './text-parser' |
| 2 | + |
| 3 | +const bindRE = /^:|^v-bind:/ |
| 4 | +const onRE = /^@|^v-on:/ |
| 5 | +const mustUsePropsRE = /^(value|selected|checked|muted)$/ |
| 6 | + |
| 7 | +export function generate (ast) { |
| 8 | + const code = genElement(ast) |
| 9 | + return new Function (`with (this) { return ${code}}`) |
| 10 | +} |
| 11 | + |
| 12 | +function genElement (el, key) { |
| 13 | + let exp |
| 14 | + if (exp = getAttr(el, 'v-for')) { |
| 15 | + return genFor(el, exp) |
| 16 | + } else if (exp = getAttr(el, 'v-if')) { |
| 17 | + return genIf(el, exp) |
| 18 | + } else if (el.tag === 'template') { |
| 19 | + return genChildren(el) |
| 20 | + } else { |
| 21 | + return `__h__('${ el.tag }', ${ genData(el, key) }, ${ genChildren(el) })` |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function genIf (el, exp) { |
| 26 | + return `(${ exp }) ? ${ genElement(el) } : ''` |
| 27 | +} |
| 28 | + |
| 29 | +function genFor (el, exp) { |
| 30 | + const inMatch = exp.match(/([a-zA-Z_][\w]*)\s+(?:in|of)\s+(.*)/) |
| 31 | + if (!inMatch) { |
| 32 | + throw new Error('Invalid v-for expression: '+ exp) |
| 33 | + } |
| 34 | + const alias = inMatch[1].trim() |
| 35 | + exp = inMatch[2].trim() |
| 36 | + const key = el.attrsMap['track-by'] || 'undefined' |
| 37 | + return `(${ exp }).map(function (${ alias }, $index) {return ${ genElement(el, key) }})` |
| 38 | +} |
| 39 | + |
| 40 | +function genData (el, key) { |
| 41 | + if (!el.attrs.length) { |
| 42 | + return '{}' |
| 43 | + } |
| 44 | + let data = key ? `{key:${ key },` : `{` |
| 45 | + if (el.attrsMap[':class'] || el.attrsMap['class']) { |
| 46 | + data += `class: _renderClass(${ el.attrsMap[':class'] }, "${ el.attrsMap['class'] || '' }"),` |
| 47 | + } |
| 48 | + let attrs = `attrs:{` |
| 49 | + let props = `props:{` |
| 50 | + let hasAttrs = false |
| 51 | + let hasProps = false |
| 52 | + for (let i = 0, l = el.attrs.length; i < l; i++) { |
| 53 | + let attr = el.attrs[i] |
| 54 | + let name = attr.name |
| 55 | + if (bindRE.test(name)) { |
| 56 | + name = name.replace(bindRE, '') |
| 57 | + if (name === 'class') { |
| 58 | + continue |
| 59 | + } else if (name === 'style') { |
| 60 | + data += `style: ${ attr.value },` |
| 61 | + } else if (mustUsePropsRE.test(name)) { |
| 62 | + hasProps = true |
| 63 | + props += `"${ name }": (${ attr.value }),` |
| 64 | + } else { |
| 65 | + hasAttrs = true |
| 66 | + attrs += `"${ name }": (${ attr.value }),` |
| 67 | + } |
| 68 | + } else if (onRE.test(name)) { |
| 69 | + name = name.replace(onRE, '') |
| 70 | + // TODO |
| 71 | + } else if (name !== 'class') { |
| 72 | + hasAttrs = true |
| 73 | + attrs += `"${ name }": (${ JSON.stringify(attr.value) }),` |
| 74 | + } |
| 75 | + } |
| 76 | + if (hasAttrs) { |
| 77 | + data += attrs.slice(0, -1) + '},' |
| 78 | + } |
| 79 | + if (hasProps) { |
| 80 | + data += props.slice(0, -1) + '},' |
| 81 | + } |
| 82 | + return data.replace(/,$/, '') + '}' |
| 83 | +} |
| 84 | + |
| 85 | +function genChildren (el) { |
| 86 | + if (!el.children.length) { |
| 87 | + return 'undefined' |
| 88 | + } |
| 89 | + return '__flatten__([' + el.children.map(genNode).join(',') + '])' |
| 90 | +} |
| 91 | + |
| 92 | +function genNode (node) { |
| 93 | + if (node.tag) { |
| 94 | + return genElement(node) |
| 95 | + } else { |
| 96 | + return genText(node) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function genText (text) { |
| 101 | + if (text === ' ') { |
| 102 | + return '" "' |
| 103 | + } else { |
| 104 | + const exp = parseText(text) |
| 105 | + if (exp) { |
| 106 | + return 'String(' + escapeNewlines(exp) + ')' |
| 107 | + } else { |
| 108 | + return escapeNewlines(JSON.stringify(text)) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +function escapeNewlines (str) { |
| 114 | + return str.replace(/\n/g, '\\n') |
| 115 | +} |
| 116 | + |
| 117 | +function getAttr (el, attr) { |
| 118 | + let val |
| 119 | + if (val = el.attrsMap[attr]) { |
| 120 | + el.attrsMap[attr] = null |
| 121 | + for (let i = 0, l = el.attrs.length; i < l; i++) { |
| 122 | + if (el.attrs[i].name === attr) { |
| 123 | + el.attrs.splice(i, 1) |
| 124 | + break |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return val |
| 129 | +} |
0 commit comments