Skip to content

Fix overflow of Highlighted white-space in Code Block#77085

Merged
Mamaduka merged 4 commits intoWordPress:trunkfrom
coderGtm:fix/code-block-overflow
Apr 8, 2026
Merged

Fix overflow of Highlighted white-space in Code Block#77085
Mamaduka merged 4 commits intoWordPress:trunkfrom
coderGtm:fix/code-block-overflow

Conversation

@coderGtm
Copy link
Copy Markdown
Contributor

@coderGtm coderGtm commented Apr 7, 2026

What?

Closes #69668

Fixes the highlight issue for the frontend but does not seem to be working for editor.

Why?

Currently, when text within a code block is highlighted and includes extra white space, it can overflow beyond the code box, which is not ideal. While this issue is uncommon, it negatively affects the overall UI experience when it does happen.

How?

The PR introduces a variable called whiteSpaceStyle that determined the value of the white-space property of the component style. It uses the modern break-spaces value to fix the issue on browsers that support this value and uses the older pre-wrap on incompatible browsers.

Testing Instructions

  1. Create a new Post
  2. Insert a code block with extra white-spaces at the end of 1 line.
  3. Highlight the code's background from the Block Controls Toolbar.
  4. Verify that the highlight does not overflow the code block in the frontend.

Screenshots

Before After
Screenshot 2025-06-03 at 5 46 06 PM Screenshot 2025-06-03 at 5 46 16 PM

Note

This PR is the same as #70301 but since it was based from trunk, there were many issues. Hence this new PR is created for a cleaner workflow.

Thanks

@github-actions github-actions bot added the [Package] Block library /packages/block-library label Apr 7, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: coderGtm <gautam23@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: Rishit30G <rishit30g@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka Mamaduka added [Type] Bug An existing feature does not function as intended [Block] Code Affects the Code Block labels Apr 7, 2026
@Mamaduka Mamaduka self-requested a review April 7, 2026 06:27
@Mamaduka
Copy link
Copy Markdown
Member

Mamaduka commented Apr 7, 2026

@coderGtm, it looks like prettier is triggering some errors for changes. See - https://github.com/WordPress/gutenberg/pull/77085/changes.

@Mamaduka
Copy link
Copy Markdown
Member

Mamaduka commented Apr 7, 2026

Thanks for creating a new PR, @coderGtm!

I just retested, and this doesn't seem to fix the problem. The support is correctly detected, but RichText ignores styles override and still applies pre-wrap.

The pre-wrap styling is coming from the useDefaultStyle hook, which is triggered later and overrides the custom whiteSpace.

Does this fix work in your testing?

Screenshot

CleanShot 2026-04-07 at 11 45 32

Copy link
Copy Markdown
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pre-wrap styling is coming from the useDefaultStyle hook, which is triggered later and overrides the custom whiteSpace.

I have confirmed the same behavior. Should we prioritize the style prop?

diff --git a/packages/rich-text/src/hook/use-default-style.js b/packages/rich-text/src/hook/use-default-style.js
index 5d45d027034..f09bd42b783 100644
--- a/packages/rich-text/src/hook/use-default-style.js
+++ b/packages/rich-text/src/hook/use-default-style.js
@@ -30,6 +30,6 @@ export function useDefaultStyle() {
                if ( ! element ) {
                        return;
                }
-               element.style.whiteSpace = whiteSpace;
+               element.style.whiteSpace = element.style.whiteSpace || whiteSpace;
        }, [] );
 }

Comment thread packages/block-library/src/code/edit.js Outdated
Comment on lines +8 to +13
const SUPPORTS_BREAK_SPACES =
typeof CSS !== 'undefined' &&
// eslint-disable-next-line no-undef
CSS.supports &&
// eslint-disable-next-line no-undef
CSS.supports( 'white-space', 'break-spaces' );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand this check is based on #70301 (comment), but since break-sopaces has been supported across all browsers for several years, I believe this check can be removed. What do you think?

https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/white-space#browser_compatibility

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. The 97% support should be good - https://caniuse.com/?search=white-space.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so should I remove it then to keep only break-spaces?

Comment on lines +18 to +20
@supports (white-space: break-spaces) {
white-space: break-spaces;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@supports (white-space: break-spaces) {
white-space: break-spaces;
}
white-space: break-spaces;

This can also be simplified

@coderGtm coderGtm requested a review from ellatrix as a code owner April 7, 2026 12:38
@github-actions github-actions bot added the [Package] Rich text /packages/rich-text label Apr 7, 2026
@coderGtm
Copy link
Copy Markdown
Contributor Author

coderGtm commented Apr 7, 2026

Hello @Mamaduka and @t-hamano

Firstly, thanks for testing it @Mamaduka, and yes, the initial commit did not solve the editor side problem as it was an inline styling and I was not sure how to override it without using !important.

Secondly, thanks to @t-hamano. Your suggested fix for overriding the default inline styling is superb and it completes the patch for the editor side also.

So now this PR makes the code block behaviour as expected for both frontend and the editor.

One thing I am concerned of is will the changes made to the useDefaultStyle() function negatively affect any other component. I am 99% sure it won't since the element.style.whiteSpace = element.style.whiteSpace || whiteSpace; will only take the element's white-space style if it is defined but still wanted to know your opinion.

@Mamaduka
Copy link
Copy Markdown
Member

Mamaduka commented Apr 7, 2026

element.style.whiteSpace = element.style.whiteSpace || whiteSpace;

This should be okay. If the consumer wants to override, then we should allow it, IMO. @ellatrix, what do you think?

Copy link
Copy Markdown
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as expected; the new white-space behavior was also approved in the old #70301 (comment).

I'm going to merge this.

@Mamaduka Mamaduka merged commit e28168c into WordPress:trunk Apr 8, 2026
41 checks passed
@github-actions github-actions bot added this to the Gutenberg 23.0 milestone Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Code Affects the Code Block [Package] Block library /packages/block-library [Package] Rich text /packages/rich-text [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code Block: Highlighted white space overflows

3 participants