]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-dom): remove attrs with nullish values
authorEvan You <yyx990803@gmail.com>
Tue, 14 Jul 2020 20:25:21 +0000 (16:25 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 14 Jul 2020 20:25:21 +0000 (16:25 -0400)
fix #1576

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

index 402950b4352d0e8ac7b88bcc993c8ff672dac7e0..f2f3dfdf15e53ec54514455cda873739d7173394 100644 (file)
@@ -107,4 +107,21 @@ describe('runtime-dom: props patching', () => {
 
     expect(`Failed setting prop "someProp" on <div>`).toHaveBeenWarnedLast()
   })
+
+  // #1576
+  test('remove attribute when value is falsy', () => {
+    const el = document.createElement('div')
+    patchProp(el, 'id', null, '')
+    expect(el.hasAttribute('id')).toBe(true)
+    patchProp(el, 'id', null, null)
+    expect(el.hasAttribute('id')).toBe(false)
+
+    patchProp(el, 'id', null, '')
+    expect(el.hasAttribute('id')).toBe(true)
+    patchProp(el, 'id', null, undefined)
+    expect(el.hasAttribute('id')).toBe(false)
+
+    patchProp(el, 'id', null, '')
+    expect(el.hasAttribute('id')).toBe(true)
+  })
 })
index 988cbb4ead756fe0988f710228428cb8139e8480..ebecc8972c67f4f1197252916bbe9d3fe3900664 100644 (file)
@@ -37,6 +37,7 @@ export function patchDOMProp(
   } else if (value == null && typeof el[key] === 'string') {
     // e.g. <div :id="null">
     el[key] = ''
+    el.removeAttribute(key)
   } else {
     // some properties perform value validation and throw
     try {