Skip to content

Commit

Permalink
fix: preserve newlines in code block when pasting (fix #693)
Browse files Browse the repository at this point in the history
  • Loading branch information
seonim-ryu committed Nov 26, 2019
1 parent 4be7ee2 commit a4597c1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/js/wwClipboardManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class WwClipboardManager {
const textNodes = domUtils.getAllTextNode(node);

textNodes.forEach((textNode) => {
if (/\n/.test(textNode.nodeValue)) {
if (domUtils.getNodeName(textNode.parentNode) === 'SPAN' &&
/\n/.test(textNode.nodeValue)) {
textNode.parentNode.innerHTML = textNode.nodeValue.replace(/\n/g, '<br>');
}
});
Expand Down
18 changes: 18 additions & 0 deletions test/unit/wwClipboardManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,23 @@ describe('WwClipboardManager', () => {
expect(range.startContainer.nodeType === Node.TEXT_NODE).toBe(true);
expect(range.endContainer.nodeType === Node.TEXT_NODE).toBe(true);
});

describe('_replaceNewLineToBr', () => {
it('replace newlines to br in plain text only', () => {
const node = $('<span>foo\nbar</span>')[0];

cbm._replaceNewLineToBr(node);

expect(node.innerHTML).toBe('foo<br>bar');
});

it('preserve newlines in code block', () => {
const node = $('<pre>foo\nbar</pre>')[0];

cbm._replaceNewLineToBr(node);

expect(node.innerHTML).toBe('foo\nbar');
});
});
});
});

0 comments on commit a4597c1

Please sign in to comment.