diff --git a/packages/runtime-dom/__tests__/patchProps.spec.ts b/packages/runtime-dom/__tests__/patchProps.spec.ts index 0501e04d01b..687b2b79056 100644 --- a/packages/runtime-dom/__tests__/patchProps.spec.ts +++ b/packages/runtime-dom/__tests__/patchProps.spec.ts @@ -120,6 +120,23 @@ describe('runtime-dom: props patching', () => { patchProp(el, 'id', null, '') expect(el.hasAttribute('id')).toBe(true) + + // #2677 + const img = document.createElement('img') + patchProp(img, 'width', null, '') + expect(el.hasAttribute('width')).toBe(false) + patchProp(img, 'width', null, 0) + expect(img.hasAttribute('width')).toBe(true) + + patchProp(img, 'width', null, null) + expect(img.hasAttribute('width')).toBe(false) + patchProp(img, 'width', null, 0) + expect(img.hasAttribute('width')).toBe(true) + + patchProp(img, 'width', null, undefined) + expect(img.hasAttribute('width')).toBe(false) + patchProp(img, 'width', null, 0) + expect(img.hasAttribute('width')).toBe(true) }) test('form attribute', () => { diff --git a/packages/runtime-dom/src/modules/props.ts b/packages/runtime-dom/src/modules/props.ts index 1a50d612b50..ab7a23a02f2 100644 --- a/packages/runtime-dom/src/modules/props.ts +++ b/packages/runtime-dom/src/modules/props.ts @@ -41,6 +41,10 @@ export function patchDOMProp( // e.g.
el[key] = '' el.removeAttribute(key) + } else if ((value == null || value === '') && typeof el[key] === 'number') { + // e.g. + el[key] = 0 + el.removeAttribute(key) } else { // some properties perform value validation and throw try {