@samitouri / QOS-React-2 / commits / ad70848e80

Use fallback data for composition events if necessary

Isaac Salier-Hellendag committed Jan 13, 2014 at 12:53 UTC ad70848e80238d12c26af41a0bc6d1c9b6fea2da
1 file changed +30 -6
src/eventPlugins/CompositionEventPlugin.js
+30 -6
@@ -31,8 +31,22 @@ var keyOf = require('keyOf');
31 var END_KEYCODES = [9, 13, 27, 32]; // Tab, Return, Esc, Space
32 var START_KEYCODE = 229;
33
34 -var useCompositionEvent = ExecutionEnvironment.canUseDOM &&
35 - 'CompositionEvent' in window;
34 +var useCompositionEvent = (
35 + ExecutionEnvironment.canUseDOM &&
36 + 'CompositionEvent' in window
37 +);
38 +
39 +// In IE9+, we have access to composition events, but the data supplied
40 +// by the native compositionend event may be incorrect. In Korean, for example,
41 +// the compositionend event contains only one character regardless of
42 +// how many characters have been composed since compositionstart.
43 +// We therefore use the fallback data while still using the native
44 +// events as triggers.
45 +var useFallbackData = (
46 + !useCompositionEvent ||
47 + 'documentMode' in document && document.documentMode > 8
48 +);
49 +
50 var topLevelTypes = EventConstants.topLevelTypes;
51 var currentComposition = null;
52
@@ -207,13 +221,23 @@ var CompositionEventPlugin = {
221 eventType = getCompositionEventType(topLevelType);
222 } else if (!currentComposition) {
223 if (isFallbackStart(topLevelType, nativeEvent)) {
210 - eventType = eventTypes.start;
211 - currentComposition = new FallbackCompositionState(topLevelTarget);
224 + eventType = eventTypes.compositionStart;
225 }
226 } else if (isFallbackEnd(topLevelType, nativeEvent)) {
227 eventType = eventTypes.compositionEnd;
215 - data = currentComposition.getData();
216 - currentComposition = null;
228 + }
229 +
230 + if (useFallbackData) {
231 + // The current composition is stored statically and must not be
232 + // overwritten while composition continues.
233 + if (!currentComposition && eventType === eventTypes.compositionStart) {
234 + currentComposition = new FallbackCompositionState(topLevelTarget);
235 + } else if (eventType === eventTypes.compositionEnd) {
236 + if (currentComposition) {
237 + data = currentComposition.getData();
238 + currentComposition = null;
239 + }
240 + }
241 }
242
243 if (eventType) {