Skip to content

Commit fb6b9f8

Browse files
authored
fix(runtime-dom): attribute should be removed with nullish values (#2679)
fix #2677
1 parent 64d4681 commit fb6b9f8

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ describe('runtime-dom: props patching', () => {
120120

121121
patchProp(el, 'id', null, '')
122122
expect(el.hasAttribute('id')).toBe(true)
123+
124+
// #2677
125+
const img = document.createElement('img')
126+
patchProp(img, 'width', null, '')
127+
expect(el.hasAttribute('width')).toBe(false)
128+
patchProp(img, 'width', null, 0)
129+
expect(img.hasAttribute('width')).toBe(true)
130+
131+
patchProp(img, 'width', null, null)
132+
expect(img.hasAttribute('width')).toBe(false)
133+
patchProp(img, 'width', null, 0)
134+
expect(img.hasAttribute('width')).toBe(true)
135+
136+
patchProp(img, 'width', null, undefined)
137+
expect(img.hasAttribute('width')).toBe(false)
138+
patchProp(img, 'width', null, 0)
139+
expect(img.hasAttribute('width')).toBe(true)
123140
})
124141

125142
test('form attribute', () => {

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

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export function patchDOMProp(
4141
// e.g. <div :id="null">
4242
el[key] = ''
4343
el.removeAttribute(key)
44+
} else if ((value == null || value === '') && typeof el[key] === 'number') {
45+
// e.g. <img :width="null">
46+
el[key] = 0
47+
el.removeAttribute(key)
4448
} else {
4549
// some properties perform value validation and throw
4650
try {

0 commit comments

Comments
 (0)