Skip to content

Commit dd2558f

Browse files
authored
Merge branch 'WordPress:trunk' into trunk
2 parents 2802df7 + eadae60 commit dd2558f

2 files changed

Lines changed: 118 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3980,7 +3980,12 @@ static function ( $pseudo_selector ) use ( $selector ) {
39803980

39813981
// 7. Generate and append any custom CSS rules.
39823982
if ( isset( $node['css'] ) && ! $is_root_selector ) {
3983-
$block_rules .= $this->process_blocks_custom_css( $node['css'], $selector );
3983+
$css_feature_selector = $block_metadata['selectors']['css'] ?? null;
3984+
if ( is_array( $css_feature_selector ) ) {
3985+
$css_feature_selector = $css_feature_selector['root'] ?? null;
3986+
}
3987+
$css_selector = is_string( $css_feature_selector ) ? $css_feature_selector : $selector;
3988+
$block_rules .= $this->process_blocks_custom_css( $node['css'], $css_selector );
39843989
}
39853990

39863991
// 8. Wrap the entire block output in a media query if this is a responsive node.
@@ -5494,10 +5499,10 @@ protected function get_feature_declarations_for_node( $metadata, &$node ) {
54945499

54955500
foreach ( $metadata['selectors'] as $feature => $feature_selectors ) {
54965501
/*
5497-
* Skip if this is the block's root selector or the block doesn't
5498-
* have any styles for the feature.
5502+
* Skip if this is the block's root selector, the custom CSS
5503+
* selector, or the block doesn't have any styles for the feature.
54995504
*/
5500-
if ( 'root' === $feature || empty( $node[ $feature ] ) ) {
5505+
if ( 'root' === $feature || 'css' === $feature || empty( $node[ $feature ] ) ) {
55015506
continue;
55025507
}
55035508

tests/phpunit/tests/theme/wpThemeJson.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8456,4 +8456,113 @@ public function data_prepend_to_selector() {
84568456
),
84578457
);
84588458
}
8459+
8460+
/**
8461+
* Tests that block custom CSS uses the css feature selector when defined
8462+
* in block metadata selectors config.
8463+
*
8464+
* @ticket 64695
8465+
*/
8466+
public function test_get_styles_for_block_custom_css_uses_css_feature_selector() {
8467+
$theme_json = new WP_Theme_JSON(
8468+
array(
8469+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
8470+
'styles' => array(
8471+
'blocks' => array(
8472+
'core/paragraph' => array(
8473+
'css' => 'color:red;',
8474+
),
8475+
),
8476+
),
8477+
)
8478+
);
8479+
8480+
$paragraph_node = array(
8481+
'name' => 'core/paragraph',
8482+
'path' => array( 'styles', 'blocks', 'core/paragraph' ),
8483+
'selector' => 'p',
8484+
'selectors' => array(
8485+
'root' => 'p',
8486+
'css' => '.custom-p',
8487+
),
8488+
);
8489+
8490+
$this->assertSame(
8491+
':root :where(.custom-p){color:red;}',
8492+
$theme_json->get_styles_for_block( $paragraph_node )
8493+
);
8494+
}
8495+
8496+
/**
8497+
* Tests that block custom CSS falls back to the root selector when no
8498+
* css feature selector is defined in block metadata selectors config.
8499+
*
8500+
* @ticket 64695
8501+
*/
8502+
public function test_get_styles_for_block_custom_css_falls_back_to_root_selector() {
8503+
$theme_json = new WP_Theme_JSON(
8504+
array(
8505+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
8506+
'styles' => array(
8507+
'blocks' => array(
8508+
'core/paragraph' => array(
8509+
'css' => 'color:red;',
8510+
),
8511+
),
8512+
),
8513+
)
8514+
);
8515+
8516+
$paragraph_node = array(
8517+
'name' => 'core/paragraph',
8518+
'path' => array( 'styles', 'blocks', 'core/paragraph' ),
8519+
'selector' => 'p',
8520+
'selectors' => array(
8521+
'root' => 'p',
8522+
),
8523+
);
8524+
8525+
$this->assertSame(
8526+
':root :where(p){color:red;}',
8527+
$theme_json->get_styles_for_block( $paragraph_node )
8528+
);
8529+
}
8530+
8531+
/**
8532+
* Tests that block custom CSS uses the css feature selector when defined
8533+
* as an object with a root subkey in block metadata selectors config.
8534+
*
8535+
* @ticket 64695
8536+
*/
8537+
public function test_get_styles_for_block_custom_css_uses_css_feature_selector_object_form() {
8538+
$theme_json = new WP_Theme_JSON(
8539+
array(
8540+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
8541+
'styles' => array(
8542+
'blocks' => array(
8543+
'core/paragraph' => array(
8544+
'css' => 'color:red;',
8545+
),
8546+
),
8547+
),
8548+
)
8549+
);
8550+
8551+
$paragraph_node = array(
8552+
'name' => 'core/paragraph',
8553+
'path' => array( 'styles', 'blocks', 'core/paragraph' ),
8554+
'selector' => 'p',
8555+
'selectors' => array(
8556+
'root' => 'p',
8557+
'css' => array(
8558+
'root' => '.custom-p',
8559+
),
8560+
),
8561+
);
8562+
8563+
$this->assertSame(
8564+
':root :where(.custom-p){color:red;}',
8565+
$theme_json->get_styles_for_block( $paragraph_node )
8566+
);
8567+
}
84598568
}

0 commit comments

Comments
 (0)