diff --git a/packages/runtime-dom/__tests__/patchProps.spec.ts b/packages/runtime-dom/__tests__/patchProps.spec.ts index 402950b4352..f17ea81f3b2 100644 --- a/packages/runtime-dom/__tests__/patchProps.spec.ts +++ b/packages/runtime-dom/__tests__/patchProps.spec.ts @@ -107,4 +107,23 @@ describe('runtime-dom: props patching', () => { expect(`Failed setting prop "someProp" on
`).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) + patchProp(el, 'id', null, false) + expect(el.hasAttribute('id')).toBe(false) + }) }) diff --git a/packages/runtime-dom/src/modules/props.ts b/packages/runtime-dom/src/modules/props.ts index 988cbb4ead7..56e684e3de5 100644 --- a/packages/runtime-dom/src/modules/props.ts +++ b/packages/runtime-dom/src/modules/props.ts @@ -31,12 +31,14 @@ export function patchDOMProp( el.value = value == null ? '' : value return } + // #1576 + if (isFalsyAttrValue(value)) { + el.removeAttribute(key) + return + } if (value === '' && typeof el[key] === 'boolean') { // e.g.