Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich text: preserve white space should strip \r #58805

Merged
merged 2 commits into from Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/rich-text/src/create.js
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