Repository: vuejs/vue
Pull request: [weex] convert kebab-case attribute name to camelCase in compiler

Weex native renderer only accepts camelCased attribute name.

Fix https://github.com/weexteam/weex-vue-framework/issues/21 .

Changed files:
filename: src/platforms/weex/compiler/modules/append.js
patch: @@ -1,6 +1,6 @@
 /* @flow */
 
-function transformNode (el: ASTElement, options: CompilerOptions) {
+function preTransformNode (el: ASTElement, options: CompilerOptions) {
   if (el.tag === 'cell' && !el.attrsList.some(item => item.name === 'append')) {
     el.attrsMap.append = 'tree'
     el.attrsList.push({ name: 'append', value: 'tree' })
@@ -16,6 +16,6 @@ function genData (el: ASTElement): string {
 
 export default {
   staticKeys: ['appendAsTree'],
-  transformNode,
+  preTransformNode,
   genData
 }

filename: src/platforms/weex/compiler/modules/index.js
patch: @@ -1,9 +1,11 @@
 import klass from './class'
 import style from './style'
+import props from './props'
 import append from './append'
 
 export default [
   klass,
   style,
+  props,
   append
 ]

filename: src/platforms/weex/compiler/modules/props.js
patch: @@ -0,0 +1,32 @@
+/* @flow */
+
+import { cached, camelize } from 'shared/util'
+
+const normalize = cached(camelize)
+
+function normalizeKeyName (str: string) : string {
+  if (str.match(/^v\-/)) {
+    return str.replace(/(v-[a-z\-]+\:)([a-z\-]+)$/i, ($, directive, prop) => {
+      return directive + normalize(prop)
+    })
+  }
+  return normalize(str)
+}
+
+function transformNode (el: ASTElement, options: CompilerOptions) {
+  if (Array.isArray(el.attrsList)) {
+    el.attrsList.forEach(attr => {
+      if (attr.name && attr.name.match(/\-/)) {
+        const realName = normalizeKeyName(attr.name)
+        if (el.attrsMap) {
+          el.attrsMap[realName] = el.attrsMap[attr.name]
+          delete el.attrsMap[attr.name]
+        }
+        attr.name = realName
+      }
+    })
+  }
+}
+export default {
+  transformNode
+}

filename: test/weex/compiler/append.spec.js
patch: @@ -0,0 +1,14 @@
+import { compile } from '../../../packages/weex-template-compiler'
+import { strToRegExp } from '../helpers/index'
+
+describe('append props', () => {
+  it('append="tree"', () => {
+    const { render, staticRenderFns, errors } = compile(`<list><cell></cell></list>`)
+    expect(render).not.toBeUndefined()
+    expect(staticRenderFns).not.toBeUndefined()
+    expect(staticRenderFns.length).toEqual(1)
+    expect(staticRenderFns).toMatch(strToRegExp(`appendAsTree:true`))
+    expect(staticRenderFns).toMatch(strToRegExp(`attrs:{"append":"tree"}`))
+    expect(errors).toEqual([])
+  })
+})

filename: test/weex/compiler/props.spec.js
patch: @@ -0,0 +1,22 @@
+import { compile } from '../../../packages/weex-template-compiler'
+import { strToRegExp } from '../helpers/index'
+
+describe('compile props', () => {
+  it('custom props', () => {
+    const { render, staticRenderFns, errors } = compile(`<div custom="whatever"></div>`)
+    expect(render).not.toBeUndefined()
+    expect(staticRenderFns).not.toBeUndefined()
+    expect(staticRenderFns.length).toEqual(0)
+    expect(render).toMatch(strToRegExp(`attrs:{"custom":"whatever"}`))
+    expect(errors).toEqual([])
+  })
+
+  it('camelize props', () => {
+    const { render, staticRenderFns, errors } = compile(`<div kebab-case="whatever"></div>`)
+    expect(render).not.toBeUndefined()
+    expect(staticRenderFns).not.toBeUndefined()
+    expect(staticRenderFns.length).toEqual(0)
+    expect(render).toMatch(strToRegExp(`attrs:{"kebabCase":"whatever"}`))
+    expect(errors).toEqual([])
+  })
+})