From a4597c1e11d468aff272bc5aebaa6dedc1eb0a4a Mon Sep 17 00:00:00 2001 From: seonim-ryu Date: Tue, 26 Nov 2019 12:48:24 -0800 Subject: [PATCH] fix: preserve newlines in code block when pasting (fix #693) --- src/js/wwClipboardManager.js | 3 ++- test/unit/wwClipboardManager.spec.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/js/wwClipboardManager.js b/src/js/wwClipboardManager.js index fc96231984..754447ef6e 100644 --- a/src/js/wwClipboardManager.js +++ b/src/js/wwClipboardManager.js @@ -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, '
'); } }); diff --git a/test/unit/wwClipboardManager.spec.js b/test/unit/wwClipboardManager.spec.js index abb84b9f5d..d02c5a5ba1 100644 --- a/test/unit/wwClipboardManager.spec.js +++ b/test/unit/wwClipboardManager.spec.js @@ -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 = $('foo\nbar')[0]; + + cbm._replaceNewLineToBr(node); + + expect(node.innerHTML).toBe('foo
bar'); + }); + + it('preserve newlines in code block', () => { + const node = $('
foo\nbar
')[0]; + + cbm._replaceNewLineToBr(node); + + expect(node.innerHTML).toBe('foo\nbar'); + }); + }); }); });