|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +/** |
| 4 | + * Cross-platform code generation for component v-model |
| 5 | + */ |
| 6 | +export function genComponentModel ( |
| 7 | + el: ASTElement, |
| 8 | + value: string, |
| 9 | + modifiers: ?ASTModifiers |
| 10 | +): ?boolean { |
| 11 | + const { number, trim } = modifiers || {} |
| 12 | + |
| 13 | + let valueExpression = 'value' |
| 14 | + if (trim) { |
| 15 | + valueExpression = `(typeof value === 'string' ? value.trim() : value)` |
| 16 | + } |
| 17 | + if (number) { |
| 18 | + valueExpression = `_n(${valueExpression})` |
| 19 | + } |
| 20 | + |
| 21 | + el.model = { |
| 22 | + value: `(${value})`, |
| 23 | + callback: `function (value) {${genAssignmentCode(value, valueExpression)}}` |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Cross-platform codegen helper for generating v-model value assignment code. |
| 29 | + */ |
| 30 | +export function genAssignmentCode ( |
| 31 | + value: string, |
| 32 | + assignment: string |
| 33 | +): string { |
| 34 | + const modelRs = parseModel(value) |
| 35 | + if (modelRs.idx === null) { |
| 36 | + return `${value}=${assignment}` |
| 37 | + } else { |
| 38 | + return `var $$exp = ${modelRs.exp}, $$idx = ${modelRs.idx};` + |
| 39 | + `if (!Array.isArray($$exp)){` + |
| 40 | + `${value}=${assignment}}` + |
| 41 | + `else{$$exp.splice($$idx, 1, ${assignment})}` |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * parse directive model to do the array update transform. a[idx] = val => $$a.splice($$idx, 1, val) |
| 47 | + * |
| 48 | + * for loop possible cases: |
| 49 | + * |
| 50 | + * - test |
| 51 | + * - test[idx] |
| 52 | + * - test[test1[idx]] |
| 53 | + * - test["a"][idx] |
| 54 | + * - xxx.test[a[a].test1[idx]] |
| 55 | + * - test.xxx.a["asa"][test1[idx]] |
| 56 | + * |
| 57 | + */ |
| 58 | + |
| 59 | +let len, str, chr, index, expressionPos, expressionEndPos |
| 60 | + |
| 61 | +export function parseModel (val: string): Object { |
| 62 | + str = val |
| 63 | + len = str.length |
| 64 | + index = expressionPos = expressionEndPos = 0 |
| 65 | + |
| 66 | + if (val.indexOf('[') < 0 || val.lastIndexOf(']') < len - 1) { |
| 67 | + return { |
| 68 | + exp: val, |
| 69 | + idx: null |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + while (!eof()) { |
| 74 | + chr = next() |
| 75 | + /* istanbul ignore if */ |
| 76 | + if (isStringStart(chr)) { |
| 77 | + parseString(chr) |
| 78 | + } else if (chr === 0x5B) { |
| 79 | + parseBracket(chr) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return { |
| 84 | + exp: val.substring(0, expressionPos), |
| 85 | + idx: val.substring(expressionPos + 1, expressionEndPos) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function next (): number { |
| 90 | + return str.charCodeAt(++index) |
| 91 | +} |
| 92 | + |
| 93 | +function eof (): boolean { |
| 94 | + return index >= len |
| 95 | +} |
| 96 | + |
| 97 | +function isStringStart (chr: number): boolean { |
| 98 | + return chr === 0x22 || chr === 0x27 |
| 99 | +} |
| 100 | + |
| 101 | +function parseBracket (chr: number): void { |
| 102 | + let inBracket = 1 |
| 103 | + expressionPos = index |
| 104 | + while (!eof()) { |
| 105 | + chr = next() |
| 106 | + if (isStringStart(chr)) { |
| 107 | + parseString(chr) |
| 108 | + continue |
| 109 | + } |
| 110 | + if (chr === 0x5B) inBracket++ |
| 111 | + if (chr === 0x5D) inBracket-- |
| 112 | + if (inBracket === 0) { |
| 113 | + expressionEndPos = index |
| 114 | + break |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function parseString (chr: number): void { |
| 120 | + const stringQuote = chr |
| 121 | + while (!eof()) { |
| 122 | + chr = next() |
| 123 | + if (chr === stringQuote) { |
| 124 | + break |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments