gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Media Caption Line Break in media library

Open JulienGardair opened this issue 2 years ago • 2 comments

Description

Media Library>Image>Caption Line Breaks are reflected in the editor >image > caption but not in the front end.

Step-by-step reproduction instructions

When line breaks are created in Media library>Image>Caption they are reflected in the BLOCK IMAGE caption in the editor but not in the front end where they appear all in one line. If you add the line breaks in the caption, in the editor, then they are reflected in the front end. I am talking about just enter or shift+enter not

Screenshots, screen recording, code snippet

No response

Environment info

WP 6.4.3 Gutenberg 17.9.0

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

JulienGardair avatar Mar 15 '24 13:03 JulienGardair

I can reproduce this bug.

Edit: when the line break is made in the media library with Enter or Shift+Enter, it's expressed as a newline \n instead of a <br>. In the editor, the \n gets translated to <br>, but not in the front end. Looks like that needs to be fixed.

tellthemachines avatar Apr 04 '24 03:04 tellthemachines

Tested this on the latest version (WordPress 6.9-alpha-60275, Gutenberg 21.0.0) and it seems like the newline characters are not persisted in the editor either. They appear only when the image block is inserted initially, but after refreshing the page, all \n characters are replaced with spaces.

https://github.com/user-attachments/assets/a8c33c80-7abf-40c4-bc61-5af14ed1e638

This likely happens because of how raw strings are converted into RichTextData. During that process, newline characters in the original HTML are replaced.

https://github.com/WordPress/gutenberg/blob/eb82aa70c00768f5f64770cc702fd3735cc03878/packages/rich-text/src/create.js#L393-L395

As @tellthemachines pointed out, converting \n in the caption string to <br /> before it reaches the RichText component solves the issue for both the editor and the frontend (since they share the same attribute).

I'll work on a fix for this soon, let me know if there's anything I might have missed

BugReportOnWeb avatar Jun 18 '25 19:06 BugReportOnWeb