Skip to content

Commit 00038cb

Browse files
committed
fix($vdom): Don't replace input for text-like type change
vuejs#6313
1 parent c628103 commit 00038cb

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

src/core/vdom/patch.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
*
77
* modified by Evan You (@yyx990803)
88
*
9-
10-
/*
119
* Not type-checking this because this file is perf-critical and the cost
1210
* of making flow understand it is not worth it.
1311
*/
1412

1513
import VNode from './vnode'
1614
import config from '../config'
17-
import { SSR_ATTR } from 'shared/constants'
15+
import { SSR_ATTR, TEXT_INPUT_TYPES } from 'shared/constants'
1816
import { registerRef } from './modules/ref'
1917
import { activeInstance } from '../instance/lifecycle'
2018

@@ -27,6 +25,8 @@ import {
2725
isPrimitive
2826
} from '../util/index'
2927

28+
const isTextInputType = makeMap(TEXT_INPUT_TYPES)
29+
3030
export const emptyNode = new VNode('', {}, [])
3131

3232
const hooks = ['create', 'activate', 'update', 'remove', 'destroy']
@@ -48,14 +48,12 @@ function sameVnode (a, b) {
4848
)
4949
}
5050

51-
// Some browsers do not support dynamically changing type for <input>
52-
// so they need to be treated as different nodes
5351
function sameInputType (a, b) {
5452
if (a.tag !== 'input') return true
5553
let i
5654
const typeA = isDef(i = a.data) && isDef(i = i.attrs) && i.type
5755
const typeB = isDef(i = b.data) && isDef(i = i.attrs) && i.type
58-
return typeA === typeB
56+
return typeA === typeB || isTextInputType(typeA) && isTextInputType(typeB)
5957
}
6058

6159
function createKeyToOldIdx (children, beginIdx, endIdx) {

src/platforms/web/runtime/directives/model.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55

66
import { looseEqual, looseIndexOf, makeMap } from 'shared/util'
7+
import { TEXT_INPUT_TYPES } from 'shared/constants'
78
import { warn, isAndroid, isIE9, isIE, isEdge } from 'core/util/index'
89

9-
const isTextInputType = makeMap('text,number,password,search,email,tel,url')
10+
const isTextInputType = makeMap(TEXT_INPUT_TYPES)
1011

1112
/* istanbul ignore if */
1213
if (isIE9) {

src/shared/constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ export const LIFECYCLE_HOOKS = [
1818
'activated',
1919
'deactivated'
2020
]
21+
22+
export const TEXT_INPUT_TYPES = 'text,number,password,search,email,tel,url'

test/unit/modules/vdom/patch/edge-cases.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,27 @@ describe('vdom patch: edge cases', () => {
135135
expect(vm.$el.children[0].value).toBe('a')
136136
}).then(done)
137137
})
138+
139+
// #6313
140+
it('should not replace node when switching between text-like inputs', done => {
141+
const vm = new Vue({
142+
data: { show: false },
143+
template: `
144+
<div>
145+
<input :type="show ? 'text' : 'password'">
146+
</div>
147+
`
148+
}).$mount()
149+
const node = vm.$el.children[0]
150+
vm.$el.children[0].value = 'test'
151+
vm.ok = true
152+
waitForUpdate(() => {
153+
expect(vm.$el.children[0]).toBe(node)
154+
expect(vm.$el.children[0].value).toBe('test')
155+
vm.ok = false
156+
}).then(() => {
157+
expect(vm.$el.children[0]).toBe(node)
158+
expect(vm.$el.children[0].value).toBe('test')
159+
}).then(done)
160+
})
138161
})

0 commit comments

Comments
 (0)