1
1
/* @flow */
2
2
3
- import { extend } from 'shared/util'
3
+ import { extend , toNumber } from 'shared/util'
4
4
5
5
function updateDOMProps ( oldVnode : VNodeWithData , vnode : VNodeWithData ) {
6
6
if ( ! oldVnode . data . domProps && ! vnode . data . domProps ) {
@@ -35,7 +35,10 @@ function updateDOMProps (oldVnode: VNodeWithData, vnode: VNodeWithData) {
35
35
elm . _value = cur
36
36
// avoid resetting cursor position when value is the same
37
37
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
+ ) ) {
39
42
elm . value = strCur
40
43
}
41
44
} else {
@@ -44,6 +47,29 @@ function updateDOMProps (oldVnode: VNodeWithData, vnode: VNodeWithData) {
44
47
}
45
48
}
46
49
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
+
47
73
export default {
48
74
create : updateDOMProps ,
49
75
update : updateDOMProps
0 commit comments