Skip to content

Commit cc52e06

Browse files
authored
Prevent BeforeInputPlugin from returning [null, null] (#11848)
The BeforeInputPlugin dispatches null elements in an array if composition or beforeInput events are not extracted. This causes a an extra array allocation, but more importantly creates null states in later event dispatch methods that are annoying to account for. This commit makes it so that BeforeInputPlugin never returns a null element inside an array.
1 parent 8ec146c commit cc52e06

File tree

2 files changed

+44
-62
lines changed

2 files changed

+44
-62
lines changed

packages/react-dom/src/events/BeforeInputEventPlugin.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -453,20 +453,29 @@ const BeforeInputEventPlugin = {
453453
nativeEvent,
454454
nativeEventTarget,
455455
) {
456-
return [
457-
extractCompositionEvent(
458-
topLevelType,
459-
targetInst,
460-
nativeEvent,
461-
nativeEventTarget,
462-
),
463-
extractBeforeInputEvent(
464-
topLevelType,
465-
targetInst,
466-
nativeEvent,
467-
nativeEventTarget,
468-
),
469-
];
456+
const composition = extractCompositionEvent(
457+
topLevelType,
458+
targetInst,
459+
nativeEvent,
460+
nativeEventTarget,
461+
);
462+
463+
const beforeInput = extractBeforeInputEvent(
464+
topLevelType,
465+
targetInst,
466+
nativeEvent,
467+
nativeEventTarget,
468+
);
469+
470+
if (composition === null) {
471+
return beforeInput;
472+
}
473+
474+
if (beforeInput === null) {
475+
return composition;
476+
}
477+
478+
return [composition, beforeInput];
470479
},
471480
};
472481

packages/react-dom/src/events/__tests__/BeforeInputEventPlugin-test.internal.js

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ describe('BeforeInputEventPlugin', function() {
7676
return function() {
7777
const newArgs = [node].concat(Array.prototype.slice.call(arguments));
7878
const newEvents = extract.apply(this, newArgs);
79-
Array.prototype.push.apply(events, newEvents);
79+
80+
if (Array.isArray(newEvents)) {
81+
Array.prototype.push.apply(events, newEvents);
82+
} else if (newEvents) {
83+
events.push(newEvents);
84+
}
8085
};
8186
}
8287

@@ -156,32 +161,17 @@ describe('BeforeInputEventPlugin', function() {
156161
// keyUp.
157162
const Expected_Webkit = () => [
158163
{type: ModuleCache.SyntheticCompositionEvent, data: {}},
159-
{type: null},
160-
{type: null},
161164
{type: ModuleCache.SyntheticInputEvent, data: {data: 'A'}},
162-
{type: null},
163-
{type: null}, // textinput of A
164-
{type: null},
165-
{type: null}, // keyUp of 65
166-
{type: null},
165+
// textinput of A
166+
// keyUp of 65
167167
{type: ModuleCache.SyntheticInputEvent, data: {data: 'abc'}},
168-
{type: null},
169-
{type: null}, // textinput of abc
170-
{type: null},
171-
{type: null}, // keyUp of 32
172-
{type: null},
168+
// textinput of abc
169+
// keyUp of 32
173170
{type: ModuleCache.SyntheticInputEvent, data: {data: 'xyz'}},
174-
{type: null},
175-
{type: null}, // textinput of xyz
176-
{type: null},
177-
{type: null}, // keyUp of 32
171+
// textinput of xyz
172+
// keyUp of 32
178173
{type: ModuleCache.SyntheticCompositionEvent, data: {data: 'Hello'}},
179-
{type: null},
180-
181174
// Emoji test
182-
{type: null},
183-
{type: null},
184-
{type: null},
185175
{type: ModuleCache.SyntheticInputEvent, data: {data: '\uD83D\uDE0A'}},
186176
];
187177

@@ -191,39 +181,22 @@ describe('BeforeInputEventPlugin', function() {
191181
// element, not event data.
192182
const Expected_IE11 = () => [
193183
{type: ModuleCache.SyntheticCompositionEvent, data: {}},
194-
{type: null},
195-
{type: null},
196-
{type: null}, // textInput of A
197-
{type: null},
198-
{type: null}, // textinput of A
199-
{type: null},
200-
{type: null}, // keyUp of 65
201-
{type: null},
202-
{type: null}, // textInput of abc
203-
{type: null},
204-
{type: null}, // textinput of abc
205-
184+
// textInput of A
185+
// textinput of A
186+
// keyUp of 65
187+
// textInput of abc
188+
// textinput of abc
206189
// fallbackData should NOT be set at keyUp with any of END_KEYCODES
207-
{type: null},
208-
{type: null}, // keyUp of 32
209-
210-
{type: null},
211-
{type: null}, // textInput of xyz
212-
{type: null},
213-
{type: null}, // textinput of xyz
214-
{type: null},
215-
{type: null}, // keyUp of 32
216-
190+
// keyUp of 32
191+
// textInput of xyz
192+
// textinput of xyz
193+
// keyUp of 32
217194
// fallbackData is retrieved from the element, which is XYZ,
218195
// at a time of compositionend
219196
{type: ModuleCache.SyntheticCompositionEvent, data: {}},
220197
{type: ModuleCache.SyntheticInputEvent, data: {data: 'XYZ'}},
221-
222198
// Emoji test
223-
{type: null},
224199
{type: ModuleCache.SyntheticInputEvent, data: {data: '\uD83D\uDE0A'}},
225-
{type: null},
226-
{type: null},
227200
];
228201

229202
function TestEditableReactComponent(Emulator, Scenario, ExpectedResult) {

0 commit comments

Comments
 (0)