Skip to content

Commit a12d32a

Browse files
committed
fix style diffing on cached/slot elements (fix #5318)
1 parent 6622839 commit a12d32a

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

flow/vnode.js

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ declare interface VNodeData {
4040
class?: any;
4141
staticStyle?: { [key: string]: any };
4242
style?: Array<Object> | Object;
43+
normalizedStyle?: Object;
4344
props?: { [key: string]: any };
4445
attrs?: { [key: string]: string };
4546
domProps?: { [key: string]: any };

src/platforms/web/runtime/modules/style.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,20 @@ function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
4545

4646
let cur, name
4747
const el: any = vnode.elm
48-
const oldStaticStyle: any = oldVnode.data.staticStyle
49-
const oldStyleBinding: any = oldVnode.data.style || {}
48+
const oldStaticStyle: any = oldData.staticStyle
49+
const oldStyleBinding: any = oldData.normalizedStyle || oldData.style || {}
5050

5151
// if static style exists, stylebinding already merged into it when doing normalizeStyleData
5252
const oldStyle = oldStaticStyle || oldStyleBinding
5353

5454
const style = normalizeStyleBinding(vnode.data.style) || {}
5555

56-
vnode.data.style = style.__ob__ ? extend({}, style) : style
56+
// store normalized style under a different key for next diff
57+
// make sure to clone it if it's reactive, since the user likley wants
58+
// to mutate it.
59+
vnode.data.normalizedStyle = style.__ob__
60+
? extend({}, style)
61+
: style
5762

5863
const newStyle = getStyle(vnode, true)
5964

test/unit/features/directives/style.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,25 @@ describe('Directive v-bind:style', () => {
346346
expect(style.marginTop).toBe('12px')
347347
}).then(done)
348348
})
349+
350+
// #5318
351+
it('should work for elements passed down as a slot', done => {
352+
const vm = new Vue({
353+
template: `<test><div :style="style"/></test>`,
354+
data: {
355+
style: { color: 'red' }
356+
},
357+
components: {
358+
test: {
359+
template: `<div><slot/></div>`
360+
}
361+
}
362+
}).$mount()
363+
364+
expect(vm.$el.children[0].style.color).toBe('red')
365+
vm.style.color = 'green'
366+
waitForUpdate(() => {
367+
expect(vm.$el.children[0].style.color).toBe('green')
368+
}).then(done)
369+
})
349370
})

0 commit comments

Comments
 (0)