Skip to content

Commit

Permalink
Rich text: preserve white space should strip \r (#58805)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
  • Loading branch information
3 people authored Feb 8, 2024
1 parent 77fec36 commit 348018b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
),
''
);
}
Expand Down

0 comments on commit 348018b

Please sign in to comment.