From 69589c19e7f822749f5a1664f63ba74b2772a2fc Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 27 Jun 2017 18:33:45 +0100 Subject: [PATCH 1/4] Work around draft-js-export-html#62 by post-processing
\n Fixes https://github.com/vector-im/riot-web/issues/4446 by post-processing the output HTML from draft-js-export-html by replacing `
\n` with `
`. This works for content within or outside of `
`. If we replace with `\n` instead, the newlines only apply in `
` tags so we use `
`. --- src/components/views/rooms/MessageComposerInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index b05beb2571..cca0a9899c 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -509,7 +509,7 @@ export default class MessageComposerInput extends React.Component { if (this.state.isRichtextEnabled) { contentHTML = HtmlUtils.stripParagraphs( RichText.contentStateToHTML(contentState), - ); + ).replace(/\\n/g, '
'); } else { const md = new Markdown(contentText); if (md.isPlainText()) { From f73fa4b49b0eb6709d6784e1ea32eb406d42fb3f Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 28 Jun 2017 11:49:50 +0100 Subject: [PATCH 2/4] Move processing into renamed function processHtmlforSending And explain why this fix is necessary --- src/HtmlUtils.js | 8 +++++++- src/components/views/rooms/MessageComposerInput.js | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index a32d05e4ff..cc302e24e5 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -84,7 +84,13 @@ export function charactersToImageNode(alt, useSvg, ...unicode) { } -export function stripParagraphs(html: string): string { +export function processHtmlForSending(html: string): string { + // Replace "
\n" with "
" because the \n is redundant and causes an + // extra newline per line within `
` tags.
+    // This is a workaround for a bug in draft-js-export-html:
+    //   https://github.com/sstur/draft-js-export-html/issues/62
+    html = html.replace(/\\n/g, '
'); + const contentDiv = document.createElement('div'); contentDiv.innerHTML = html; diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index cca0a9899c..aa6f3f2ac5 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -507,9 +507,9 @@ export default class MessageComposerInput extends React.Component { } if (this.state.isRichtextEnabled) { - contentHTML = HtmlUtils.stripParagraphs( + contentHTML = HtmlUtils.processHtmlForSending( RichText.contentStateToHTML(contentState), - ).replace(/\\n/g, '
'); + ); } else { const md = new Markdown(contentText); if (md.isPlainText()) { From eeb1c3386827f24ef2484eb665b05000f0957852 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 28 Jun 2017 14:27:24 +0100 Subject: [PATCH 3/4] Do the less invasive fix of replacing `
\n` with `\n` but only within `
`

---
 src/HtmlUtils.js | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js
index cc302e24e5..13b3f3094f 100644
--- a/src/HtmlUtils.js
+++ b/src/HtmlUtils.js
@@ -85,11 +85,6 @@ export function charactersToImageNode(alt, useSvg, ...unicode) {
 
 
 export function processHtmlForSending(html: string): string {
-    // Replace "
\n" with "
" because the \n is redundant and causes an - // extra newline per line within `
` tags.
-    // This is a workaround for a bug in draft-js-export-html:
-    //   https://github.com/sstur/draft-js-export-html/issues/62
-    html = html.replace(/\\n/g, '
'); const contentDiv = document.createElement('div'); contentDiv.innerHTML = html; @@ -103,6 +98,14 @@ export function processHtmlForSending(html: string): string { const element = contentDiv.children[i]; if (element.tagName.toLowerCase() === 'p') { contentHTML += element.innerHTML + '
'; + } else if (element.tagName.toLowerCase() === 'pre') { + // Replace "
\n" with "
" because the \n is redundant and causes an + // extra newline per line within `
` tags.
+            // This is a workaround for a bug in draft-js-export-html:
+            //   https://github.com/sstur/draft-js-export-html/issues/62
+            contentHTML += '
' +
+                element.innerHTML.replace(/
\n/g, '\n').trim() + + '
'; } else { const temp = document.createElement('div'); temp.appendChild(element.cloneNode(true)); From 9b24f70d0052dd763e23848e7f3b307e1d24172c Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 28 Jun 2017 14:29:53 +0100 Subject: [PATCH 4/4] Update comment --- src/HtmlUtils.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index 13b3f3094f..ee679391cc 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -99,9 +99,8 @@ export function processHtmlForSending(html: string): string { if (element.tagName.toLowerCase() === 'p') { contentHTML += element.innerHTML + '
'; } else if (element.tagName.toLowerCase() === 'pre') { - // Replace "
\n" with "
" because the \n is redundant and causes an - // extra newline per line within `
` tags.
-            // This is a workaround for a bug in draft-js-export-html:
+            // Replace "
\n" with "\n" within `
` tags because the 
is + // redundant. This is a workaround for a bug in draft-js-export-html: // https://github.com/sstur/draft-js-export-html/issues/62 contentHTML += '
' +
                 element.innerHTML.replace(/
\n/g, '\n').trim() +