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

[weex] convert kebab-case attribute name to camelCase in compiler #4964

Merged
merged 6 commits into from
Feb 19, 2017
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
4 changes: 2 additions & 2 deletions src/platforms/weex/compiler/modules/append.js
Original file line number Diff line number Diff line change
@@ -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' })
Expand All @@ -16,6 +16,6 @@ function genData (el: ASTElement): string {

export default {
staticKeys: ['appendAsTree'],
transformNode,
preTransformNode,
genData
}
2 changes: 2 additions & 0 deletions src/platforms/weex/compiler/modules/index.js
Original file line number Diff line number Diff line change
@@ -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
]
32 changes: 32 additions & 0 deletions src/platforms/weex/compiler/modules/props.js
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions test/weex/compiler/append.spec.js
Original file line number Diff line number Diff line change
@@ -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([])
})
})
22 changes: 22 additions & 0 deletions test/weex/compiler/props.spec.js
Original file line number Diff line number Diff line change
@@ -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([])
})
})