MCPcopy Create free account
hub / github.com/krasimir/react-in-patterns / getFallbackBeforeInputChars

Function getFallbackBeforeInputChars

code/event-handlers/public/app.js:2718–2774  ·  view source on GitHub ↗

* For browsers that do not provide the `textInput` event, extract the * appropriate string to use for SyntheticInputEvent. * * @param {string} topLevelType Record from `BrowserEventConstants`. * @param {object} nativeEvent Native browser event. * @return {?string} The fallback string for this `

(topLevelType, nativeEvent)

Source from the content-addressed store, hash-verified

2716 * @return {?string} The fallback string for this `beforeInput` event.
2717 */
2718function getFallbackBeforeInputChars(topLevelType, nativeEvent) {
2719 // If we are currently composing (IME) and using a fallback to do so,
2720 // try to extract the composed characters from the fallback object.
2721 // If composition event is available, we extract a string only at
2722 // compositionevent, otherwise extract it at fallback events.
2723 if (isComposing) {
2724 if (topLevelType === 'topCompositionEnd' || !canUseCompositionEvent && isFallbackCompositionEnd(topLevelType, nativeEvent)) {
2725 var chars = getData();
2726 reset();
2727 isComposing = false;
2728 return chars;
2729 }
2730 return null;
2731 }
2732
2733 switch (topLevelType) {
2734 case 'topPaste':
2735 // If a paste event occurs after a keypress, throw out the input
2736 // chars. Paste events should not lead to BeforeInput events.
2737 return null;
2738 case 'topKeyPress':
2739 /**
2740 * As of v27, Firefox may fire keypress events even when no character
2741 * will be inserted. A few possibilities:
2742 *
2743 * - `which` is `0`. Arrow keys, Esc key, etc.
2744 *
2745 * - `which` is the pressed key code, but no char is available.
2746 * Ex: 'AltGr + d` in Polish. There is no modified character for
2747 * this key combination and no character is inserted into the
2748 * document, but FF fires the keypress for char code `100` anyway.
2749 * No `input` event will occur.
2750 *
2751 * - `which` is the pressed key code, but a command combination is
2752 * being used. Ex: `Cmd+C`. No character is inserted, and no
2753 * `input` event will occur.
2754 */
2755 if (!isKeypressCommand(nativeEvent)) {
2756 // IE fires the `keypress` event when a user types an emoji via
2757 // Touch keyboard of Windows. In such a case, the `char` property
2758 // holds an emoji character like `\uD83D\uDE0A`. Because its length
2759 // is 2, the property `which` does not represent an emoji correctly.
2760 // In such a case, we directly return the `char` property instead of
2761 // using `which`.
2762 if (nativeEvent.char && nativeEvent.char.length > 1) {
2763 return nativeEvent.char;
2764 } else if (nativeEvent.which) {
2765 return String.fromCharCode(nativeEvent.which);
2766 }
2767 }
2768 return null;
2769 case 'topCompositionEnd':
2770 return useFallbackCompositionData ? null : nativeEvent.data;
2771 default:
2772 return null;
2773 }
2774}
2775

Callers 1

extractBeforeInputEventFunction · 0.70

Calls 4

isFallbackCompositionEndFunction · 0.70
getDataFunction · 0.70
resetFunction · 0.70
isKeypressCommandFunction · 0.70

Tested by

no test coverage detected