Editor: Fix block style variation selector generation.

These changes fix the generation of selectors for block style variations. Previously, an incorrect CSS selector could be generated if the block's base selector used an element tag etc.

Props aaronrobertshaw, youknowriad, mukesh27.
Fixes #60453.
Built from https://develop.svn.wordpress.org/trunk@57547


git-svn-id: http://core.svn.wordpress.org/trunk@57048 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
youknowriad 2024-02-07 08:53:13 +00:00
parent e692b93540
commit 8f1ccc91ae
2 changed files with 36 additions and 2 deletions

View File

@ -1032,7 +1032,7 @@ class WP_Theme_JSON {
if ( ! empty( $block_type->styles ) ) {
$style_selectors = array();
foreach ( $block_type->styles as $style ) {
$style_selectors[ $style['name'] ] = static::append_to_selector( '.is-style-' . $style['name'], static::$blocks_metadata[ $block_name ]['selector'] );
$style_selectors[ $style['name'] ] = static::get_block_style_variation_selector( $style['name'], static::$blocks_metadata[ $block_name ]['selector'] );
}
static::$blocks_metadata[ $block_name ]['styleVariations'] = $style_selectors;
}
@ -3925,4 +3925,38 @@ class WP_Theme_JSON {
$theme_json->theme_json['styles'] = self::convert_variables_to_value( $styles, $vars );
return $theme_json;
}
/**
* Generates a selector for a block style variation.
*
* @since 6.5.0
*
* @param string $variation_name Name of the block style variation.
* @param string $block_selector CSS selector for the block.
* @return string Block selector with block style variation selector added to it.
*/
protected static function get_block_style_variation_selector( $variation_name, $block_selector ) {
$variation_class = ".is-style-$variation_name";
if ( ! $block_selector ) {
return $variation_class;
}
$limit = 1;
$selector_parts = explode( ',', $block_selector );
$result = array();
foreach ( $selector_parts as $part ) {
$result[] = preg_replace_callback(
'/((?::\([^)]+\))?\s*)([^\s:]+)/',
function ( $matches ) use ( $variation_class ) {
return $matches[1] . $matches[2] . $variation_class;
},
$part,
$limit
);
}
return implode( ',', $result );
}
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57546';
$wp_version = '6.5-alpha-57547';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.