Skip to content

Commit

Permalink
fix(runtime-dom): v-bind style should clear previous css string value (
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-lx authored and OnlyWick committed Feb 27, 2024
1 parent a795ddd commit a920111
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 9 additions & 0 deletions packages/runtime-dom/__tests__/patchStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;')
})
})
17 changes: 13 additions & 4 deletions packages/runtime-dom/src/modules/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')
}
}
}
}
Expand Down

0 comments on commit a920111

Please sign in to comment.