From: edison Date: Tue, 1 Dec 2020 00:28:03 +0000 (+0800) Subject: fix(runtime-dom): attribute should be removed with nullish values (#2679) X-Git-Tag: v3.0.4~19 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fb6b9f8e8ff35ca4d8723a9f96e36266de0dd947;p=thirdparty%2Fvuejs%2Fcore.git fix(runtime-dom): attribute should be removed with nullish values (#2679) fix #2677 --- diff --git a/packages/runtime-dom/__tests__/patchProps.spec.ts b/packages/runtime-dom/__tests__/patchProps.spec.ts index 0501e04d01..687b2b7905 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 1a50d612b5..ab7a23a02f 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 {