Skip to content

Commit ac2eeb9

Browse files
committed
Editor: Fix complex variation selectors when using selectors API.
Fixes a bug in the manipulation of selectors for block style variations that would result in an incorrect selector and fail to match the appropriate elements on the frontend. Props aaronrobertshaw, ramonopoly, joemcgill. Fixes #62471. git-svn-id: https://develop.svn.wordpress.org/trunk@59814 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 108af60 commit ac2eeb9

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/wp-includes/class-wp-theme-json.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,8 +2850,14 @@ public function get_styles_for_block( $block_metadata ) {
28502850

28512851
// Combine selectors with style variation's selector and add to overall style variation declarations.
28522852
foreach ( $variation_declarations as $current_selector => $new_declarations ) {
2853-
// If current selector includes block classname, remove it but leave the whitespace in.
2854-
$shortened_selector = str_replace( $block_metadata['selector'] . ' ', ' ', $current_selector );
2853+
/*
2854+
* Clean up any whitespace between comma separated selectors.
2855+
* This prevents these spaces breaking compound selectors such as:
2856+
* - `.wp-block-list:not(.wp-block-list .wp-block-list)`
2857+
* - `.wp-block-image img, .wp-block-image.my-class img`
2858+
*/
2859+
$clean_current_selector = preg_replace( '/,\s+/', ',', $current_selector );
2860+
$shortened_selector = str_replace( $block_metadata['selector'], '', $clean_current_selector );
28552861

28562862
// Prepend the variation selector to the current selector.
28572863
$split_selectors = explode( ',', $shortened_selector );

tests/phpunit/tests/theme/wpThemeJson.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4627,6 +4627,80 @@ public function data_get_styles_for_block_with_style_variations() {
46274627
);
46284628
}
46294629

4630+
/**
4631+
* Tests that block style variation selectors are generated correctly
4632+
* for block selectors of various structures.
4633+
*
4634+
* @ticket 62471
4635+
*/
4636+
public function test_get_styles_for_block_with_style_variations_and_custom_selectors() {
4637+
register_block_type(
4638+
'test/milk',
4639+
array(
4640+
'api_version' => 3,
4641+
'selectors' => array(
4642+
'root' => '.milk',
4643+
'color' => '.wp-block-test-milk .liquid, .wp-block-test-milk:not(.spoiled), .wp-block-test-milk.in-bottle',
4644+
),
4645+
)
4646+
);
4647+
4648+
register_block_style(
4649+
'test/milk',
4650+
array(
4651+
'name' => 'chocolate',
4652+
'label' => 'Chocolate',
4653+
)
4654+
);
4655+
4656+
$theme_json = new WP_Theme_JSON(
4657+
array(
4658+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
4659+
'styles' => array(
4660+
'blocks' => array(
4661+
'test/milk' => array(
4662+
'color' => array(
4663+
'background' => 'white',
4664+
),
4665+
'variations' => array(
4666+
'chocolate' => array(
4667+
'color' => array(
4668+
'background' => '#35281E',
4669+
),
4670+
),
4671+
),
4672+
),
4673+
),
4674+
),
4675+
)
4676+
);
4677+
4678+
$metadata = array(
4679+
'name' => 'test/milk',
4680+
'path' => array( 'styles', 'blocks', 'test/milk' ),
4681+
'selector' => '.wp-block-test-milk',
4682+
'selectors' => array(
4683+
'color' => '.wp-block-test-milk .liquid, .wp-block-test-milk:not(.spoiled), .wp-block-test-milk.in-bottle',
4684+
),
4685+
'variations' => array(
4686+
'chocolate' => array(
4687+
'path' => array( 'styles', 'blocks', 'test/milk', 'variations', 'chocolate' ),
4688+
'selector' => '.is-style-chocolate.wp-block-test-milk',
4689+
),
4690+
),
4691+
);
4692+
4693+
$actual_styles = $theme_json->get_styles_for_block( $metadata );
4694+
$default_styles = ':root :where(.wp-block-test-milk .liquid, .wp-block-test-milk:not(.spoiled), .wp-block-test-milk.in-bottle){background-color: white;}';
4695+
$variation_styles = ':root :where(.is-style-chocolate.wp-block-test-milk .liquid,.is-style-chocolate.wp-block-test-milk:not(.spoiled),.is-style-chocolate.wp-block-test-milk.in-bottle){background-color: #35281E;}';
4696+
$expected = $default_styles . $variation_styles;
4697+
4698+
unregister_block_style( 'test/milk', 'chocolate' );
4699+
unregister_block_type( 'test/milk' );
4700+
4701+
$this->assertSame( $expected, $actual_styles );
4702+
}
4703+
46304704
public function test_block_style_variations() {
46314705
wp_set_current_user( static::$administrator_id );
46324706

0 commit comments

Comments
 (0)