Skip to content

Commit 0163a6f

Browse files
committed
proper fix for vuejs#4392 (via vuejs#4402)
1 parent 8567e7d commit 0163a6f

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/platforms/web/runtime/modules/dom-props.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import { extend } from 'shared/util'
3+
import { extend, toNumber } from 'shared/util'
44

55
function updateDOMProps (oldVnode: VNodeWithData, vnode: VNodeWithData) {
66
if (!oldVnode.data.domProps && !vnode.data.domProps) {
@@ -35,7 +35,10 @@ function updateDOMProps (oldVnode: VNodeWithData, vnode: VNodeWithData) {
3535
elm._value = cur
3636
// avoid resetting cursor position when value is the same
3737
const strCur = cur == null ? '' : String(cur)
38-
if (elm.value !== strCur && !elm.composing && document.activeElement !== elm) {
38+
if (!elm.composing && (
39+
(document.activeElement !== elm && elm.value !== strCur) ||
40+
isValueChanged(vnode, strCur)
41+
)) {
3942
elm.value = strCur
4043
}
4144
} else {
@@ -44,6 +47,29 @@ function updateDOMProps (oldVnode: VNodeWithData, vnode: VNodeWithData) {
4447
}
4548
}
4649

50+
function isValueChanged (vnode: VNodeWithData, newVal: string): boolean {
51+
const value = vnode.elm.value
52+
const modifiers = getModelModifier(vnode)
53+
if ((modifiers && modifiers.number) || vnode.elm.type === 'number') {
54+
return toNumber(value) !== toNumber(newVal)
55+
}
56+
if (modifiers && modifiers.trim) {
57+
return value.trim() !== newVal.trim()
58+
}
59+
return value !== newVal
60+
}
61+
62+
function getModelModifier (vnode: VNodeWithData): ?ASTModifiers {
63+
const directives = vnode.data.directives
64+
if (!directives) return
65+
for (let i = 0, directive; i < directives.length; i++) {
66+
directive = directives[i]
67+
if (directive.name === 'model') {
68+
return directive.modifiers
69+
}
70+
}
71+
}
72+
4773
export default {
4874
create: updateDOMProps,
4975
update: updateDOMProps

0 commit comments

Comments
 (0)