Skip to content

Commit b9d47b1

Browse files
author
Rose Robertson
committed
Fix bug when user pastes non-string
I don’t know how people manage to get non-strings into their clipboard but I was seeing a bunch of errors in sentry "length of undefined" and tracked it down to this plugin. To re-create on mac 1. Open terminal, execute following command: `pbcopy < /dev/null` 2. Paste in a draft editor that uses this plugin 3. You will see an error in your console Fix is to early-return if `text` has no value, instead of trying to loop through it.
1 parent 53b91a0 commit b9d47b1

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/__test__/plugin-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
379379
expect(subject()).to.equal('not-handled');
380380
});
381381
});
382+
describe('non-string empty value in clipboard', () => {
383+
beforeEach(() => {
384+
pastedText = null;
385+
});
386+
it('returns not-handled', () => {
387+
expect(subject()).to.equal('not-handled');
388+
});
389+
});
382390
describe('pasted just text', () => {
383391
beforeEach(() => {
384392
pastedText = 'hello';

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ const createMarkdownShortcutsPlugin = (config = { insertEmptyBlockOnReturnWithMo
149149
if (html) {
150150
return 'not-handled';
151151
}
152+
153+
if (!text) {
154+
return 'not-handled';
155+
}
156+
152157
let newEditorState = editorState;
153158
let buffer = [];
154159
for (let i = 0; i < text.length; i++) { // eslint-disable-line no-plusplus

0 commit comments

Comments
 (0)