Skip to content

Commit

Permalink
fix style diffing on cached/slot elements (fix vuejs#5318)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and awamwang committed Jun 15, 2017
1 parent 2a4ef8f commit 018595f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions flow/vnode.js
Expand Up @@ -40,6 +40,7 @@ declare interface VNodeData {
class?: any;
staticStyle?: { [key: string]: any };
style?: Array<Object> | Object;
normalizedStyle?: Object;
props?: { [key: string]: any };
attrs?: { [key: string]: string };
domProps?: { [key: string]: any };
Expand Down
11 changes: 8 additions & 3 deletions src/platforms/web/runtime/modules/style.js
Expand Up @@ -45,15 +45,20 @@ function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {

let cur, name
const el: any = vnode.elm
const oldStaticStyle: any = oldVnode.data.staticStyle
const oldStyleBinding: any = oldVnode.data.style || {}
const oldStaticStyle: any = oldData.staticStyle
const oldStyleBinding: any = oldData.normalizedStyle || oldData.style || {}

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

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

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

const newStyle = getStyle(vnode, true)

Expand Down
21 changes: 21 additions & 0 deletions test/unit/features/directives/style.spec.js
Expand Up @@ -346,4 +346,25 @@ describe('Directive v-bind:style', () => {
expect(style.marginTop).toBe('12px')
}).then(done)
})

// #5318
it('should work for elements passed down as a slot', done => {
const vm = new Vue({
template: `<test><div :style="style"/></test>`,
data: {
style: { color: 'red' }
},
components: {
test: {
template: `<div><slot/></div>`
}
}
}).$mount()

expect(vm.$el.children[0].style.color).toBe('red')
vm.style.color = 'green'
waitForUpdate(() => {
expect(vm.$el.children[0].style.color).toBe('green')
}).then(done)
})
})

0 comments on commit 018595f

Please sign in to comment.