]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-dom): attribute should be removed with nullish values (#2679)
authoredison <daiwei521@126.com>
Tue, 1 Dec 2020 00:28:03 +0000 (08:28 +0800)
committerGitHub <noreply@github.com>
Tue, 1 Dec 2020 00:28:03 +0000 (19:28 -0500)
fix #2677

packages/runtime-dom/__tests__/patchProps.spec.ts
packages/runtime-dom/src/modules/props.ts

index 0501e04d01bb03684ad6014fe64dbdc4aed070b3..687b2b7905652eaf8cc8e81ea4af6be74a23b731 100644 (file)
@@ -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', () => {
index 1a50d612b5058b5705ce68b4159300ed22c8147e..ab7a23a02f2ee1c337a9a76e2374d7520ef10453 100644 (file)
@@ -41,6 +41,10 @@ export function patchDOMProp(
     // e.g. <div :id="null">
     el[key] = ''
     el.removeAttribute(key)
+  } else if ((value == null || value === '') && typeof el[key] === 'number') {
+    // e.g. <img :width="null">
+    el[key] = 0
+    el.removeAttribute(key)
   } else {
     // some properties perform value validation and throw
     try {