-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 multiple spaces on front-end #56280
Conversation
Size Change: +130 B (0%) Total Size: 1.7 MB
ℹ️ View Unchanged
|
|
||
// When inserting multiple spaces, alternate between a normal space | ||
// and a non-breaking space. This is to prevent browsers from | ||
// collapsing multiple spaces into one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yikes. this seems good, but dang - so hard to make an editor in the browser!
Array.from( { length: endOffset - startOffset } ) | ||
.map( ( _, index ) => ( index % 2 === 0 ? SP : NBSP ) ) | ||
.join( '' ) + | ||
text.slice( endOffset ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it make sense here to use a regular expression to handle this?
// Replace all successive spaces with an alternating pattern of space/nbsp.
newValue.text = newValue.replace( /[ \u00a0]+/g, match => ''.padStart( match.length, ' \u00a0' ) );
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to be replacing all spaces. We shouldn't modify existing content too much imo, only the spaces around the selection.
Fixed by #56341 instead |
What?
This PR fixes a long standing issue in RichText where multiple inserted spaces are not preserved on the front-end. They are collapsed into a single space.
Why?
What you see in the editor should be the same as the front-end. It's unexpected that spaces on the front-end are collapsed into a single space, after you explicitly inserted multiple.
How?
When inserting multiple space, we should alternate between normal spaces and non breaking spaces. This is the default behaviour in all browsers, but it's turned off in Gutenberg because we have
white-space: pre-wrap
turned on for all rich text instances. We intentionally enabled this for stability, one of the reasons being that browsers insert the non breaking spaces a bit too much. For example, if you type two spaces, and remove one, you shouldn't be left with a non breaking space.Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast