gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Link color is not applied correctly on front end

Open t-hamano opened this issue 3 years ago • 7 comments

Description

This issue is a bit more complicated. If you apply a link color to a block, you will get the following CSS output:

.wp-elements-XXXXXXXXXXXXXXXXXXXXXX a {
	color: blue;
}

If the parent block also has a link color applied, its style will be rendered before the child block's link color in order to give it priority.

// Link color style for the parent block
.wp-elements-XXXXXXXXXXXXXXXXXXXXXX a {
	color: red;
}

// Link color style for the child block
.wp-elements-XXXXXXXXXXXXXXXXXXXXXX a {
	color: blue;
}

This logic generally works correctly, but if blocks with exactly the same markup exist in the content, the parent selector may take precedence.

Step-by-step reproduction instructions

Use the following HTML to insert a block. This content has a paragraph block and a group block with a paragraph block inside.

HTML
<!-- wp:paragraph {"style":{"elements":{"link":{"color":{"text":"var:preset|color|vivid-cyan-blue"}}}}} -->
<p class="has-link-color"><a href="#">This paragraph block text is linked.</a></p>
<!-- /wp:paragraph -->

<!-- wp:group {"align":"wide","style":{"elements":{"link":{"color":{"text":"var:preset|color|vivid-red"}}}},"layout":{"type":"constrained"}} -->
<div class="wp-block-group alignwide has-link-color"><!-- wp:paragraph {"style":{"elements":{"link":{"color":{"text":"var:preset|color|vivid-cyan-blue"}}}}} -->
<p class="has-link-color"><a href="#">This paragraph block text is linked.</a></p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

Notice that the blue link color is applied to the paragraph block and the red link color to the group block, and that the two paragraph blocks have exactly the same text. The blue link color should take precedence in the paragraph block within the group block.

editor

Check the front-end display. The paragraph block within a group block has the red link color applied to the parent group block. Check the browser developer tool and you will see that the color style of the group block is output after the paragraph block.

css-styles

The two paragraph blocks have exactly the same markup. Therefore, my understanding is that the link style of the paragraph block will come before that of the group block, based on the following logic:

  • The link style of the first paragraph block is output
  • The link style of the next group block is output
  • The paragraphs in the group block generate the exact same selector as the first paragraph block, so no style is output

The reason for this is that if you change even a single character of text in either paragraph block, the link color intended by the front end should be applied because the selectors generated are different.

t-hamano avatar Aug 18 '23 09:08 t-hamano

Update regarding this issue :

I have tested with latest gutenberg version and from my testing it seems that issue is resolved now, below are screenshots and environment info in which i have tested :

WordPress Version : 6.9.4 Gutenberg Version : 23.1.1 Theme : TT5 theme Browser : Chrome, Safari, Brave

Image 1 Image 2
Image Image

dpmehta avatar May 13 '26 12:05 dpmehta

@dpmehta Thanks for the testing. However, this issue has not been resolved yet. Please test it with WordPress 7.0 RC3. However, it seems that this issue is incidentally resolved when the Gutenberg plugin is enabled. The following is a summary of the investigation conducted by AI.


Root cause: filter order on render_block_data

wp_get_elements_class_name() generates the class name via md5(serialize($parsed_block)). The function wp_add_parent_layout_to_parsed_block runs on the same hook at priority 10. If it runs before elements, only the inner block gets a parentLayout key → hash diverges → collision avoided.

Core

In wp-settings.php, elements.php is required before layout.php → elements filter runs first → collision.

Gutenberg

lib/block-supports/elements.php does remove_filter then add_filter, moving elements to the end of priority 10 → elements runs after layout → collision avoided.

Recommendation

  • Gutenberg's "fix" is an accidental side effect and fragile
  • Proper fix: revise the hashing logic in wp_get_elements_class_name(), or have the elements filter explicitly incorporate parent context

t-hamano avatar May 13 '26 12:05 t-hamano

@t-hamano Thanks for guiding , i have tested it with WordPress 7.0 RC3 and gutenberg plugin disabled and i can reproduce the issue and i also came across the function wp_get_elements_class_name() while debugging which generates hash and due to duplicate hash, issue is occurring, i will try to look deeply regarding this issue.

dpmehta avatar May 13 '26 12:05 dpmehta

@t-hamano I have investigated the issue and below are details :

This is a CSS cascade failure caused by aggressive class name deduplication. Currently, the wp_get_elements_class_name() function takes the serialized block data (md5(serialize($block))) and generates a CSS class. If you use two identical paragraph blocks (e.g. one standalone, one inside a Group block), they will generate the exact same wp-elements-* class.

The Style Engine deduplicates the CSS rule and will render it once. If the parent Group block has conflicting styles (like a red link color), its CSS rule is output after the deduplicated child paragraph rule, allowing the parent to incorrectly override the child's styles due to standard CSS source-order precedence.

The Fix

To fix this in a native way, without breaking backwards compatibility or deterministic test outputs, gutenberg_get_elements_class_name() is introduced.

This new function introduces a static collision tracker ($seen_hashes).

  1. For unique blocks (99% of cases): It generates the standard, expected MD5 hash.

  2. For duplicate blocks: If an identical block structure is encountered again during the same page render, it increments a counter and generates a unique hash md5( $hash . $counter ).

This ensures that the same nested blocks get completely unique class names, which in turn allows the Style Engine to output their rules in the correct, sequential order in the DOM, thus preserving the cascade of CSS.

Below is the exact diff to resolve the issue :

If it looks good i would be happy to create PR.

--- a/lib/block-supports/elements.php
+++ b/lib/block-supports/elements.php
@@ -105,6 +105,32 @@ function gutenberg_should_add_elements_class_name( $block, $options ) {
  *
  * @return array The same parsed block with elements classname added if appropriate.
  */
+function gutenberg_get_elements_class_name( $block ) {
+	static $seen_hashes = array();
+
+	$hash = md5( serialize( $block ) );
+
+	if ( isset( $seen_hashes[ $hash ] ) ) {
+		++$seen_hashes[ $hash ];
+		return 'wp-elements-' . md5( $hash . $seen_hashes[ $hash ] );
+	}
+
+	$seen_hashes[ $hash ] = 0;
+
+	return 'wp-elements-' . $hash;
+}
+
+/**
+ * Render the elements stylesheet and adds elements class name to block as required.
+ *
+ * In the case of nested blocks we want the parent element styles to be rendered before their descendants.
+ * This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant:
+ * we want the descendant style to take priority, and this is done by loading it after, in DOM order.
+ *
+ * @param array $parsed_block The parsed block.
+ *
+ * @return array The same parsed block with elements classname added if appropriate.
+ */
 function gutenberg_render_elements_support_styles( $parsed_block ) {
 	/*
 	 * The generation of element styles and classname were moved to the
@@ -152,7 +178,7 @@ function gutenberg_render_elements_support_styles( $parsed_block ) {
 		return $parsed_block;
 	}
 
-	$class_name         = wp_get_elements_class_name( $parsed_block );
+	$class_name         = gutenberg_get_elements_class_name( $parsed_block );
 	$updated_class_name = isset( $parsed_block['attrs']['className'] ) ? $parsed_block['attrs']['className'] . " $class_name" : $class_name;
 
 	_wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name );

dpmehta avatar May 14 '26 14:05 dpmehta

@dpmehta Thank you for the investigation. So, the fundamental issue is that the wp_get_elements_class_name() function needs to be fixed, right? If so, it ultimately requires a fix on the core side. We can submit a core ticket and a core PR, and then close this issue.

t-hamano avatar May 29 '26 13:05 t-hamano

Hi, I have created the core ticket for this since we need to fix this from core: https://core.trac.wordpress.org/ticket/65435

USERSATOSHI avatar Jun 08 '26 13:06 USERSATOSHI

Thank you @USERSATOSHI! Let's close this in favor of https://core.trac.wordpress.org/ticket/65435

t-hamano avatar Jun 08 '26 13:06 t-hamano