Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support v-model in weex #4178

Merged
merged 6 commits into from
Nov 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/compiler/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,87 @@ export function getAndRemoveAttr (el: ASTElement, name: string): ?string {
}
return val
}

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 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()
/* istanbul ignore if */
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
}
}
}
3 changes: 1 addition & 2 deletions src/platforms/web/compiler/directives/model.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* @flow */

import { isIE } from 'core/util/env'
import { addHandler, addProp, getBindingAttr } from 'compiler/helpers'
import parseModel from 'web/util/model'
import { addHandler, addProp, getBindingAttr, parseModel } from 'compiler/helpers'

let warn

Expand Down
85 changes: 0 additions & 85 deletions src/platforms/web/util/model.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/platforms/weex/compiler/directives/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import model from './model'

export default {
model
}
39 changes: 39 additions & 0 deletions src/platforms/weex/compiler/directives/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* @flow */

import { addHandler, addAttr, parseModel } from 'compiler/helpers'

export default function model (
el: ASTElement,
dir: ASTDirective,
_warn: Function
): ?boolean {
genDefaultModel(el, dir.value, dir.modifiers)
}

function genDefaultModel (
el: ASTElement,
value: string,
modifiers: ?ASTModifiers
): ?boolean {
const { lazy, trim } = modifiers || {}
const event = lazy ? 'change' : 'input'
const isNative = el.tag === 'input' || el.tag === 'textarea'
const valueExpression = isNative
? `$event.target.attr.value${trim ? '.trim()' : ''}`
: `$event`
const code = genAssignmentCode(value, valueExpression)
addAttr(el, 'value', `(${value})`)
addHandler(el, event, code, null, true)
}

function genAssignmentCode (value: string, assignment: string): string {
const modelRs = parseModel(value)
if (modelRs.idx === null) {
return `${value}=${assignment}`
} else {
return `var $$exp = ${modelRs.exp}, $$idx = ${modelRs.idx};` +
`if (!Array.isArray($$exp)){` +
`${value}=${assignment}}` +
`else{$$exp.splice($$idx, 1, ${assignment})}`
}
}
2 changes: 1 addition & 1 deletion test/unit/features/directives/model-parse.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import parseModel from 'web/util/model'
import { parseModel } from 'compiler/helpers'

describe('model expression parser', () => {
it('parse string in brackets', () => {
Expand Down
17 changes: 9 additions & 8 deletions test/weex/compiler/class.spec.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
import { compile } from '../../../packages/weex-template-compiler'
import { strToRegExp } from '../helpers/index'

describe('compile class', () => {
it('should be compiled', () => {
const { render, staticRenderFns, errors } = compile(`<div class="a b c"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).not.toBeUndefined()
expect(staticRenderFns.length).toEqual(1)
expect(staticRenderFns).toMatch(/staticClass\:\["a","b","c"\]/)
expect(staticRenderFns).toMatch(strToRegExp(`staticClass:["a","b","c"]`))
expect(errors).toEqual([])
})

it('should compile dynamic class', () => {
const { render, staticRenderFns, errors } = compile(`<div class="a {{b}} c"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).toEqual([])
expect(render).toMatch(/class\:\["a",_s\(b\),"c"\]/)
expect(render).toMatch(strToRegExp(`class:["a",_s(b),"c"]`))
expect(errors).not.toBeUndefined()
expect(errors.length).toEqual(1)
expect(errors[0]).toMatch(/a \{\{b\}\} c/)
expect(errors[0]).toMatch(/v\-bind/)
expect(errors[0]).toMatch(strToRegExp(`a {{b}} c`))
expect(errors[0]).toMatch(strToRegExp(`v-bind`))
})

it('should compile class binding of array', () => {
const { render, staticRenderFns, errors } = compile(`<div v-bind:class="['a', 'b', c]"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).toEqual([])
expect(render).toMatch(/class\:\['a', 'b', c\]/)
expect(render).toMatch(strToRegExp(`class:['a', 'b', c]`))
expect(errors).toEqual([])
})

it('should compile class binding of map', () => {
const { render, staticRenderFns, errors } = compile(`<div v-bind:class="{ a: true, b: x }"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).toEqual([])
expect(render).toMatch(/class\:\{ a\: true, b\: x \}/)
expect(render).toMatch(strToRegExp(`class:{ a: true, b: x }`))
expect(errors).toEqual([])
})

it('should compile class binding of a variable', () => {
const { render, staticRenderFns, errors } = compile(`<div v-bind:class="x"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).toEqual([])
expect(render).toMatch(/class\:x/)
expect(render).toMatch(strToRegExp(`class:x`))
expect(errors).toEqual([])
})

it('should compile class binding by shorthand', () => {
const { render, staticRenderFns, errors } = compile(`<div :class="['a', 'b', c]"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).toEqual([])
expect(render).toMatch(/class\:\['a', 'b', c\]/)
expect(render).toMatch(strToRegExp(`class:['a', 'b', c]`))
expect(errors).toEqual([])
})
})
21 changes: 11 additions & 10 deletions test/weex/compiler/style.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { compile } from '../../../packages/weex-template-compiler'
import { strToRegExp } from '../helpers/index'

describe('compile style', () => {
it('should be compiled', () => {
const { render, staticRenderFns, errors } = compile(`<div style="a: x; b: y"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).not.toBeUndefined()
expect(staticRenderFns.length).toEqual(1)
expect(staticRenderFns).toMatch(/staticStyle\:\{a:"x",b:"y"\}/)
expect(staticRenderFns).toMatch(strToRegExp(`staticStyle:{a:"x",b:"y"}`))
expect(errors).toEqual([])
})

Expand All @@ -24,7 +25,7 @@ describe('compile style', () => {
expect(render).not.toBeUndefined()
expect(staticRenderFns).not.toBeUndefined()
expect(staticRenderFns.length).toEqual(1)
expect(staticRenderFns).toMatch(/staticStyle\:\{a:"x",b:"y"\}/)
expect(staticRenderFns).toMatch(strToRegExp(`staticStyle:{a:"x",b:"y"}`))
expect(errors).toEqual([])
})

Expand All @@ -33,50 +34,50 @@ describe('compile style', () => {
expect(render).not.toBeUndefined()
expect(staticRenderFns).not.toBeUndefined()
expect(staticRenderFns.length).toEqual(1)
expect(staticRenderFns).toMatch(/staticStyle\:\{AbcDef\:"x-y",abcDef\:"x-y"\}/)
expect(staticRenderFns).toMatch(strToRegExp(`staticStyle:{AbcDef:"x-y",abcDef:"x-y"}`))
expect(errors).toEqual([])
})

it('should compile dynamic style', () => {
const { render, staticRenderFns, errors } = compile(`<div style="a: x; b: {{y}}"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).toEqual([])
expect(render).toMatch(/style\:\{a:"x",b:_s\(y\)\}/)
expect(render).toMatch(strToRegExp(`style:{a:"x",b:_s(y)}`))
expect(errors).not.toBeUndefined()
expect(errors.length).toEqual(1)
expect(errors[0]).toMatch(/b\: \{\{y\}\}/)
expect(errors[0]).toMatch(/v\-bind/)
expect(errors[0]).toMatch(strToRegExp(`b: {{y}}`))
expect(errors[0]).toMatch(strToRegExp(`v-bind`))
})

it('should compile style binding of array', () => {
const { render, staticRenderFns, errors } = compile(`<div v-bind:style="[a, b, c]"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).toEqual([])
expect(render).toMatch(/style\:\[a, b, c\]/)
expect(render).toMatch(strToRegExp(`style:[a, b, c]`))
expect(errors).toEqual([])
})

it('should compile style binding of map', () => {
const { render, staticRenderFns, errors } = compile(`<div v-bind:style="{ a: x, b: 'y' + z }"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).toEqual([])
expect(render).toMatch(/style\:\{ a\: x, b\: 'y' \+ z \}/)
expect(render).toMatch(strToRegExp(`style:{ a: x, b: 'y' + z }`))
expect(errors).toEqual([])
})

it('should compile style binding of a variable', () => {
const { render, staticRenderFns, errors } = compile(`<div v-bind:style="x"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).toEqual([])
expect(render).toMatch(/style\:x/)
expect(render).toMatch(strToRegExp(`style:x`))
expect(errors).toEqual([])
})

it('should compile style binding by shorthand', () => {
const { render, staticRenderFns, errors } = compile(`<div :style="[a, b, c]"></div>`)
expect(render).not.toBeUndefined()
expect(staticRenderFns).toEqual([])
expect(render).toMatch(/style\:\[a, b, c\]/)
expect(render).toMatch(strToRegExp(`style:[a, b, c]`))
expect(errors).toEqual([])
})
})