diff --git a/packages/rich-text/src/create.js b/packages/rich-text/src/create.js index aa7911bc9083b..fce6d5a55b202 100644 --- a/packages/rich-text/src/create.js +++ b/packages/rich-text/src/create.js @@ -401,14 +401,26 @@ function collapseWhiteSpace( element, isRoot = true ) { } /** - * Removes reserved characters used by rich-text (zero width non breaking spaces added by `toTree` and object replacement characters). + * We need to normalise line breaks to `\n` so they are consistent across + * platforms and serialised properly. Not removing \r would cause it to + * linger and result in double line breaks when whitespace is preserved. + */ +const CARRIAGE_RETURN = '\r'; + +/** + * Removes reserved characters used by rich-text (zero width non breaking spaces + * added by `toTree` and object replacement characters). * * @param {string} string */ export function removeReservedCharacters( string ) { - // with the global flag, note that we should create a new regex each time OR reset lastIndex state. + // with the global flag, note that we should create a new regex each time OR + // reset lastIndex state. return string.replace( - new RegExp( `[${ ZWNBSP }${ OBJECT_REPLACEMENT_CHARACTER }]`, 'gu' ), + new RegExp( + `[${ ZWNBSP }${ OBJECT_REPLACEMENT_CHARACTER }${ CARRIAGE_RETURN }]`, + 'gu' + ), '' ); }