Browse Source
* fix v-model with array binding * add mutli selects test case * add test case. v-bind with array * add comments * code refactordev
defcc
8 years ago
committed by
Evan You
6 changed files with 231 additions and 7 deletions
@ -0,0 +1,84 @@ |
|||
/* @flow */ |
|||
|
|||
let len, str, chr, index, expressionPos, expressionEndPos |
|||
|
|||
/** |
|||
* parse directive model to do the array update transform. a[idx] = val => $$a.splice($$idx, 1, val) |
|||
* |
|||
* for loop possible cases: |
|||
* |
|||
* - test |
|||
* - test[idx] |
|||
* - test[test1[idx]] |
|||
* - test["a"][idx] |
|||
* - xxx.test[a[a].test1[idx]] |
|||
* - test.xxx.a["asa"][test1[idx]] |
|||
* |
|||
*/ |
|||
|
|||
export default function parseModel (val: string): Object { |
|||
str = val |
|||
len = str.length |
|||
index = expressionPos = expressionEndPos = 0 |
|||
|
|||
if (val.indexOf('[') < 0) { |
|||
return { |
|||
exp: val, |
|||
idx: null |
|||
} |
|||
} |
|||
|
|||
while (!eof()) { |
|||
chr = next() |
|||
if (isStringStart(chr)) { |
|||
parseString(chr) |
|||
} else if (chr === 0x5B) { |
|||
parseBracket(chr) |
|||
} |
|||
} |
|||
|
|||
return { |
|||
exp: val.substring(0, expressionPos), |
|||
idx: val.substring(expressionPos + 1, expressionEndPos) |
|||
} |
|||
} |
|||
|
|||
function next (): number { |
|||
return str.charCodeAt(++index) |
|||
} |
|||
|
|||
function eof (): boolean { |
|||
return index >= len |
|||
} |
|||
|
|||
function isStringStart (chr: number): boolean { |
|||
return chr === 0x22 || chr === 0x27 |
|||
} |
|||
|
|||
function parseBracket (chr: number): void { |
|||
let inBracket = 1 |
|||
expressionPos = index |
|||
while (!eof()) { |
|||
chr = next() |
|||
if (isStringStart(chr)) { |
|||
parseString(chr) |
|||
continue |
|||
} |
|||
if (chr === 0x5B) inBracket++ |
|||
if (chr === 0x5D) inBracket-- |
|||
if (inBracket === 0) { |
|||
expressionEndPos = index |
|||
break |
|||
} |
|||
} |
|||
} |
|||
|
|||
function parseString (chr: number): void { |
|||
const stringQuote = chr |
|||
while (!eof()) { |
|||
chr = next() |
|||
if (chr === stringQuote) { |
|||
break |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue