mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Editor: Fix styles for nested elements (link color)
This fixes an issue by which link color behaves differently in the editor and front end. Related Gutenberg PR: https://github.com/WordPress/gutenberg/pull/37728. Props oandregal. See #55567. git-svn-id: https://develop.svn.wordpress.org/trunk@53260 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
dea44459da
commit
0cb3193616
@ -7,7 +7,20 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* Render the elements stylesheet.
|
||||
* Get the elements class names.
|
||||
*
|
||||
* @since 6.0.0
|
||||
* @access private
|
||||
*
|
||||
* @param array $block Block object.
|
||||
* @return string The unique class name.
|
||||
*/
|
||||
function wp_get_elements_class_name( $block ) {
|
||||
return 'wp-elements-' . md5( serialize( $block ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the block content with elements class names.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @access private
|
||||
@ -43,17 +56,7 @@ function wp_render_elements_support( $block_content, $block ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$class_name = wp_unique_id( 'wp-elements-' );
|
||||
|
||||
if ( strpos( $link_color, 'var:preset|color|' ) !== false ) {
|
||||
// Get the name from the string and add proper styles.
|
||||
$index_to_splice = strrpos( $link_color, '|' ) + 1;
|
||||
$link_color_name = substr( $link_color, $index_to_splice );
|
||||
$link_color = "var(--wp--preset--color--$link_color_name)";
|
||||
}
|
||||
$link_color_declaration = esc_html( safecss_filter_attr( "color: $link_color" ) );
|
||||
|
||||
$style = ".$class_name a{" . $link_color_declaration . ';}';
|
||||
$class_name = wp_get_elements_class_name( $block );
|
||||
|
||||
// Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
|
||||
// Retrieve the opening tag of the first HTML element.
|
||||
@ -75,9 +78,62 @@ function wp_render_elements_support( $block_content, $block ) {
|
||||
$content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 );
|
||||
}
|
||||
|
||||
wp_enqueue_block_support_styles( $style );
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the elements stylesheet.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @since 6.0.0
|
||||
* @access private
|
||||
*
|
||||
* @param string|null $pre_render The pre-rendered content. Default null.
|
||||
* @param array $block The block being rendered.
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
function wp_render_elements_support_styles( $pre_render, $block ) {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
$skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
|
||||
if ( $skip_link_color_serialization ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$link_color = null;
|
||||
if ( ! empty( $block['attrs'] ) ) {
|
||||
$link_color = _wp_array_get( $block['attrs'], array( 'style', 'elements', 'link', 'color', 'text' ), null );
|
||||
}
|
||||
|
||||
/*
|
||||
* For now we only care about link color.
|
||||
* This code in the future when we have a public API
|
||||
* should take advantage of WP_Theme_JSON::compute_style_properties
|
||||
* and work for any element and style.
|
||||
*/
|
||||
if ( null === $link_color ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$class_name = wp_get_elements_class_name( $block );
|
||||
|
||||
if ( strpos( $link_color, 'var:preset|color|' ) !== false ) {
|
||||
// Get the name from the string and add proper styles.
|
||||
$index_to_splice = strrpos( $link_color, '|' ) + 1;
|
||||
$link_color_name = substr( $link_color, $index_to_splice );
|
||||
$link_color = "var(--wp--preset--color--$link_color_name)";
|
||||
}
|
||||
$link_color_declaration = esc_html( safecss_filter_attr( "color: $link_color" ) );
|
||||
|
||||
$style = ".$class_name a{" . $link_color_declaration . ';}';
|
||||
|
||||
wp_enqueue_block_support_styles( $style );
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
add_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
|
||||
add_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 );
|
||||
|
||||
@ -11,7 +11,7 @@ class Tests_Block_Supports_Elements extends WP_UnitTestCase {
|
||||
* @return string String where the unique id classes were replaced with "wp-elements-1".
|
||||
*/
|
||||
private static function make_unique_id_one( $string ) {
|
||||
return preg_replace( '/wp-elements-\d+/', 'wp-elements-1', $string );
|
||||
return preg_replace( '/wp-elements-[a-zA-Z0-9]+/', 'wp-elements-1', $string );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user