From 44e1735514a81befb2d3c90ad30762241cac4f1c Mon Sep 17 00:00:00 2001 From: zhangshuai Date: Wed, 9 Mar 2022 19:08:19 +0800 Subject: [PATCH] fix: some dom property setting error(#5545) --- .../runtime-dom/__tests__/patchProps.spec.ts | 17 ++++- packages/runtime-dom/src/modules/props.ts | 63 +++++++++---------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/packages/runtime-dom/__tests__/patchProps.spec.ts b/packages/runtime-dom/__tests__/patchProps.spec.ts index 07aee676ded..b923f07275e 100644 --- a/packages/runtime-dom/__tests__/patchProps.spec.ts +++ b/packages/runtime-dom/__tests__/patchProps.spec.ts @@ -234,12 +234,27 @@ describe('runtime-dom: props patching', () => { expect(el.getAttribute('x')).toBe('2') }) - test('input with size', () => { + test('input with size (number property)', () => { const el = document.createElement('input') patchProp(el, 'size', null, 100) expect(el.size).toBe(100) patchProp(el, 'size', 100, null) expect(el.getAttribute('size')).toBe(null) + expect('Failed setting prop "size" on ').toHaveBeenWarnedLast() + }) + test('select with type (string property)', () => { + const el = document.createElement('select') + patchProp(el, 'type', null, 'test') + expect(el.type).toBe('select-one') + expect('Failed setting prop "type" on ' + ).toHaveBeenWarnedLast() }) test('patch value for select', () => { diff --git a/packages/runtime-dom/src/modules/props.ts b/packages/runtime-dom/src/modules/props.ts index 5092911e44a..401de475593 100644 --- a/packages/runtime-dom/src/modules/props.ts +++ b/packages/runtime-dom/src/modules/props.ts @@ -51,51 +51,48 @@ export function patchDOMProp( return } + let needRemove = false if (value === '' || value == null) { const type = typeof el[key] if (type === 'boolean') { // e.g. try { el[key] = value } catch (e: any) { @@ -107,4 +104,6 @@ export function patchDOMProp( ) } } + needRemove && el.removeAttribute(key) + return value }