Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix v-model with array binding * add mutli selects test case * add test case. v-bind with array * add comments * code refactor
- Loading branch information
Showing
6 changed files
with
231 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters