diff --git a/src/compiler/codegen/index.js b/src/compiler/codegen/index.js index e5985d9028..59b14ea791 100644 --- a/src/compiler/codegen/index.js +++ b/src/compiler/codegen/index.js @@ -483,15 +483,22 @@ function genComponent ( })` } -function genProps (props: Array<{ name: string, value: string }>): string { +function genProps (props: Array<{ name: string, value: any }>): string { let res = '' for (let i = 0; i < props.length; i++) { const prop = props[i] - res += `"${prop.name}":${transformSpecialNewlines(prop.value)},` + res += `"${prop.name}":${generateValue(prop.value)},` } return res.slice(0, -1) } +function generateValue (value) { + if (typeof value === 'string') { + return transformSpecialNewlines(value) + } + return JSON.stringify(value) +} + // #3895, #4268 function transformSpecialNewlines (text: string): string { return text diff --git a/src/compiler/helpers.js b/src/compiler/helpers.js index 1349fa364f..ebcd5708bb 100644 --- a/src/compiler/helpers.js +++ b/src/compiler/helpers.js @@ -20,7 +20,7 @@ export function addProp (el: ASTElement, name: string, value: string) { (el.props || (el.props = [])).push({ name, value }) } -export function addAttr (el: ASTElement, name: string, value: string) { +export function addAttr (el: ASTElement, name: string, value: any) { (el.attrs || (el.attrs = [])).push({ name, value }) } diff --git a/src/platforms/weex/compiler/modules/recycle-list/index.js b/src/platforms/weex/compiler/modules/recycle-list/index.js index 82cc6e8f60..0fe7bb138a 100644 --- a/src/platforms/weex/compiler/modules/recycle-list/index.js +++ b/src/platforms/weex/compiler/modules/recycle-list/index.js @@ -1,6 +1,7 @@ /* @flow */ import { transformText } from './text' +import { transformVBind } from './v-bind' let currentRecycleList = null @@ -22,6 +23,7 @@ function postTransformNode (el: ASTElement) { if (el.tag === 'text') { transformText(el) } + transformVBind(el) } if (el === currentRecycleList) { currentRecycleList = null diff --git a/src/platforms/weex/compiler/modules/recycle-list/text.js b/src/platforms/weex/compiler/modules/recycle-list/text.js index a950aff93a..fb72c81b55 100644 --- a/src/platforms/weex/compiler/modules/recycle-list/text.js +++ b/src/platforms/weex/compiler/modules/recycle-list/text.js @@ -16,7 +16,9 @@ function genText (node: ASTNode) { export function transformText (el: ASTElement) { // weex can only contain text, so the parser // always generates a single child. - addAttr(el, 'value', genText(el.children[0])) - el.children = [] - el.plain = false + if (el.children.length) { + addAttr(el, 'value', genText(el.children[0])) + el.children = [] + el.plain = false + } } diff --git a/src/platforms/weex/compiler/modules/recycle-list/v-bind.js b/src/platforms/weex/compiler/modules/recycle-list/v-bind.js new file mode 100644 index 0000000000..62bb0ac6a2 --- /dev/null +++ b/src/platforms/weex/compiler/modules/recycle-list/v-bind.js @@ -0,0 +1,31 @@ +/* @flow */ + +import { getAndRemoveAttr, addAttr } from 'compiler/helpers' + +function isBindingAttr (name) { + return /^(v\-bind)?\:/.test(name) +} + +function parseRealName (name: string): string { + return name.replace(/^(v\-bind)?\:/, '') +} + +export function transformVBind (el: ASTElement) { + if (!el.attrsList.length) { + return + } + el.attrsList.forEach(attr => { + // console.log('is binding attr:', attr.name, isBindingAttr(attr.name)) + if (isBindingAttr(attr.name)) { + const realName: string = parseRealName(attr.name) + const binding = getAndRemoveAttr(el, attr.name) + if (el.attrs) { + el.attrs = el.attrs.filter(at => at.name !== realName) // omit duplicated + } + getAndRemoveAttr(el, realName) + addAttr(el, realName, { '@binding': binding }) + } + }) + el.hasBindings = false + // el.plain = true +}