Evan You 9 лет назад
Родитель
Сommit
0163a6fe53
1 измененных файлов с 28 добавлено и 2 удалено
  1. 28 2
      src/platforms/web/runtime/modules/dom-props.js

+ 28 - 2
src/platforms/web/runtime/modules/dom-props.js

@@ -1,6 +1,6 @@
 /* @flow */
 
-import { extend } from 'shared/util'
+import { extend, toNumber } from 'shared/util'
 
 function updateDOMProps (oldVnode: VNodeWithData, vnode: VNodeWithData) {
   if (!oldVnode.data.domProps && !vnode.data.domProps) {
@@ -35,7 +35,10 @@ function updateDOMProps (oldVnode: VNodeWithData, vnode: VNodeWithData) {
       elm._value = cur
       // avoid resetting cursor position when value is the same
       const strCur = cur == null ? '' : String(cur)
-      if (elm.value !== strCur && !elm.composing && document.activeElement !== elm) {
+      if (!elm.composing && (
+          (document.activeElement !== elm && elm.value !== strCur) ||
+          isValueChanged(vnode, strCur)
+      )) {
         elm.value = strCur
       }
     } else {
@@ -44,6 +47,29 @@ function updateDOMProps (oldVnode: VNodeWithData, vnode: VNodeWithData) {
   }
 }
 
+function isValueChanged (vnode: VNodeWithData, newVal: string): boolean {
+  const value = vnode.elm.value
+  const modifiers = getModelModifier(vnode)
+  if ((modifiers && modifiers.number) || vnode.elm.type === 'number') {
+    return toNumber(value) !== toNumber(newVal)
+  }
+  if (modifiers && modifiers.trim) {
+    return value.trim() !== newVal.trim()
+  }
+  return value !== newVal
+}
+
+function getModelModifier (vnode: VNodeWithData): ?ASTModifiers {
+  const directives = vnode.data.directives
+  if (!directives) return
+  for (let i = 0, directive; i < directives.length; i++) {
+    directive = directives[i]
+    if (directive.name === 'model') {
+      return directive.modifiers
+    }
+  }
+}
+
 export default {
   create: updateDOMProps,
   update: updateDOMProps