From 4734f2f579229db5139bbee69ffcf8bca9c34689 Mon Sep 17 00:00:00 2001
From: Marijn Haverbeke <marijnh@gmail.com>
Date: Wed, 3 Dec 2014 15:46:43 +0100
Subject: [PATCH] [contenteditable input] Kill composition when key is handled
 or input is reset

---
 lib/codemirror.js | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/lib/codemirror.js b/lib/codemirror.js
index ea358a0f..07ce3ffa 100644
--- a/lib/codemirror.js
+++ b/lib/codemirror.js
@@ -1506,10 +1506,11 @@
         input.composing.value = e.data;
       });
       on(div, "compositionend", function(e) {
+        if (!input.composing) return;
         var data = e.data || input.composing.value, sel = input.composing.sel;
         setTimeout(function() {
           operation(cm, applyTextInput)(cm, data, 0, sel);
-          input.composing = false;
+          input.composing = null;
         }, 50);
       });
 
@@ -1614,9 +1615,6 @@
 
     supportsTouch: function() { return true; },
 
-    reset: nothing,
-    resetPosition: nothing,
-
     receivedFocus: function() {
       var input = this;
       if (this.selectionInEditor())
@@ -1691,8 +1689,18 @@
       return true;
     },
 
-    ensurePolled: nothing,
-    onContextMenu: nothing,
+    ensurePolled: function() {
+      if (this.composing) this.forceCompositionEnd();
+    },
+    reset: function() {
+      if (this.composing) this.forceCompositionEnd();
+    },
+    forceCompositionEnd: function() {
+      operation(this.cm, applyTextInput)(this.cm, this.composing.value, 0, this.composing.sel);
+      this.composing = null;
+      this.div.blur();
+      this.div.focus();
+    },
 
     setUneditable: function(node) {
       node.setAttribute("contenteditable", "false");
@@ -1701,7 +1709,10 @@
     onKeyPress: function(e) {
       e.preventDefault();
       operation(this.cm, applyTextInput)(this.cm, String.fromCharCode(e.charCode == null ? e.keyCode : e.charCode), 0);
-    }
+    },
+
+    onContextMenu: nothing,
+    resetPosition: nothing
   }, ContentEditableInput.prototype);
 
   function posToDOM(cm, pos) {
-- 
GitLab