Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 690f7ef

Browse files
niveditcfacebook-github-bot
authored andcommitted
Fix check for tree blocks
Summary: We were wrongly converting from tree to raw whenever a block had a children array, even if no blocks actually have children. This is bad because when we toggle the GK, we clear the children, but we don't necessarily delete the field altogether (and shouldn't). A tree to raw conversion (or vice versa) is needed only when blocks are actually nested. Reviewed By: agoder Differential Revision: D10145467 fbshipit-source-id: 10e489e9c3ba3ae0d16c3da4767cd1400345d92e
1 parent 0688fa3 commit 690f7ef

File tree

3 files changed

+227
-1
lines changed

3 files changed

+227
-1
lines changed

src/model/encoding/__tests__/__snapshots__/convertFromRawToDraftState-test.js.snap

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,174 @@ Object {
278278
}
279279
`;
280280

281+
exports[`ignore empty children array 1`] = `
282+
Object {
283+
"A": Object {
284+
"characterList": Array [
285+
Object {
286+
"entity": null,
287+
"style": Array [],
288+
},
289+
],
290+
"children": Array [],
291+
"data": Object {},
292+
"depth": 0,
293+
"key": "A",
294+
"nextSibling": "B",
295+
"parent": null,
296+
"prevSibling": null,
297+
"text": "A",
298+
"type": "ordered-list-item",
299+
},
300+
"B": Object {
301+
"characterList": Array [
302+
Object {
303+
"entity": null,
304+
"style": Array [],
305+
},
306+
],
307+
"children": Array [],
308+
"data": Object {},
309+
"depth": 0,
310+
"key": "B",
311+
"nextSibling": "C",
312+
"parent": null,
313+
"prevSibling": "A",
314+
"text": "B",
315+
"type": "ordered-list-item",
316+
},
317+
"C": Object {
318+
"characterList": Array [
319+
Object {
320+
"entity": null,
321+
"style": Array [],
322+
},
323+
],
324+
"children": Array [],
325+
"data": Object {},
326+
"depth": 0,
327+
"key": "C",
328+
"nextSibling": null,
329+
"parent": null,
330+
"prevSibling": "B",
331+
"text": "C",
332+
"type": "ordered-list-item",
333+
},
334+
}
335+
`;
336+
337+
exports[`ignore empty children array for tree conversion 1 1`] = `
338+
Object {
339+
"A": Object {
340+
"characterList": Array [
341+
Object {
342+
"entity": null,
343+
"style": Array [],
344+
},
345+
],
346+
"children": Array [],
347+
"data": Object {},
348+
"depth": 0,
349+
"key": "A",
350+
"nextSibling": "B",
351+
"parent": null,
352+
"prevSibling": null,
353+
"text": "A",
354+
"type": "ordered-list-item",
355+
},
356+
"B": Object {
357+
"characterList": Array [
358+
Object {
359+
"entity": null,
360+
"style": Array [],
361+
},
362+
],
363+
"children": Array [],
364+
"data": Object {},
365+
"depth": 0,
366+
"key": "B",
367+
"nextSibling": "C",
368+
"parent": null,
369+
"prevSibling": "A",
370+
"text": "B",
371+
"type": "ordered-list-item",
372+
},
373+
"C": Object {
374+
"characterList": Array [
375+
Object {
376+
"entity": null,
377+
"style": Array [],
378+
},
379+
],
380+
"children": Array [],
381+
"data": Object {},
382+
"depth": 0,
383+
"key": "C",
384+
"nextSibling": null,
385+
"parent": null,
386+
"prevSibling": "B",
387+
"text": "C",
388+
"type": "ordered-list-item",
389+
},
390+
}
391+
`;
392+
393+
exports[`ignore empty children array for tree conversion 2 1`] = `
394+
Object {
395+
"A": Object {
396+
"characterList": Array [
397+
Object {
398+
"entity": null,
399+
"style": Array [],
400+
},
401+
],
402+
"children": Array [],
403+
"data": Object {},
404+
"depth": 0,
405+
"key": "A",
406+
"nextSibling": "B",
407+
"parent": null,
408+
"prevSibling": null,
409+
"text": "A",
410+
"type": "ordered-list-item",
411+
},
412+
"B": Object {
413+
"characterList": Array [
414+
Object {
415+
"entity": null,
416+
"style": Array [],
417+
},
418+
],
419+
"children": Array [],
420+
"data": Object {},
421+
"depth": 0,
422+
"key": "B",
423+
"nextSibling": "C",
424+
"parent": null,
425+
"prevSibling": "A",
426+
"text": "B",
427+
"type": "ordered-list-item",
428+
},
429+
"C": Object {
430+
"characterList": Array [
431+
Object {
432+
"entity": null,
433+
"style": Array [],
434+
},
435+
],
436+
"children": Array [],
437+
"data": Object {},
438+
"depth": 0,
439+
"key": "C",
440+
"nextSibling": null,
441+
"parent": null,
442+
"prevSibling": "B",
443+
"text": "C",
444+
"type": "ordered-list-item",
445+
},
446+
}
447+
`;
448+
281449
exports[`must be able to convert content blocks that have list with depth from raw state to tree state when experimentalTreeDataSupport is enabled 1`] = `
282450
Object {
283451
"A": Object {

src/model/encoding/__tests__/convertFromRawToDraftState-test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,59 @@ test('must be able to convert content blocks that have list with depth from raw
207207

208208
assertDraftState(rawState);
209209
});
210+
211+
test('ignore empty children array', () => {
212+
const rawState = {
213+
blocks: [
214+
{key: 'A', type: 'ordered-list-item', depth: 0, text: 'A'},
215+
{key: 'B', type: 'ordered-list-item', depth: 0, text: 'B'},
216+
{
217+
key: 'C',
218+
type: 'ordered-list-item',
219+
depth: 0,
220+
text: 'C',
221+
children: [],
222+
},
223+
],
224+
entityMap: {},
225+
};
226+
227+
assertDraftState(rawState);
228+
});
229+
230+
test('ignore empty children array for tree conversion 1', () => {
231+
const rawState = {
232+
blocks: [
233+
{key: 'A', type: 'ordered-list-item', depth: 0, text: 'A'},
234+
{key: 'B', type: 'ordered-list-item', depth: 0, text: 'B'},
235+
{
236+
key: 'C',
237+
type: 'ordered-list-item',
238+
depth: 0,
239+
text: 'C',
240+
children: [],
241+
},
242+
],
243+
entityMap: {},
244+
};
245+
assertDraftState(rawState);
246+
});
247+
248+
test('ignore empty children array for tree conversion 2', () => {
249+
toggleExperimentalTreeDataSupport(true);
250+
const rawState = {
251+
blocks: [
252+
{key: 'A', type: 'ordered-list-item', depth: 0, text: 'A'},
253+
{key: 'B', type: 'ordered-list-item', depth: 0, text: 'B'},
254+
{
255+
key: 'C',
256+
type: 'ordered-list-item',
257+
depth: 0,
258+
text: 'C',
259+
children: [],
260+
},
261+
],
262+
entityMap: {},
263+
};
264+
assertDraftState(rawState);
265+
});

src/model/encoding/convertFromRawToDraftState.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ const decodeRawBlocks = (
215215
rawState: RawDraftContentState,
216216
entityMap: *,
217217
): BlockMap => {
218-
const isTreeRawBlock = Array.isArray(rawState.blocks[0].children);
218+
const isTreeRawBlock = rawState.blocks.find(
219+
block => Array.isArray(block.children) && block.children.length > 0,
220+
);
219221
const rawBlocks =
220222
experimentalTreeDataSupport && !isTreeRawBlock
221223
? DraftTreeAdapter.fromRawStateToRawTreeState(rawState).blocks

0 commit comments

Comments
 (0)