-
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: remove preserveWhiteSpace serialisation differences #55999
Conversation
let valueB = create( { | ||
html: htmlB, | ||
...mapRichTextSettings( attributeDefinitionB ), | ||
} ); |
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.
Like I said, a pain to work with.
* @param {string} string | ||
*/ | ||
function collapseWhiteSpace( string ) { | ||
return string.replace( /[\n\r\t]+/g, ' ' ); |
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.
This is sort of replaced by logic in the paste handler
let newData = node.data.replace( /[ \r\n\t]+/g, ' ' ); |
Size Change: -229 B (0%) Total Size: 1.7 MB
ℹ️ View Unchanged
|
2cd0b0f
to
e11bc4c
Compare
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've been testing this with the Playground and can't find a way to break it.
I explored variations of whitepspace and conversion to and from a paragraph, code block, HTML block.
The only thing that stood out to me was that my spaces were preserved inside the editor for the paragraph but obviously didn't remain in the rendered post. I would have expected successive spaces in the editor (for example, typing this alignment
) would produce HTML that preserves those spaces, such as with a non-breaking space. Since this happens in trunk
it's not a blocker or even a comment about this change specifically.
While reviewing I did wonder what our reason has been for removing whitespace at all. If we don't preserve the intent or entered text from the editor to the rendered page, why modify it at all? Exploring the DOM inside the browser revealed that the browser doesn't modify textContent
to remove extra whitespace, and I couldn't find any way to make it do it, even with calls to .normalize()
and .cloneNode()
. In other words, if browsers handle the whitespace and the editor has is own rules, what are we trying to accomplish by messing with whitespace?
That's because we have white space: pre-wrap turned on for all rich text instances. I'll try to remove it in 587dede. |
Flaky tests detected in 2ef1e3a. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6826391569
|
587dede
to
0c26493
Compare
FYI: This change apparently broke my Syntax-highlighting Code Block plugin, since the I have a workaround to replace all |
@westonruter Worth nothing that the RichText and the code block can contain any HTML formatting. If you create a custom block and don't want formatting, you should ideally use the PlainText component (v2). |
@ellatrix Thanks. Good point. However, I'm hoping to retain the ability to do HTML formatting and have syntax highlighting (westonruter/syntax-highlighting-code-block#205), likely facilitated with the HTML Tag Processor. If I fail at that, I'll go with what you suggest. |
@westonruter are you looking to render the contents of the PRE element verbatim as code? if so, I'm not sure that's a reliable method anyway. the PRE element doesn't add any semantic to its contents other than that they should be monospace-rendered. have you looked at storing the contents in a disabled TEXTAREA? or have you tried first decoding the contents on the server? if we interpret the PRE contents appropriately I wonder if the problem would persist? that's now doable with the HTML API in 6.5. does the syntax highlighter have the same struggles with encoded characters? |
What?
This PR adjusts the
preserveWhiteSpace
prop from rich text, which was being used for the Preformatted, Code, and Verse blocks. We don't have to continuously collapse whitespace inside rich text on value changes, and we don't need it for serialisation. Additionally, it's no longer needed for splitting/merging all instances serialise the same way.Why?
multiline
, which has recently been removed) is a pain to work with. Currently, to parse and serialised rich text content (withcreate
andtoHTMLString
), you need to know where this HTML came from (an instance that preserves white space or not). This required us to add a special setting to the block attribute definition, so we can properly handle merging and splitting. Now all rich text values serialise the same way, with line breaks always serialising to the HTML BR tag.How?
As said before, we can rely on the initial HTML parse and the paste handler to remove superfluous white space. The logic in the paste handler was introduces in #17470, much after we added this
preserveWhiteSpace
prop to rich text.Additionally, for line break serialisation, it should be safe to use proper line break elements for rich text instances. It's worth noting that Preformatted, Code, and Verse use rich text components, allowing HTML formatting like bold, links, etc., so line break HTML elements are not odd. If we don't want any HTML elements, we should use the plain text component instead, which outputs line breaks as simple text line breaks.
Testing Instructions
We have lots of e2e test coverage, so we should be good. Mainly check the Preformatted, Code, and Verse blocks.
Testing Instructions for Keyboard
Screenshots or screencast