diff --git a/src/model/encoding/__tests__/__snapshots__/convertFromRawToDraftState-test.js.snap b/src/model/encoding/__tests__/__snapshots__/convertFromRawToDraftState-test.js.snap index 46fc8c273f..afc25b8a64 100644 --- a/src/model/encoding/__tests__/__snapshots__/convertFromRawToDraftState-test.js.snap +++ b/src/model/encoding/__tests__/__snapshots__/convertFromRawToDraftState-test.js.snap @@ -278,6 +278,174 @@ Object { } `; +exports[`ignore empty children array 1`] = ` +Object { + "A": Object { + "characterList": Array [ + Object { + "entity": null, + "style": Array [], + }, + ], + "children": Array [], + "data": Object {}, + "depth": 0, + "key": "A", + "nextSibling": "B", + "parent": null, + "prevSibling": null, + "text": "A", + "type": "ordered-list-item", + }, + "B": Object { + "characterList": Array [ + Object { + "entity": null, + "style": Array [], + }, + ], + "children": Array [], + "data": Object {}, + "depth": 0, + "key": "B", + "nextSibling": "C", + "parent": null, + "prevSibling": "A", + "text": "B", + "type": "ordered-list-item", + }, + "C": Object { + "characterList": Array [ + Object { + "entity": null, + "style": Array [], + }, + ], + "children": Array [], + "data": Object {}, + "depth": 0, + "key": "C", + "nextSibling": null, + "parent": null, + "prevSibling": "B", + "text": "C", + "type": "ordered-list-item", + }, +} +`; + +exports[`ignore empty children array for tree conversion 1 1`] = ` +Object { + "A": Object { + "characterList": Array [ + Object { + "entity": null, + "style": Array [], + }, + ], + "children": Array [], + "data": Object {}, + "depth": 0, + "key": "A", + "nextSibling": "B", + "parent": null, + "prevSibling": null, + "text": "A", + "type": "ordered-list-item", + }, + "B": Object { + "characterList": Array [ + Object { + "entity": null, + "style": Array [], + }, + ], + "children": Array [], + "data": Object {}, + "depth": 0, + "key": "B", + "nextSibling": "C", + "parent": null, + "prevSibling": "A", + "text": "B", + "type": "ordered-list-item", + }, + "C": Object { + "characterList": Array [ + Object { + "entity": null, + "style": Array [], + }, + ], + "children": Array [], + "data": Object {}, + "depth": 0, + "key": "C", + "nextSibling": null, + "parent": null, + "prevSibling": "B", + "text": "C", + "type": "ordered-list-item", + }, +} +`; + +exports[`ignore empty children array for tree conversion 2 1`] = ` +Object { + "A": Object { + "characterList": Array [ + Object { + "entity": null, + "style": Array [], + }, + ], + "children": Array [], + "data": Object {}, + "depth": 0, + "key": "A", + "nextSibling": "B", + "parent": null, + "prevSibling": null, + "text": "A", + "type": "ordered-list-item", + }, + "B": Object { + "characterList": Array [ + Object { + "entity": null, + "style": Array [], + }, + ], + "children": Array [], + "data": Object {}, + "depth": 0, + "key": "B", + "nextSibling": "C", + "parent": null, + "prevSibling": "A", + "text": "B", + "type": "ordered-list-item", + }, + "C": Object { + "characterList": Array [ + Object { + "entity": null, + "style": Array [], + }, + ], + "children": Array [], + "data": Object {}, + "depth": 0, + "key": "C", + "nextSibling": null, + "parent": null, + "prevSibling": "B", + "text": "C", + "type": "ordered-list-item", + }, +} +`; + exports[`must be able to convert content blocks that have list with depth from raw state to tree state when experimentalTreeDataSupport is enabled 1`] = ` Object { "A": Object { diff --git a/src/model/encoding/__tests__/convertFromRawToDraftState-test.js b/src/model/encoding/__tests__/convertFromRawToDraftState-test.js index d43bc698a8..231c1b7000 100644 --- a/src/model/encoding/__tests__/convertFromRawToDraftState-test.js +++ b/src/model/encoding/__tests__/convertFromRawToDraftState-test.js @@ -207,3 +207,59 @@ test('must be able to convert content blocks that have list with depth from raw assertDraftState(rawState); }); + +test('ignore empty children array', () => { + const rawState = { + blocks: [ + {key: 'A', type: 'ordered-list-item', depth: 0, text: 'A'}, + {key: 'B', type: 'ordered-list-item', depth: 0, text: 'B'}, + { + key: 'C', + type: 'ordered-list-item', + depth: 0, + text: 'C', + children: [], + }, + ], + entityMap: {}, + }; + + assertDraftState(rawState); +}); + +test('ignore empty children array for tree conversion 1', () => { + const rawState = { + blocks: [ + {key: 'A', type: 'ordered-list-item', depth: 0, text: 'A'}, + {key: 'B', type: 'ordered-list-item', depth: 0, text: 'B'}, + { + key: 'C', + type: 'ordered-list-item', + depth: 0, + text: 'C', + children: [], + }, + ], + entityMap: {}, + }; + assertDraftState(rawState); +}); + +test('ignore empty children array for tree conversion 2', () => { + toggleExperimentalTreeDataSupport(true); + const rawState = { + blocks: [ + {key: 'A', type: 'ordered-list-item', depth: 0, text: 'A'}, + {key: 'B', type: 'ordered-list-item', depth: 0, text: 'B'}, + { + key: 'C', + type: 'ordered-list-item', + depth: 0, + text: 'C', + children: [], + }, + ], + entityMap: {}, + }; + assertDraftState(rawState); +}); diff --git a/src/model/encoding/convertFromRawToDraftState.js b/src/model/encoding/convertFromRawToDraftState.js index 0bd9763fd4..b0ca0c1970 100644 --- a/src/model/encoding/convertFromRawToDraftState.js +++ b/src/model/encoding/convertFromRawToDraftState.js @@ -215,7 +215,9 @@ const decodeRawBlocks = ( rawState: RawDraftContentState, entityMap: *, ): BlockMap => { - const isTreeRawBlock = Array.isArray(rawState.blocks[0].children); + const isTreeRawBlock = rawState.blocks.find( + block => Array.isArray(block.children) && block.children.length > 0, + ); const rawBlocks = experimentalTreeDataSupport && !isTreeRawBlock ? DraftTreeAdapter.fromRawStateToRawTreeState(rawState).blocks