Skip to content

Commit

Permalink
fix(runtime-dom): handle multiple patch with falsy style value
Browse files Browse the repository at this point in the history
  • Loading branch information
linzhe141 committed Nov 12, 2024
1 parent ed01d92 commit 58464f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 26 additions & 0 deletions packages/runtime-dom/__tests__/patchStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ describe(`runtime-dom: style patching`, () => {
expect(el.style.width).toBe('0px')
})

it('multiple patch with falsy style value', () => {
const el = document.createElement('div')
const styleA = {
left: 0,
right: false,
top: 0,
bottom: false,
}
patchProp(el as any, 'style', null, styleA)
expect(el.style.left).toBe('0px')
expect(el.style.right).toBe('')
expect(el.style.top).toBe('0px')
expect(el.style.bottom).toBe('')
const styleB = {
left: false,
right: 0,
top: false,
bottom: 0,
}
patchProp(el as any, 'style', styleA, styleB)
expect(el.style.left).toBe('')
expect(el.style.right).toBe('0px')
expect(el.style.top).toBe('')
expect(el.style.bottom).toBe('0px')
})

it('should remove style attribute on falsy value', () => {
const el = document.createElement('div')
el.style.cssText = 'color: red;'
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-dom/src/modules/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export function patchStyle(el: Element, prev: Style, next: Style): void {
if (prev) {
if (!isString(prev)) {
for (const key in prev) {
if (next[key] == null) {
if (!next[key]) {
setStyle(style, key, '')
}
}
} else {
for (const prevStyle of prev.split(';')) {
const key = prevStyle.slice(0, prevStyle.indexOf(':')).trim()
if (next[key] == null) {
if (!next[key]) {
setStyle(style, key, '')
}
}
Expand Down

0 comments on commit 58464f2

Please sign in to comment.