Skip to content

Commit 64ede28

Browse files
Editor: ensure no incorrect selector output for pseudo-states.
The output for combined feature selectors and pseudo-states was fixed in [62444] and this removes an extra loop that was outputting an incorrect selector and adds a test. Props onemaggie, isabel_brison, wildworks. Fixes #64838. git-svn-id: https://develop.svn.wordpress.org/trunk@62639 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 448f857 commit 64ede28

2 files changed

Lines changed: 36 additions & 25 deletions

File tree

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,23 +3498,6 @@ public function get_styles_for_block( $block_metadata ) {
34983498
$element_pseudo_allowed = static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ];
34993499
}
35003500

3501-
/*
3502-
* Check if we're processing a block pseudo-selector.
3503-
* $block_metadata['path'] = array( 'styles', 'blocks', 'core/button', ':hover' );
3504-
*/
3505-
$is_processing_block_pseudo = false;
3506-
$block_pseudo_selector = null;
3507-
if ( in_array( 'blocks', $block_metadata['path'], true ) && count( $block_metadata['path'] ) >= 4 ) {
3508-
$block_name = static::get_block_name_from_metadata_path( $block_metadata ); // 'core/button'
3509-
$last_path_element = $block_metadata['path'][ count( $block_metadata['path'] ) - 1 ]; // ':hover'
3510-
3511-
if ( isset( static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ] ) &&
3512-
in_array( $last_path_element, static::VALID_BLOCK_PSEUDO_SELECTORS[ $block_name ], true ) ) {
3513-
$is_processing_block_pseudo = true;
3514-
$block_pseudo_selector = $last_path_element;
3515-
}
3516-
}
3517-
35183501
/*
35193502
* Check for allowed pseudo classes (e.g. ":hover") from the $selector ("a:hover").
35203503
* This also resets the array keys.
@@ -3544,15 +3527,12 @@ static function ( $pseudo_selector ) use ( $selector ) {
35443527
&& in_array( $pseudo_selector, static::VALID_ELEMENT_PSEUDO_SELECTORS[ $current_element ], true )
35453528
) {
35463529
$declarations = static::compute_style_properties( $node[ $pseudo_selector ], $settings, null, $this->theme_json, $selector, $use_root_padding );
3547-
} elseif ( $is_processing_block_pseudo ) {
3548-
// Process block pseudo-selector styles
3549-
// For block pseudo-selectors, we need to get the block data first, then access the pseudo-selector
3550-
$block_name = static::get_block_name_from_metadata_path( $block_metadata ); // 'core/button'
3551-
$block_data = _wp_array_get( $this->theme_json, array( 'styles', 'blocks', $block_name ), array() );
3552-
$pseudo_data = $block_data[ $block_pseudo_selector ] ?? array();
3553-
3554-
$declarations = static::compute_style_properties( $pseudo_data, $settings, null, $this->theme_json, $selector, $use_root_padding );
35553530
} else {
3531+
/*
3532+
* For block pseudo-selector nodes (e.g. ':hover'), $node has already had any
3533+
* feature-selector properties (e.g. writingMode) removed by get_feature_declarations_for_node,
3534+
* so those properties are not output twice.
3535+
*/
35563536
$declarations = static::compute_style_properties( $node, $settings, null, $this->theme_json, $selector, $use_root_padding );
35573537
}
35583538

tests/phpunit/tests/theme/wpThemeJson.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7160,6 +7160,37 @@ public function test_merge_incoming_data_unique_slugs_always_preserved() {
71607160
$this->assertEqualSetsWithIndex( $expected, $actual );
71617161
}
71627162

7163+
/**
7164+
* Tests that when a block with a custom feature selector (e.g. core/button's writingMode
7165+
* uses '.wp-block-button' rather than the root '.wp-block-button .wp-block-button__link')
7166+
* has pseudo-state styles, the feature selector CSS is scoped to the pseudo-state and not
7167+
* output under the block's default-state selector.
7168+
*/
7169+
public function test_get_stylesheet_pseudo_selector_scopes_feature_selector_css() {
7170+
$theme_json = new WP_Theme_JSON(
7171+
array(
7172+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
7173+
'styles' => array(
7174+
'blocks' => array(
7175+
'core/button' => array(
7176+
':hover' => array(
7177+
'typography' => array(
7178+
'writingMode' => 'vertical-rl',
7179+
),
7180+
),
7181+
),
7182+
),
7183+
),
7184+
),
7185+
'default'
7186+
);
7187+
7188+
$css = $theme_json->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) );
7189+
7190+
// writing-mode should be scoped to :hover, not the root block selector.
7191+
$this->assertSame( ':root :where(.wp-block-button:hover){writing-mode: vertical-rl;}', $css );
7192+
}
7193+
71637194
/**
71647195
* Test that block pseudo selectors are processed correctly.
71657196
*/

0 commit comments

Comments
 (0)