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

Function getFallbackBeforeInputChars

code/styling/public/app.js:3388–3444  ·  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

3386 * @return {?string} The fallback string for this `beforeInput` event.
3387 */
3388function getFallbackBeforeInputChars(topLevelType, nativeEvent) {
3389 // If we are currently composing (IME) and using a fallback to do so,
3390 // try to extract the composed characters from the fallback object.
3391 // If composition event is available, we extract a string only at
3392 // compositionevent, otherwise extract it at fallback events.
3393 if (isComposing) {
3394 if (topLevelType === 'topCompositionEnd' || !canUseCompositionEvent && isFallbackCompositionEnd(topLevelType, nativeEvent)) {
3395 var chars = getData();
3396 reset();
3397 isComposing = false;
3398 return chars;
3399 }
3400 return null;
3401 }
3402
3403 switch (topLevelType) {
3404 case 'topPaste':
3405 // If a paste event occurs after a keypress, throw out the input
3406 // chars. Paste events should not lead to BeforeInput events.
3407 return null;
3408 case 'topKeyPress':
3409 /**
3410 * As of v27, Firefox may fire keypress events even when no character
3411 * will be inserted. A few possibilities:
3412 *
3413 * - `which` is `0`. Arrow keys, Esc key, etc.
3414 *
3415 * - `which` is the pressed key code, but no char is available.
3416 * Ex: 'AltGr + d` in Polish. There is no modified character for
3417 * this key combination and no character is inserted into the
3418 * document, but FF fires the keypress for char code `100` anyway.
3419 * No `input` event will occur.
3420 *
3421 * - `which` is the pressed key code, but a command combination is
3422 * being used. Ex: `Cmd+C`. No character is inserted, and no
3423 * `input` event will occur.
3424 */
3425 if (!isKeypressCommand(nativeEvent)) {
3426 // IE fires the `keypress` event when a user types an emoji via
3427 // Touch keyboard of Windows. In such a case, the `char` property
3428 // holds an emoji character like `\uD83D\uDE0A`. Because its length
3429 // is 2, the property `which` does not represent an emoji correctly.
3430 // In such a case, we directly return the `char` property instead of
3431 // using `which`.
3432 if (nativeEvent.char && nativeEvent.char.length > 1) {
3433 return nativeEvent.char;
3434 } else if (nativeEvent.which) {
3435 return String.fromCharCode(nativeEvent.which);
3436 }
3437 }
3438 return null;
3439 case 'topCompositionEnd':
3440 return useFallbackCompositionData ? null : nativeEvent.data;
3441 default:
3442 return null;
3443 }
3444}
3445

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