From e2d323538e71d404e729148fd19a08bbc2e3da9b Mon Sep 17 00:00:00 2001 From: zhoulixiang <18366276315@163.com> Date: Sun, 25 Feb 2024 20:53:00 +0800 Subject: [PATCH] fix(runtime-dom): v-bind style should clear previous css string value (#10373) close #10352 --- .../runtime-dom/__tests__/patchStyle.spec.ts | 9 +++++++++ packages/runtime-dom/src/modules/style.ts | 17 +++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/runtime-dom/__tests__/patchStyle.spec.ts b/packages/runtime-dom/__tests__/patchStyle.spec.ts index e7cd0984a19..8b2765e2149 100644 --- a/packages/runtime-dom/__tests__/patchStyle.spec.ts +++ b/packages/runtime-dom/__tests__/patchStyle.spec.ts @@ -158,4 +158,13 @@ describe(`runtime-dom: style patching`, () => { ) expect(el.style.display).toBe('flex') }) + + it('should clear previous css string value', () => { + const el = document.createElement('div') + patchProp(el, 'style', {}, 'color:red') + expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;') + + patchProp(el, 'style', 'color:red', { fontSize: '12px' }) + expect(el.style.cssText.replace(/\s/g, '')).toBe('font-size:12px;') + }) }) diff --git a/packages/runtime-dom/src/modules/style.ts b/packages/runtime-dom/src/modules/style.ts index 9f897a6b2b0..11f7ce1c027 100644 --- a/packages/runtime-dom/src/modules/style.ts +++ b/packages/runtime-dom/src/modules/style.ts @@ -13,10 +13,19 @@ export function patchStyle(el: Element, prev: Style, next: Style) { const currentDisplay = style.display let hasControlledDisplay = false if (next && !isCssString) { - if (prev && !isString(prev)) { - for (const key in prev) { - if (next[key] == null) { - setStyle(style, key, '') + if (prev) { + if (!isString(prev)) { + for (const key in prev) { + if (next[key] == null) { + setStyle(style, key, '') + } + } + } else { + for (const prevStyle of prev.split(';')) { + const key = prevStyle.slice(0, prevStyle.indexOf(':')).trim() + if (next[key] == null) { + setStyle(style, key, '') + } } } }