Skip to content

Commit 0c725b7

Browse files
committed
Hidden text instances should have correct host context
Adds a test for a subtle edge case that only occurs in persistent mode.
1 parent 9d0d294 commit 0c725b7

File tree

3 files changed

+215
-31
lines changed

3 files changed

+215
-31
lines changed

packages/react-noop-renderer/src/createReactNoop.js

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ type Instance = {|
4141
text: string | null,
4242
prop: any,
4343
hidden: boolean,
44+
context: HostContext,
4445
|};
45-
type TextInstance = {|text: string, id: number, hidden: boolean|};
46+
type TextInstance = {|
47+
text: string,
48+
id: number,
49+
hidden: boolean,
50+
context: HostContext,
51+
|};
52+
type HostContext = Object;
4653

4754
const NO_CONTEXT = {};
55+
const UPPERCASE_CONTEXT = {};
4856
const UPDATE_SIGNAL = {};
4957
if (__DEV__) {
5058
Object.freeze(NO_CONTEXT);
@@ -190,10 +198,11 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
190198
type: type,
191199
children: keepChildren ? instance.children : [],
192200
text: shouldSetTextContent(type, newProps)
193-
? (newProps.children: any) + ''
201+
? computeText((newProps.children: any) + '', instance.context)
194202
: null,
195203
prop: newProps.prop,
196204
hidden: newProps.hidden === true,
205+
context: instance.context,
197206
};
198207
Object.defineProperty(clone, 'id', {
199208
value: clone.id,
@@ -203,6 +212,10 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
203212
value: clone.text,
204213
enumerable: false,
205214
});
215+
Object.defineProperty(clone, 'context', {
216+
value: clone.context,
217+
enumerable: false,
218+
});
206219
hostCloneCounter++;
207220
return clone;
208221
}
@@ -216,20 +229,36 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
216229
);
217230
}
218231

232+
function computeText(rawText, hostContext) {
233+
return hostContext === UPPERCASE_CONTEXT ? rawText.toUpperCase() : rawText;
234+
}
235+
219236
const sharedHostConfig = {
220237
getRootHostContext() {
221238
return NO_CONTEXT;
222239
},
223240

224-
getChildHostContext() {
241+
getChildHostContext(
242+
parentHostContext: HostContext,
243+
type: string,
244+
rootcontainerInstance: Container,
245+
) {
246+
if (type === 'uppercase') {
247+
return UPPERCASE_CONTEXT;
248+
}
225249
return NO_CONTEXT;
226250
},
227251

228252
getPublicInstance(instance) {
229253
return instance;
230254
},
231255

232-
createInstance(type: string, props: Props): Instance {
256+
createInstance(
257+
type: string,
258+
props: Props,
259+
rootContainerInstance: Container,
260+
hostContext: HostContext,
261+
): Instance {
233262
if (type === 'errorInCompletePhase') {
234263
throw new Error('Error in host config.');
235264
}
@@ -238,17 +267,22 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
238267
type: type,
239268
children: [],
240269
text: shouldSetTextContent(type, props)
241-
? (props.children: any) + ''
270+
? computeText((props.children: any) + '', hostContext)
242271
: null,
243272
prop: props.prop,
244273
hidden: props.hidden === true,
274+
context: hostContext,
245275
};
246276
// Hide from unit tests
247277
Object.defineProperty(inst, 'id', {value: inst.id, enumerable: false});
248278
Object.defineProperty(inst, 'text', {
249279
value: inst.text,
250280
enumerable: false,
251281
});
282+
Object.defineProperty(inst, 'context', {
283+
value: inst.context,
284+
enumerable: false,
285+
});
252286
return inst;
253287
},
254288

@@ -298,9 +332,21 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
298332
hostContext: Object,
299333
internalInstanceHandle: Object,
300334
): TextInstance {
301-
const inst = {text: text, id: instanceCounter++, hidden: false};
335+
if (hostContext === UPPERCASE_CONTEXT) {
336+
text = text.toUpperCase();
337+
}
338+
const inst = {
339+
text: text,
340+
id: instanceCounter++,
341+
hidden: false,
342+
context: hostContext,
343+
};
302344
// Hide from unit tests
303345
Object.defineProperty(inst, 'id', {value: inst.id, enumerable: false});
346+
Object.defineProperty(inst, 'context', {
347+
value: inst.context,
348+
enumerable: false,
349+
});
304350
return inst;
305351
},
306352

@@ -351,7 +397,10 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
351397
instance.prop = newProps.prop;
352398
instance.hidden = newProps.hidden === true;
353399
if (shouldSetTextContent(type, newProps)) {
354-
instance.text = (newProps.children: any) + '';
400+
instance.text = computeText(
401+
(newProps.children: any) + '',
402+
instance.context,
403+
);
355404
}
356405
},
357406

@@ -361,7 +410,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
361410
newText: string,
362411
): void {
363412
hostUpdateCounter++;
364-
textInstance.text = newText;
413+
textInstance.text = computeText(newText, textInstance.context);
365414
},
366415

367416
appendChild,
@@ -471,12 +520,21 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
471520
hostContext: Object,
472521
internalInstanceHandle: Object,
473522
): TextInstance {
474-
const inst = {text: text, id: instanceCounter++, hidden: true};
523+
const inst = {
524+
text: text,
525+
id: instanceCounter++,
526+
hidden: true,
527+
context: hostContext,
528+
};
475529
// Hide from unit tests
476530
Object.defineProperty(inst, 'id', {
477531
value: inst.id,
478532
enumerable: false,
479533
});
534+
Object.defineProperty(inst, 'context', {
535+
value: inst.context,
536+
enumerable: false,
537+
});
480538
return inst;
481539
},
482540
};

0 commit comments

Comments
 (0)