This repository was archived by the owner on Feb 6, 2023. It is now read-only.
Commit c42662e
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- meta/bundle-size-stats
- src/model/immutable
3 files changed
+5
-2
lines changedLarge diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
78 | 81 | | |
79 | 82 | | |
80 | 83 | | |
| |||
0 commit comments