diff --git a/packages/runtime-dom/__tests__/patchProps.spec.ts b/packages/runtime-dom/__tests__/patchProps.spec.ts index 402950b4352..f2f3dfdf15e 100644 --- a/packages/runtime-dom/__tests__/patchProps.spec.ts +++ b/packages/runtime-dom/__tests__/patchProps.spec.ts @@ -107,4 +107,21 @@ 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) + }) }) diff --git a/packages/runtime-dom/src/modules/props.ts b/packages/runtime-dom/src/modules/props.ts index 988cbb4ead7..ebecc8972c6 100644 --- a/packages/runtime-dom/src/modules/props.ts +++ b/packages/runtime-dom/src/modules/props.ts @@ -37,6 +37,7 @@ export function patchDOMProp( } else if (value == null && typeof el[key] === 'string') { // e.g.
el[key] = '' + el.removeAttribute(key) } else { // some properties perform value validation and throw try {