Do the less invasive fix of replacing `<br>\n` with `\n` but only within `<pre>`

pull/21833/head
Luke Barnard 2017-06-28 14:27:24 +01:00
parent f73fa4b49b
commit eeb1c33868
1 changed files with 8 additions and 5 deletions

View File

@ -85,11 +85,6 @@ export function charactersToImageNode(alt, useSvg, ...unicode) {
export function processHtmlForSending(html: string): string { export function processHtmlForSending(html: string): string {
// Replace "<br>\n" with "<br>" because the \n is redundant and causes an
// extra newline per line within `<pre>` 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(/\<br\>\n/g, '<br>');
const contentDiv = document.createElement('div'); const contentDiv = document.createElement('div');
contentDiv.innerHTML = html; contentDiv.innerHTML = html;
@ -103,6 +98,14 @@ export function processHtmlForSending(html: string): string {
const element = contentDiv.children[i]; const element = contentDiv.children[i];
if (element.tagName.toLowerCase() === 'p') { if (element.tagName.toLowerCase() === 'p') {
contentHTML += element.innerHTML + '<br />'; contentHTML += element.innerHTML + '<br />';
} else if (element.tagName.toLowerCase() === 'pre') {
// Replace "<br>\n" with "<br>" because the \n is redundant and causes an
// extra newline per line within `<pre>` tags.
// This is a workaround for a bug in draft-js-export-html:
// https://github.com/sstur/draft-js-export-html/issues/62
contentHTML += '<pre>' +
element.innerHTML.replace(/<br>\n/g, '\n').trim() +
'</pre>';
} else { } else {
const temp = document.createElement('div'); const temp = document.createElement('div');
temp.appendChild(element.cloneNode(true)); temp.appendChild(element.cloneNode(true));