Skip to content

Commit

Permalink
Fix error when pasting HTML that parses to a blank doc
Browse files Browse the repository at this point in the history
closes #619
- it's possible to paste HTML that parses into a blank doc, if the paste results in a section being replaced then the `post-inserter` would throw an error because it expects the new post to have sections
- adds a guard in the paste event to avoid inserting a blank post
  • Loading branch information
kevinansfield committed May 21, 2018
1 parent e363b9b commit ba81444
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/js/editor/event-manager.js
Expand Up @@ -251,10 +251,12 @@ export default class EventManager {
let targetFormat = this.modifierKeys.shift ? 'text' : 'html';
let pastedPost = parsePostFromPaste(event, editor, {targetFormat});

editor.run(postEditor => {
let nextPosition = postEditor.insertPost(position, pastedPost);
postEditor.setRange(nextPosition);
});
if (!pastedPost.isBlank) {
editor.run(postEditor => {
let nextPosition = postEditor.insertPost(position, pastedPost);
postEditor.setRange(nextPosition);
});
}
}

drop(event) {
Expand Down
22 changes: 22 additions & 0 deletions tests/acceptance/editor-copy-paste-test.js
Expand Up @@ -412,3 +412,25 @@ test('paste with shift key pastes plain text', (assert) => {

assert.postIsSimilar(editor.post, expected);
});

test('paste with html that parses to blank doc doesn\'t error', (assert) => {
let expected;
let mobiledoc = Helpers.mobiledoc.build(({post, markupSection, marker}) => {
expected = post([
markupSection('p', [])
]);

return post([
markupSection('p', [marker('abcd')])
]);
});

editor = new Editor({mobiledoc, cards});
editor.render(editorElement);

Helpers.dom.setCopyData('text/html', `<div></div>`);
editor.selectRange(editor.post.toRange());
Helpers.dom.triggerPasteEvent(editor);

assert.postIsSimilar(editor.post, expected);
});

0 comments on commit ba81444

Please sign in to comment.