Skip to content

Commit e2d3235

Browse files
authored
fix(runtime-dom): v-bind style should clear previous css string value (#10373)
close #10352
1 parent 76c9c74 commit e2d3235

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

packages/runtime-dom/__tests__/patchStyle.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,13 @@ describe(`runtime-dom: style patching`, () => {
158158
)
159159
expect(el.style.display).toBe('flex')
160160
})
161+
162+
it('should clear previous css string value', () => {
163+
const el = document.createElement('div')
164+
patchProp(el, 'style', {}, 'color:red')
165+
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
166+
167+
patchProp(el, 'style', 'color:red', { fontSize: '12px' })
168+
expect(el.style.cssText.replace(/\s/g, '')).toBe('font-size:12px;')
169+
})
161170
})

packages/runtime-dom/src/modules/style.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
1313
const currentDisplay = style.display
1414
let hasControlledDisplay = false
1515
if (next && !isCssString) {
16-
if (prev && !isString(prev)) {
17-
for (const key in prev) {
18-
if (next[key] == null) {
19-
setStyle(style, key, '')
16+
if (prev) {
17+
if (!isString(prev)) {
18+
for (const key in prev) {
19+
if (next[key] == null) {
20+
setStyle(style, key, '')
21+
}
22+
}
23+
} else {
24+
for (const prevStyle of prev.split(';')) {
25+
const key = prevStyle.slice(0, prevStyle.indexOf(':')).trim()
26+
if (next[key] == null) {
27+
setStyle(style, key, '')
28+
}
2029
}
2130
}
2231
}

0 commit comments

Comments
 (0)