From f50b3f365db1dbb14701a0ceac1250f1e9c0cec3 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Mon, 5 Mar 2018 22:02:58 +0100 Subject: [PATCH] Prevent unintended initial empty line --- .../client/utils.js | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/plugins/talk-plugin-rich-text-pell/client/utils.js b/plugins/talk-plugin-rich-text-pell/client/utils.js index bb71c2b4f..6155fbfb2 100644 --- a/plugins/talk-plugin-rich-text-pell/client/utils.js +++ b/plugins/talk-plugin-rich-text-pell/client/utils.js @@ -1,11 +1,20 @@ export function htmlNormalizer(htmlInput) { - const str = htmlInput; + let str = htmlInput; // We are normalizing the input from contenteditable of each browser, also removing unnecesary html tags // https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content#Differences_in_markup_generation - console.log(htmlInput); - return str - .replace(/

/g, '
') - .replace(/<\/p>/g, '') - .replace(/

/g, '
') - .replace(/<\/div>/g, ''); + + // Old browsers uses `p` normalize to `div` instead. + str = str + .replace(/

/g, '

') // IE and old browsers outputs

instead of

s + .replace(/<\/p>/g, '
'); // IE and old browsers outputs

instead of

s + + // Remove first opening tag, otherwise + // with the following transformation below + // we might add an unintended first empty line. + if (str.startsWith('
')) { + str = str.replace('
', ''); + } + + // Normalize
s to
. + return str.replace(/
/g, '
').replace(/<\/div>/g, ''); }