Skip to content

Commit b4b55f1

Browse files
committed
support prefixing props with "data-" (close vuejs#1034)
1 parent 17e4b33 commit b4b55f1

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/compiler/compile-props.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var literalValueRE = /^(true|false)$|^\d.*/
2121
module.exports = function compileProps (el, propOptions) {
2222
var props = []
2323
var i = propOptions.length
24-
var options, name, value, path, prop, literal, single
24+
var options, name, attr, value, path, prop, literal, single
2525
while (i--) {
2626
options = propOptions[i]
2727
name = options.name
@@ -36,7 +36,12 @@ module.exports = function compileProps (el, propOptions) {
3636
)
3737
continue
3838
}
39-
value = el.getAttribute(_.hyphenate(name))
39+
attr = _.hyphenate(name)
40+
value = el.getAttribute(attr)
41+
if (value === null) {
42+
attr = 'data-' + attr
43+
value = el.getAttribute(attr)
44+
}
4045
// create a prop descriptor
4146
prop = {
4247
name: name,
@@ -48,7 +53,7 @@ module.exports = function compileProps (el, propOptions) {
4853
if (value !== null) {
4954
// important so that this doesn't get compiled
5055
// again as a normal attribute binding
51-
el.removeAttribute(name)
56+
el.removeAttribute(attr)
5257
var tokens = textParser.parse(value)
5358
if (tokens) {
5459
if (el && el.nodeType === 1) {

test/unit/specs/compiler/compile_spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ if (_.inBrowser) {
176176
a: 123
177177
}
178178
}
179-
}
179+
},
180+
'withDataPrefix'
180181
].map(function (p) {
181182
return typeof p === 'string' ? { name: p } : p
182183
})
@@ -191,6 +192,7 @@ if (_.inBrowser) {
191192
el.setAttribute('camel-case', 'hi')
192193
el.setAttribute('boolean-literal', '{{true}}')
193194
el.setAttribute('boolean', '')
195+
el.setAttribute('data-with-data-prefix', '1')
194196
compiler.compileAndLinkProps(vm, el, props)
195197
// should skip literals and one-time bindings
196198
expect(vm._bindDir.calls.count()).toBe(4)
@@ -228,7 +230,7 @@ if (_.inBrowser) {
228230
expect(args[3]).toBe(def)
229231
// literal and one time should've been set on the _data
230232
// and numbers should be casted
231-
expect(Object.keys(vm._data).length).toBe(9)
233+
expect(Object.keys(vm._data).length).toBe(10)
232234
expect(vm.a).toBe(1)
233235
expect(vm._data.a).toBe(1)
234236
expect(vm.someOtherAttr).toBe(2)
@@ -247,6 +249,8 @@ if (_.inBrowser) {
247249
expect(vm._data.booleanAbsent).toBe(false)
248250
expect(vm.factory).toBe(vm._data.factory)
249251
expect(vm.factory.a).toBe(123)
252+
expect(vm.withDataPrefix).toBe(1)
253+
expect(vm._data.withDataPrefix).toBe(1)
250254
})
251255

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

0 commit comments

Comments
 (0)