Skip to content

Commit 3e523e6

Browse files
Hanks10100yyx990803
authored andcommitted
[weex] convert kebab-case attribute name to camelCase in compiler (vuejs#4964)
* [weex] add test case for camelize props * [weex] add test case for append props * [weex] camelize component attribute name * [weex] more reliable camelize functions
1 parent 3dc9338 commit 3e523e6

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

src/platforms/weex/compiler/modules/append.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
function transformNode (el: ASTElement, options: CompilerOptions) {
3+
function preTransformNode (el: ASTElement, options: CompilerOptions) {
44
if (el.tag === 'cell' && !el.attrsList.some(item => item.name === 'append')) {
55
el.attrsMap.append = 'tree'
66
el.attrsList.push({ name: 'append', value: 'tree' })
@@ -16,6 +16,6 @@ function genData (el: ASTElement): string {
1616

1717
export default {
1818
staticKeys: ['appendAsTree'],
19-
transformNode,
19+
preTransformNode,
2020
genData
2121
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import klass from './class'
22
import style from './style'
3+
import props from './props'
34
import append from './append'
45

56
export default [
67
klass,
78
style,
9+
props,
810
append
911
]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* @flow */
2+
3+
import { cached, camelize } from 'shared/util'
4+
5+
const normalize = cached(camelize)
6+
7+
function normalizeKeyName (str: string) : string {
8+
if (str.match(/^v\-/)) {
9+
return str.replace(/(v-[a-z\-]+\:)([a-z\-]+)$/i, ($, directive, prop) => {
10+
return directive + normalize(prop)
11+
})
12+
}
13+
return normalize(str)
14+
}
15+
16+
function transformNode (el: ASTElement, options: CompilerOptions) {
17+
if (Array.isArray(el.attrsList)) {
18+
el.attrsList.forEach(attr => {
19+
if (attr.name && attr.name.match(/\-/)) {
20+
const realName = normalizeKeyName(attr.name)
21+
if (el.attrsMap) {
22+
el.attrsMap[realName] = el.attrsMap[attr.name]
23+
delete el.attrsMap[attr.name]
24+
}
25+
attr.name = realName
26+
}
27+
})
28+
}
29+
}
30+
export default {
31+
transformNode
32+
}

test/weex/compiler/append.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { compile } from '../../../packages/weex-template-compiler'
2+
import { strToRegExp } from '../helpers/index'
3+
4+
describe('append props', () => {
5+
it('append="tree"', () => {
6+
const { render, staticRenderFns, errors } = compile(`<list><cell></cell></list>`)
7+
expect(render).not.toBeUndefined()
8+
expect(staticRenderFns).not.toBeUndefined()
9+
expect(staticRenderFns.length).toEqual(1)
10+
expect(staticRenderFns).toMatch(strToRegExp(`appendAsTree:true`))
11+
expect(staticRenderFns).toMatch(strToRegExp(`attrs:{"append":"tree"}`))
12+
expect(errors).toEqual([])
13+
})
14+
})

test/weex/compiler/props.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { compile } from '../../../packages/weex-template-compiler'
2+
import { strToRegExp } from '../helpers/index'
3+
4+
describe('compile props', () => {
5+
it('custom props', () => {
6+
const { render, staticRenderFns, errors } = compile(`<div custom="whatever"></div>`)
7+
expect(render).not.toBeUndefined()
8+
expect(staticRenderFns).not.toBeUndefined()
9+
expect(staticRenderFns.length).toEqual(0)
10+
expect(render).toMatch(strToRegExp(`attrs:{"custom":"whatever"}`))
11+
expect(errors).toEqual([])
12+
})
13+
14+
it('camelize props', () => {
15+
const { render, staticRenderFns, errors } = compile(`<div kebab-case="whatever"></div>`)
16+
expect(render).not.toBeUndefined()
17+
expect(staticRenderFns).not.toBeUndefined()
18+
expect(staticRenderFns.length).toEqual(0)
19+
expect(render).toMatch(strToRegExp(`attrs:{"kebabCase":"whatever"}`))
20+
expect(errors).toEqual([])
21+
})
22+
})

0 commit comments

Comments
 (0)