Skip to content

Commit 5f0cbac

Browse files
committed
Merge pull request #608 from skovhus/v-style-for-object
Added v-style support for objects
2 parents 33dbb5e + 923aee6 commit 5f0cbac

File tree

2 files changed

+70
-25
lines changed

2 files changed

+70
-25
lines changed

src/directives/style.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,55 @@ var importantRE = /!important;?$/
33

44
module.exports = {
55

6+
deep: true,
7+
68
bind: function () {
79
var prop = this.arg
810
if (!prop) return
9-
if (prop.charAt(0) === '$') {
10-
// properties that start with $ will be auto-prefixed
11-
prop = prop.slice(1)
12-
this.prefixed = true
13-
}
1411
this.prop = prop
1512
},
1613

1714
update: function (value) {
18-
var prop = this.prop
15+
if (this.prop) {
16+
this.setCssProperty(this.prop, value)
17+
} else {
18+
if (typeof value === 'object') {
19+
for (var prop in value) {
20+
this.setCssProperty(prop, value[prop])
21+
}
22+
} else {
23+
this.el.style.cssText = value
24+
}
25+
}
26+
},
27+
28+
setCssProperty: function (prop, value) {
29+
var prefixed = false
30+
if (prop.charAt(0) === '$') {
31+
// properties that start with $ will be auto-prefixed
32+
prop = prop.slice(1)
33+
prefixed = true
34+
}
1935
// cast possible numbers/booleans into strings
2036
if (value != null) {
2137
value += ''
2238
}
23-
if (prop) {
24-
var isImportant = importantRE.test(value)
25-
? 'important'
26-
: ''
27-
if (isImportant) {
28-
value = value.replace(importantRE, '').trim()
29-
}
30-
this.el.style.setProperty(prop, value, isImportant)
31-
if (this.prefixed) {
32-
var i = prefixes.length
33-
while (i--) {
34-
this.el.style.setProperty(
35-
prefixes[i] + prop,
36-
value,
37-
isImportant
38-
)
39-
}
39+
var isImportant = importantRE.test(value)
40+
? 'important'
41+
: ''
42+
if (isImportant) {
43+
value = value.replace(importantRE, '').trim()
44+
}
45+
this.el.style.setProperty(prop, value, isImportant)
46+
if (prefixed) {
47+
var i = prefixes.length
48+
while (i--) {
49+
this.el.style.setProperty(
50+
prefixes[i] + prop,
51+
value,
52+
isImportant
53+
)
4054
}
41-
} else {
42-
this.el.style.cssText = value
4355
}
4456
}
4557

test/unit/specs/directives/style_spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var _ = require('../../../../src/util')
22
var def = require('../../../../src/directives/style')
3+
var Vue = require('../../../../src/vue')
34

45
if (_.inBrowser) {
56
describe('v-style', function () {
@@ -43,5 +44,37 @@ if (_.inBrowser) {
4344
expect(spy).toHaveBeenCalledWith('-webkit-transform', val, '')
4445
})
4546

47+
it('update with object', function () {
48+
dir.bind()
49+
dir.update({color: 'red', 'margin-right': '30px'})
50+
expect(el.style.getPropertyValue('color')).toBe('red')
51+
expect(el.style.getPropertyValue('margin-right')).toBe('30px')
52+
})
53+
54+
it('update with object and auto prefix', function () {
55+
var spy = el.style.setProperty = jasmine.createSpy()
56+
dir.bind()
57+
var scale = 'scale(0.5)';
58+
dir.update({'$transform': scale})
59+
expect(spy).toHaveBeenCalledWith('transform', scale, '')
60+
expect(spy).toHaveBeenCalledWith('-ms-transform', scale, '')
61+
expect(spy).toHaveBeenCalledWith('-moz-transform', scale, '')
62+
expect(spy).toHaveBeenCalledWith('-webkit-transform', scale, '')
63+
})
64+
65+
it('updates object deep', function (done) {
66+
el.setAttribute('v-style', 'divStyling')
67+
var vm = new Vue({
68+
el: el,
69+
data: {divStyling: { display: 'none'}}
70+
})
71+
expect(el.style.display).toBe('none')
72+
vm.divStyling.display = 'block'
73+
_.nextTick(function () {
74+
expect(el.style.display).toBe('block')
75+
done()
76+
})
77+
})
78+
4679
})
4780
}

0 commit comments

Comments
 (0)