Skip to content

Commit

Permalink
fix(vdom): Don't replace input for text-like type change (vuejs#6344)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmessing authored and 张祥闰 committed Sep 5, 2017
1 parent b5afbc0 commit 7a52104
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
7 changes: 2 additions & 5 deletions src/core/vdom/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*
* modified by Evan You (@yyx990803)
*
/*
* Not type-checking this because this file is perf-critical and the cost
* of making flow understand it is not worth it.
*/
Expand All @@ -17,6 +15,7 @@ import config from '../config'
import { SSR_ATTR } from 'shared/constants'
import { registerRef } from './modules/ref'
import { activeInstance } from '../instance/lifecycle'
import { isTextInputType } from 'web/util/element'

import {
warn,
Expand Down Expand Up @@ -48,14 +47,12 @@ function sameVnode (a, b) {
)
}

// Some browsers do not support dynamically changing type for <input>
// so they need to be treated as different nodes
function sameInputType (a, b) {
if (a.tag !== 'input') return true
let i
const typeA = isDef(i = a.data) && isDef(i = i.attrs) && i.type
const typeB = isDef(i = b.data) && isDef(i = i.attrs) && i.type
return typeA === typeB
return typeA === typeB || isTextInputType(typeA) && isTextInputType(typeB)
}

function createKeyToOldIdx (children, beginIdx, endIdx) {
Expand Down
5 changes: 2 additions & 3 deletions src/platforms/web/runtime/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* properties to Elements.
*/

import { looseEqual, looseIndexOf, makeMap } from 'shared/util'
import { isTextInputType } from 'web/util/element'
import { looseEqual, looseIndexOf } from 'shared/util'
import { warn, isAndroid, isIE9, isIE, isEdge } from 'core/util/index'

const isTextInputType = makeMap('text,number,password,search,email,tel,url')

/* istanbul ignore if */
if (isIE9) {
// http://www.matts411.com/post/internet-explorer-9-oninput/
Expand Down
2 changes: 2 additions & 0 deletions src/platforms/web/util/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ export function isUnknownElement (tag: string): boolean {
return (unknownElementCache[tag] = /HTMLUnknownElement/.test(el.toString()))
}
}

export const isTextInputType = makeMap('text,number,password,search,email,tel,url')
26 changes: 26 additions & 0 deletions test/unit/modules/vdom/patch/edge-cases.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,30 @@ describe('vdom patch: edge cases', () => {
expect(vm.$el.children[0].value).toBe('a')
}).then(done)
})

// #6313
it('should not replace node when switching between text-like inputs', done => {
const vm = new Vue({
data: { show: false },
template: `
<div>
<input :type="show ? 'text' : 'password'">
</div>
`
}).$mount()
const node = vm.$el.children[0]
expect(vm.$el.children[0].type).toBe('password')
vm.$el.children[0].value = 'test'
vm.show = true
waitForUpdate(() => {
expect(vm.$el.children[0]).toBe(node)
expect(vm.$el.children[0].value).toBe('test')
expect(vm.$el.children[0].type).toBe('text')
vm.show = false
}).then(() => {
expect(vm.$el.children[0]).toBe(node)
expect(vm.$el.children[0].value).toBe('test')
expect(vm.$el.children[0].type).toBe('password')
}).then(done)
})
})

0 comments on commit 7a52104

Please sign in to comment.