diff --git a/src/platforms/web/runtime/directives/model.js b/src/platforms/web/runtime/directives/model.js index c3b669f3bb5..aa0e10e64c2 100644 --- a/src/platforms/web/runtime/directives/model.js +++ b/src/platforms/web/runtime/directives/model.js @@ -6,7 +6,7 @@ import { isTextInputType } from 'web/util/element' import { looseEqual, looseIndexOf } from 'shared/util' import { mergeVNodeHook } from 'core/vdom/helpers/index' -import { warn, isAndroid, isIE9, isIE, isEdge } from 'core/util/index' +import { warn, inBrowser, isIE9, isIE, isEdge } from 'core/util/index' /* istanbul ignore if */ if (isIE9) { @@ -19,6 +19,8 @@ if (isIE9) { }) } +const isCompositionEventSupported = inBrowser && typeof window.CompositionEvent === 'function' + const directive = { inserted (el, binding, vnode, oldVnode) { if (vnode.tag === 'select') { @@ -34,14 +36,14 @@ const directive = { } else if (vnode.tag === 'textarea' || isTextInputType(el.type)) { el._vModifiers = binding.modifiers if (!binding.modifiers.lazy) { - // Safari < 10.2 & UIWebView doesn't fire compositionend when - // switching focus before confirming composition choice - // this also fixes the issue where some browsers e.g. iOS Chrome - // fires "change" instead of "input" on autocomplete. - el.addEventListener('change', onCompositionEnd) - if (!isAndroid) { + if (isCompositionEventSupported) { el.addEventListener('compositionstart', onCompositionStart) el.addEventListener('compositionend', onCompositionEnd) + // Safari < 10.2 & UIWebView doesn't fire compositionend when + // switching focus before confirming composition choice + // this also fixes the issue where some browsers e.g. iOS Chrome + // fires "change" instead of "input" on autocomplete. + el.addEventListener('change', onCompositionEnd) } /* istanbul ignore if */ if (isIE9) { diff --git a/test/unit/features/directives/model-text.spec.js b/test/unit/features/directives/model-text.spec.js index 7aae24b654e..e4ae1dd0107 100644 --- a/test/unit/features/directives/model-text.spec.js +++ b/test/unit/features/directives/model-text.spec.js @@ -1,5 +1,7 @@ import Vue from 'vue' -import { isIE9, isIE, isAndroid } from 'core/util/env' +import { inBrowser, isIE9, isIE } from 'core/util/env' + +const isCompositionEventSupported = inBrowser && typeof window.CompositionEvent === 'function' describe('Directive v-model text', () => { it('should update value both ways', done => { @@ -183,7 +185,7 @@ describe('Directive v-model text', () => { }) } - if (!isAndroid) { + if (isCompositionEventSupported) { it('compositionevents', function (done) { const vm = new Vue({ data: { @@ -291,7 +293,7 @@ describe('Directive v-model text', () => { expect('conflicts with v-model').not.toHaveBeenWarned() }) - if (!isAndroid) { + if (isCompositionEventSupported) { it('does not trigger extra input events with single compositionend', () => { const spy = jasmine.createSpy() const vm = new Vue({