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

Commit c21a9f7

Browse files
jankdcfacebook-github-bot
authored andcommitted
Fix bad destructuring when content block key has a - (#1995)
Summary: **Summary** If a content block was created with a key that contains the `-`, it would fail to decode the `decoratorKey` and the `leafKey` because the current implementation relies on `-` to be a parsing delimiter. Here's an example: ```js class SomeComponent extends React.Component { constructor(props) { super(props); const block = new ContentBlock({ type: 'paragraph', text: 'some-text', key: 'some-key' }); const contentState = ContentBlock.createFromBlockArray([block]); this.state = {editorState: EditorState.createWithContent(contentState)}; this.onChange = editorState => this.setState({editorState}); } render() { return ( <Editor editorState={this.state.editorState} onChange={this.onChange} /> ); } } ``` When you select anywhere on the editor, you'd get this error: ``` TypeError: Cannot read property 'getIn' of undefined 45 | const focusBlockKey = focusPath.blockKey; 46 | const focusLeaf = editorState > 47 | .getBlockTree(focusBlockKey) | ^ 48 | .getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]); 49 | 50 | const anchorLeafStart: number = anchorLeaf.get('start'); at getUpdatedSelectionState (src/component/selection/getUpdatedSelectionState.js:47:30) at getDraftEditorSelectionWithNodes (src/component/selection/getDraftEditorSelectionWithNodes.js:48:58) at getDraftEditorSelection (src/component/selection/getDraftEditorSelection.js:37:53) at assertGetDraftEditorSelection (src/component/selection/__tests__/getDraftEditorSelection-test.js:55:53) at Object.<anonymous> (src/component/selection/__tests__/getDraftEditorSelection-test.js:211:3) ``` **Test Plan** Added a unit test for `DraftOffsetKey.js` to check for all delimiter cases. Pull Request resolved: #1995 Differential Revision: D13982004 fbshipit-source-id: 3cd5967ad86041e310c41e7bcbfff4e868062804
1 parent a97ed7e commit c21a9f7

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/component/selection/DraftOffsetKey.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ const DraftOffsetKey = {
2525
},
2626

2727
decode: function(offsetKey: string): DraftOffsetKeyPath {
28-
const [blockKey, decoratorKey, leafKey] = offsetKey.split(KEY_DELIMITER);
28+
// Extracts the last two parts of offsetKey and captures the rest in blockKeyParts
29+
const [leafKey, decoratorKey, ...blockKeyParts] = offsetKey
30+
.split(KEY_DELIMITER)
31+
.reverse();
32+
2933
return {
30-
blockKey,
34+
// Recomposes the parts of blockKey after reversing them
35+
blockKey: blockKeyParts.reverse().join(KEY_DELIMITER),
3136
decoratorKey: parseInt(decoratorKey, 10),
3237
leafKey: parseInt(leafKey, 10),
3338
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails oncall+draft_js
8+
* @format
9+
* @flow strict-local
10+
*/
11+
12+
jest.disableAutomock();
13+
14+
const DraftOffsetKey = require('DraftOffsetKey');
15+
16+
test('decodes offset key with no delimiter', () => {
17+
expect(DraftOffsetKey.decode('key-0-1')).toMatchSnapshot();
18+
});
19+
20+
test('decodes offset key with delimiter in between', () => {
21+
expect(DraftOffsetKey.decode('key-with-delimiter-0-1')).toMatchSnapshot();
22+
});
23+
24+
test('decodes offset key with delimiter at the beginning', () => {
25+
expect(DraftOffsetKey.decode('-key-0-1')).toMatchSnapshot();
26+
});
27+
28+
test('decodes offset key with delimiter at the end', () => {
29+
expect(DraftOffsetKey.decode('key--0-1')).toMatchSnapshot();
30+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`decodes offset key with delimiter at the beginning 1`] = `
4+
Object {
5+
"blockKey": "-key",
6+
"decoratorKey": 0,
7+
"leafKey": 1,
8+
}
9+
`;
10+
11+
exports[`decodes offset key with delimiter at the end 1`] = `
12+
Object {
13+
"blockKey": "key-",
14+
"decoratorKey": 0,
15+
"leafKey": 1,
16+
}
17+
`;
18+
19+
exports[`decodes offset key with delimiter in between 1`] = `
20+
Object {
21+
"blockKey": "key-with-delimiter",
22+
"decoratorKey": 0,
23+
"leafKey": 1,
24+
}
25+
`;
26+
27+
exports[`decodes offset key with no delimiter 1`] = `
28+
Object {
29+
"blockKey": "key",
30+
"decoratorKey": 0,
31+
"leafKey": 1,
32+
}
33+
`;

0 commit comments

Comments
 (0)