Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear extra styles when hydrating/mounting element #10993

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/platforms/web/runtime/modules/style.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow */

import { getStyle, normalizeStyleBinding } from 'web/util/style'
import { getStyle, normalizeStyleBinding, parseStyleText } from 'web/util/style'
import { cached, camelize, extend, isDef, isUndef, hyphenate } from 'shared/util'

const cssVarRE = /^--/
Expand Down Expand Up @@ -44,7 +44,7 @@ const normalize = cached(function (prop) {
}
})

function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData, creating: boolean = false) {
const data = vnode.data
const oldData = oldVnode.data

Expand All @@ -60,7 +60,7 @@ function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
const oldStyleBinding: any = oldData.normalizedStyle || oldData.style || {}

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

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

Expand All @@ -73,6 +73,10 @@ function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {

const newStyle = getStyle(vnode, true)

if (creating && el.style.cssText) {
oldStyle = parseStyleText(el.style.cssText)
}

for (name in oldStyle) {
if (isUndef(newStyle[name])) {
setProp(el, name, '')
Expand All @@ -88,6 +92,6 @@ function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
}

export default {
create: updateStyle,
create: (oldVNode: VNodeWithData, vnode: VNodeWithData) => updateStyle(oldVNode, vnode, true),
update: updateStyle
}
13 changes: 13 additions & 0 deletions test/unit/modules/vdom/patch/hydration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,19 @@ describe('vdom patch: hydration', () => {
}).then(done)
})

it('should hydrate with correct inline styles', () => {
const dom = createMockSSRDOM('<div style="padding-left: 0px; padding-right: 3px"></div>')

const vm = new Vue({
data: {
style: { paddingLeft: '2px' }
},
template: `<div><div :style="style"></div></div>`
}).$mount(dom)

expect(vm.$el.innerHTML).toBe('<div style="padding-left: 2px;"></div>')
})

it('should properly initialize dynamic class bindings for future updates', done => {
const dom = createMockSSRDOM('<div class="foo bar"></div>')

Expand Down