Skip to content

Conversation

BugReportOnWeb
Copy link
Contributor

What?

Closes #59911

This PR ensures that newline characters (\n) in image captions, particularly when added via the Media Library are preserved and rendered correctly in both the editor and the frontend.

Why?

When an image caption is populated from the Media Library, it may include newline characters (\n). These appear as line breaks initially in the editor but are lost after a refresh, as \n gets converted to spaces. As a result, line breaks are not respected unless manually re-entered in the editor. confusing for users.

How?

Added a useEffect in the ImageEdit component to detect when the caption is a string containing \n, and replace those newline characters with <br /> before the data is passed to Caption component. This ensures consistent rendering by using proper HTML line breaks.

Testing Instructions

  1. Upload an image through Media Library and include a multi line caption (using Enter or Shift+Enter).
  2. Insert the image into a post using the Image block.
  3. Verify that the line breaks are respected in the editor.
  4. Refresh the editor page.
  5. Verify that the caption still retains line breaks in the editor.
  6. Save the post and view it on the frontend.
  7. Confirm that line breaks are correctly rendered on the frontend as well.

Screenshots or screencast

Before

Screen.Recording.2025-06-19.at.12.35.01.AM.1.mov

After

Screen.Recording.2025-06-19.at.7.25.16.PM.mov

@BugReportOnWeb BugReportOnWeb marked this pull request as ready for review June 19, 2025 14:47
Copy link

github-actions bot commented Jun 19, 2025

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @JulienGardair.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Unlinked contributors: JulienGardair.

Co-authored-by: BugReportOnWeb <devasheeshkaul@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: tellthemachines <isabel_brison@git.wordpress.org>

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

@shail-mehta shail-mehta added [Type] Bug An existing feature does not function as intended [Block] Image Affects the Image Block labels Jun 19, 2025
Copy link
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.

Thanks for working on this, @BugReportOnWeb!

Instead of adding an effect to normalize captions from the Media Library, I think we can do that in the onSelectImage method.

Comment on lines 140 to 147
useEffect( () => {
// Replacing `\n` with `<br />` ensures that line breaks are preserved,
// consistenctly in both the editor and the frontend.
if ( typeof caption === 'string' && caption.includes( '\n' ) ) {
const normalized = caption.replace( /\n/g, '<br />' );
setAttributes( { caption: normalized } );
}
}, [ caption, setAttributes ] );
Copy link
Member

Choose a reason for hiding this comment

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

A similar attribute update or synchronization isn't reliable and can introduce unexpected side effects. At a glance, this will introduce an undo trap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's interesting, I wasn't aware of the undo trap behavior before, Now that I've tested it, I can see the issue with unexpected undo steps after block insertion. I'll go ahead and make the changes accordingly.

@BugReportOnWeb BugReportOnWeb requested a review from Mamaduka July 2, 2025 11:05
) {
mediaAttributes.caption = mediaAttributes.caption.replace(
/\n/g,
'<br />'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'<br />'
'<br>'

Let's match the line break tags generated by RichText.

Screenshot

CleanShot 2025-07-02 at 15 26 35

@Mamaduka Mamaduka merged commit 90fe3bb into WordPress:trunk Jul 2, 2025
69 of 70 checks passed
@github-actions github-actions bot added this to the Gutenberg 21.2 milestone Jul 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Image Affects the Image Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Media Caption Line Break in media library
3 participants