From 0163a6fe53cc17a69450f5c0ef0ac16308c1f5b6 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 8 Dec 2016 16:02:26 -0500 Subject: [PATCH] proper fix for #4392 (via #4402) --- .../web/runtime/modules/dom-props.js | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/platforms/web/runtime/modules/dom-props.js b/src/platforms/web/runtime/modules/dom-props.js index 335eac24..352faa6e 100644 --- a/src/platforms/web/runtime/modules/dom-props.js +++ b/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