gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

[Pattern Block Overwrides] - Inline style attribute is stripped from rich text

Open webexpr-dhenriet opened this issue 1 year ago • 4 comments

Description

There is a major inconsistency in how inline style attributes are handled within synchronized patterns that have overrides enabled. The rich text "Highlight" tool in the block editor creates a <mark> tag with an inline style attribute (e.g., style="background-color:..."). However, when a user overrides the content of a block containing this markup within a synced pattern, the style attribute is stripped out upon saving, while the class attribute is preserved.

The core inconsistency is :

  • why does the editor provide a tool that generates inline styles if the pattern override system is designed to strip them ?
  • the behavior should be consistent: either never add inline styles, or always preserve them ?

Step-by-step reproduction instructions

  1. Create a new synchronized pattern.
  2. Insert a Heading block. In the heading's text, use the "Highlight" tool to color a portion of the text. To create colored text without a background, one common method is to set the highlight background to transparent, which results in a <mark> tag with an inline style
  3. Ensure that overrides are enabled for the Heading block within the pattern.
  4. Save the synchronized pattern.
  5. Insert this pattern into a new page or post.
  6. Modify the text content of the heading. For example, change "Title with color" to "A new title with color".
  7. Save the page and inspect the code of the overridden heading block.

<!-- wp:group {"metadata":{"name":"Custom title section"},"style":{"spacing":{"padding":{"bottom":"var:preset|spacing|50"}}},"layout":{"type":"constrained"}} -->
<div class="wp-block-group" style="padding-bottom:var(--wp--preset--spacing--50)">
<!-- wp:heading {"metadata":{"name":"Title","bindings":{"__default":{"source":"core/pattern-overrides"}}}} -->
<h2 class="wp-block-heading">
Title
<mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-orange-700-color">with color</mark>
</h2>
<!-- /wp:heading -->
</div>
<!-- /wp:group -->

Actual behavior The style="background-color:rgba(0, 0, 0, 0)" attribute is stripped from the mark tag, while the class attribute is correctly preserved. The resulting code is :

<h2 class="wp-block-heading">Title <mark class="has-inline-color has-orange-700-color">with color</mark></h2>

Expected behavior The mark tag within the overridden heading should preserve both its class and its inline style attribute. The code should look like this :

<h2 class="wp-block-heading">Title <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-orange-700-color">with color</mark></h2>

Screenshots, screen recording, code snippet

Color menu

Image

Pattern

Image

Pattern in a page (back office)

Image


Pattern in page front end

Image

Environment info

  • WordPress Version : 6.8.1
  • Gutenberg Plugin : Not active
  • Theme : custom hybrid theme
  • Theme json version : V3, https://schemas.wp.org/wp/6.6/theme.json
  • Browser : All

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

  • [x] Yes

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

  • [x] Yes

Please confirm which theme type you used for testing.

  • [ ] Block
  • [ ] Classic
  • [x] Hybrid (e.g. classic with theme.json)
  • [ ] Not sure

webexpr-dhenriet avatar Jun 06 '25 10:06 webexpr-dhenriet

why does the editor provide a tool that generates inline styles if the pattern override system is designed to strip them ? the behavior should be consistent: either never add inline styles, or always preserve them ?

Thanks for reporting this, it's not designed to do this, unfortunately it's a bug. It should preserve the content. It's a really unusual bug, since the color is preserved.

talldan avatar Jun 09 '25 03:06 talldan

@webexpr-dhenriet The issue can be replicated in the latest version of Gutenberg (21.0.0-rc.1). Image

Frontend: Image

Will update here with a patch.

patil-vipul avatar Jun 09 '25 11:06 patil-vipul

@talldan @patil-vipul thanks for the feedback ;)

webexpr-dhenriet avatar Jun 09 '25 12:06 webexpr-dhenriet

I tested this on trunk and it looks fixed by #84213. The style was removed when the override was rendered, because WP_Block::replace_html() passes the override through wp_kses_post(), and safecss_filter_attr() dropped any value using rgba(). #84213 lets CSS color functions through kses, and with it the override renders as expected. Removing that filter brings the bug back. The Core change is tracked in Trac #66387 for WordPress 7.3, so I think this can be closed once that lands.

dhananjaykuber avatar Nov 12 '26 05:11 dhananjaykuber