From fa1d14c2c82a70743ed837ee91c8966373aa8142 Mon Sep 17 00:00:00 2001 From: zscumt123 Date: Wed, 13 Apr 2022 17:25:11 +0800 Subject: [PATCH] fix(runtime-dom): catch more cases of DOM property setting error (#5552) close #5545 --- .../runtime-dom/__tests__/patchProps.spec.ts | 19 +++++- packages/runtime-dom/src/modules/props.ts | 62 +++++++++---------- 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/packages/runtime-dom/__tests__/patchProps.spec.ts b/packages/runtime-dom/__tests__/patchProps.spec.ts index 24760695040..9a324970cc9 100644 --- a/packages/runtime-dom/__tests__/patchProps.spec.ts +++ b/packages/runtime-dom/__tests__/patchProps.spec.ts @@ -234,12 +234,29 @@ 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..591f8caa7d0 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,5 @@ export function patchDOMProp( ) } } + needRemove && el.removeAttribute(key) }