Skip to content

Commit d07bcce

Browse files
committed
support camelCase props (close vuejs#1000)
1 parent 5e38384 commit d07bcce

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

src/compiler/compile-props.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,13 @@ module.exports = function compileProps (el, propOptions) {
2929
// interpreted as minus calculations by the parser
3030
// so we need to camelize the path here
3131
path = _.camelize(name.replace(dataAttrRE, ''))
32-
if (/[A-Z]/.test(name)) {
33-
_.warn(
34-
'You seem to be using camelCase for a component prop, ' +
35-
'but HTML doesn\'t differentiate between upper and ' +
36-
'lower case. You should use hyphen-delimited ' +
37-
'attribute names. For more info see ' +
38-
'http://vuejs.org/api/options.html#props'
39-
)
40-
}
4132
if (!identRE.test(path)) {
4233
_.warn(
4334
'Invalid prop key: "' + name + '". Prop keys ' +
4435
'must be valid identifiers.'
4536
)
4637
}
47-
value = el.getAttribute(name)
38+
value = el.getAttribute(_.hyphenate(name))
4839
// create a prop descriptor
4940
prop = {
5041
name: name,

src/util/lang.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,31 @@ exports.stripQuotes = function (str) {
7272
}
7373

7474
/**
75-
* Replace helper
75+
* Camelize a hyphen-delmited string.
7676
*
77-
* @param {String} _ - matched delimiter
78-
* @param {String} c - matched char
77+
* @param {String} str
7978
* @return {String}
8079
*/
80+
81+
exports.camelize = function (str) {
82+
return str.replace(/-(\w)/g, toUpper)
83+
}
84+
8185
function toUpper (_, c) {
8286
return c ? c.toUpperCase() : ''
8387
}
8488

8589
/**
86-
* Camelize a hyphen-delmited string.
90+
* Hyphenate a camelCase string.
8791
*
8892
* @param {String} str
8993
* @return {String}
9094
*/
9195

92-
var camelRE = /-(\w)/g
93-
exports.camelize = function (str) {
94-
return str.replace(camelRE, toUpper)
96+
exports.hyphenate = function (str) {
97+
return str
98+
.replace(/([a-z\d])([A-Z])/g, '$1-$2')
99+
.toLowerCase()
95100
}
96101

97102
/**

test/unit/specs/compiler/compile_spec.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ if (_.inBrowser) {
179179
el.setAttribute('onetime', '{{*a}}')
180180
el.setAttribute('twoway', '{{@a}}')
181181
el.setAttribute('with-filter', '{{a | filter}}')
182+
el.setAttribute('camel-case', 'hi')
182183
el.setAttribute('boolean-literal', '{{true}}')
183184
el.setAttribute('boolean', '')
184185
compiler.compileAndLinkProps(vm, el, props)
@@ -227,12 +228,10 @@ if (_.inBrowser) {
227228
expect(vm._data.onetime).toBe('from parent: a')
228229
expect(vm.booleanLiteral).toBe('from parent: true')
229230
expect(vm._data.booleanLiteral).toBe('from parent: true')
230-
expect(vm._data.camelCase).toBeUndefined()
231+
expect(vm._data.camelCase).toBe('hi')
231232
expect(vm._data.defaultValue).toBe(123)
232233
expect(vm._data.boolean).toBe(true)
233234
expect(vm._data.booleanAbsent).toBe(false)
234-
// camelCase should've warn
235-
expect(hasWarned(_, 'using camelCase')).toBe(true)
236235
})
237236

238237
it('props on root instance', function () {

test/unit/specs/util/lang_spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ describe('Util - Language Enhancement', function () {
2929
expect(_.camelize('some-long-name')).toBe('someLongName')
3030
})
3131

32+
it('hyphenate', function () {
33+
expect(_.hyphenate('whatsUp')).toBe('whats-up')
34+
expect(_.hyphenate('a1BfC')).toBe('a1-bf-c')
35+
expect(_.hyphenate('already-With-Hyphen')).toBe('already-with-hyphen')
36+
})
37+
3238
it('classify', function () {
3339
expect(_.classify('abc')).toBe('Abc')
3440
expect(_.classify('some-long-name')).toBe('SomeLongName')

0 commit comments

Comments
 (0)