فهرست منبع

fix(runtime-dom): attribute should be removed with nullish values (#2679)

fix #2677
edison 5 سال پیش
والد
کامیت
fb6b9f8e8f
2فایلهای تغییر یافته به همراه21 افزوده شده و 0 حذف شده
  1. 17 0
      packages/runtime-dom/__tests__/patchProps.spec.ts
  2. 4 0
      packages/runtime-dom/src/modules/props.ts

+ 17 - 0
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', () => {

+ 4 - 0
packages/runtime-dom/src/modules/props.ts

@@ -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 {