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

Commit c42662e

Browse files
davoscriptfacebook-github-bot
authored andcommitted
Defaulting to createEmpty if block map is empty in createWithContent (Fixes issue #2226) (#2240)
Summary: **Summary** When passing `createWithContent` a `state` parameter created using `convertFromHTML` from an empty string and `createFromBlockArray`, as shown in the example: ``` const sampleMarkup = '<b>Bold text</b>, <i>Italic text</i><br/ ><br />' + '<a href="http://www.facebook.com">Example link</a><br /><br/ >' + '<img src="image.png" height="112" width="200" />'; const blocksFromHTML = convertFromHTML(sampleMarkup); const state = ContentState.createFromBlockArray( blocksFromHTML.contentBlocks, blocksFromHTML.entityMap, ); this.state = { editorState: EditorState.createWithContent( state, decorator, ), }; ``` The following error is thrown: ``` TypeError: Cannot read property 'getKey' of undefined 84 | EditorState.createWithContent = function createWithContent(contentState, decorator) { > 85 | var firstKey = contentState.getBlockMap().first().getKey(); | ^ 86 | return EditorState.create({ 87 | currentContent: contentState, 88 | undoStack: Stack(), ``` The previous error is generated because `createWithContent` asumes that `contentState.getBlockMap().first()` will return an element, which is wrong for scenarios where the block map is empty. As a consecuence the `getKey();` function in `contentState.getBlockMap().first().getKey();` is executed on `undefined`, thus throwing the error. The previous scenario is very common if you are planning to use the editor to allow a user to write HTML content which could also be blank, especially as a default value. **Solution** In order to make the less amount of modifications to the code, I've added a validation that will run `createEmpty` in case the block map is empty. **Test Plan** Open `/draft-js/examples/draft-0-10-0/convertFromHTML/convert.html` and change line #61 from: `const blocksFromHTML = convertFromHTML(sampleMarkup);` to: `const blocksFromHTML = convertFromHTML('');` No error will be shown. Pull Request resolved: #2240 Differential Revision: D18247644 Pulled By: mrkev fbshipit-source-id: 3eb90fd5379e8a2d85efbb2b7b9587ca87701d12
1 parent 2a761af commit c42662e

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

meta/bundle-size-stats/Draft.js.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

meta/bundle-size-stats/Draft.min.js.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/model/immutable/EditorState.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class EditorState {
7575
contentState: ContentState,
7676
decorator?: ?DraftDecoratorType,
7777
): EditorState {
78+
if (contentState.getBlockMap().count() === 0) {
79+
return EditorState.createEmpty(decorator);
80+
}
7881
const firstKey = contentState
7982
.getBlockMap()
8083
.first()

0 commit comments

Comments
 (0)