Make WordPress Core

Changeset 64340


Ignore:
Timestamp:
10/14/2026 05:12:04 AM (5 days ago)
Author:
isabel_brison
Message:

Editor: fix block style variation custom CSS selector

Ensures block style variations use the feature selector for custom CSS if the block defines one.

Props isabel_brison, andrewserong.
Fixes #66297.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r64309 r64340  
    39173917                $responsive_media_queries = static::get_viewport_media_queries( $settings['viewport'] ?? null );
    39183918
     3919                $css_feature_selector = $block_metadata['selectors']['css'] ?? null;
     3920                if ( is_array( $css_feature_selector ) ) {
     3921                        $css_feature_selector = $css_feature_selector['root'] ?? null;
     3922                }
     3923                if ( ! is_string( $css_feature_selector ) ) {
     3924                        $css_feature_selector = null;
     3925                }
     3926
    39193927                // Update text indent selector for paragraph blocks based on the textIndent setting.
    39203928                $block_name           = $block_metadata['name'] ?? null;
    … …  
    39663974                                $style_variation_declarations  = array_merge( $style_variation_declarations, $variation_pseudo_declarations );
    39673975
     3976                                // Custom CSS targets the block's `css` feature selector, if any.
     3977                                $variation_css_selector = null !== $css_feature_selector
     3978                                        ? static::get_block_style_variation_feature_selector( $style_variation, $css_feature_selector )
     3979                                        : $style_variation['selector'];
     3980
    39683981                                // Store custom CSS for the style variation.
    39693982                                if ( isset( $style_variation_node['css'] ) ) {
    3970                                         $style_variation_custom_css[ $style_variation['selector'] ] = $this->process_blocks_custom_css( $style_variation_node['css'], $style_variation['selector'] );
     3983                                        $style_variation_custom_css[ $style_variation['selector'] ] = $this->process_blocks_custom_css( $style_variation_node['css'], $variation_css_selector );
    39713984                                }
    39723985
    … …  
    40344047                                        // Process custom CSS for this breakpoint.
    40354048                                        if ( isset( $breakpoint_node['css'] ) ) {
    4036                                                 $breakpoint_custom_css     = static::process_blocks_custom_css( $breakpoint_node['css'], $style_variation['selector'] );
     4049                                                $breakpoint_custom_css     = static::process_blocks_custom_css( $breakpoint_node['css'], $variation_css_selector );
    40374050                                                $variation_responsive_css .= $breakpoint_media . '{' . $breakpoint_custom_css . '}';
    40384051                                        }
    … …  
    42784291                // 7. Generate and append any custom CSS rules.
    42794292                if ( isset( $node['css'] ) && ! $is_root_selector ) {
    4280                         $css_feature_selector = $block_metadata['selectors']['css'] ?? null;
    4281                         if ( is_array( $css_feature_selector ) ) {
    4282                                 $css_feature_selector = $css_feature_selector['root'] ?? null;
    4283                         }
    4284                         $css_selector = is_string( $css_feature_selector ) ? $css_feature_selector : $selector;
     4293                        $css_selector = $css_feature_selector ?? $selector;
    42854294                        $block_rules .= $this->process_blocks_custom_css( $node['css'], $css_selector );
    42864295                }
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r64309 r64340  
    95559555                );
    95569556        }
     9557
     9558        /**
     9559         * Tests that block style variation custom CSS, including breakpoint custom
     9560         * CSS, uses the block's css feature selector when defined.
     9561         *
     9562         * @ticket 66297
     9563         *
     9564         */
     9565        public function test_get_styles_for_block_variation_custom_css_uses_css_feature_selector() {
     9566                register_block_style(
     9567                        'core/paragraph',
     9568                        array(
     9569                                'name'  => 'fancy',
     9570                                'label' => 'Fancy',
     9571                        )
     9572                );
     9573
     9574                $theme_json = new WP_Theme_JSON(
     9575                        array(
     9576                                'version' => WP_Theme_JSON::LATEST_SCHEMA,
     9577                                'styles'  => array(
     9578                                        'blocks' => array(
     9579                                                'core/paragraph' => array(
     9580                                                        'variations' => array(
     9581                                                                'fancy' => array(
     9582                                                                        'css'     => 'color:red;',
     9583                                                                        '@tablet' => array(
     9584                                                                                'css' => 'color:blue;',
     9585                                                                        ),
     9586                                                                ),
     9587                                                        ),
     9588                                                ),
     9589                                        ),
     9590                                ),
     9591                        )
     9592                );
     9593
     9594                $paragraph_node = array(
     9595                        'name'       => 'core/paragraph',
     9596                        'path'       => array( 'styles', 'blocks', 'core/paragraph' ),
     9597                        'selector'   => 'p',
     9598                        'selectors'  => array(
     9599                                'root' => 'p',
     9600                                'css'  => '.custom-p',
     9601                        ),
     9602                        'variations' => array(
     9603                                array(
     9604                                        'path'     => array( 'styles', 'blocks', 'core/paragraph', 'variations', 'fancy' ),
     9605                                        'selector' => 'p.is-style-fancy',
     9606                                ),
     9607                        ),
     9608                );
     9609
     9610                $actual = $theme_json->get_styles_for_block( $paragraph_node );
     9611
     9612                unregister_block_style( 'core/paragraph', 'fancy' );
     9613
     9614                $this->assertStringContainsString( ':root :where(.custom-p.is-style-fancy){color:red;}', $actual );
     9615                $this->assertStringContainsString( '{:root :where(.custom-p.is-style-fancy){color:blue;}}', $actual );
     9616                $this->assertStringNotContainsString( ':root :where(p.is-style-fancy){color:', $actual );
     9617        }
    95579618}
Note: See TracChangeset for help on using the changeset viewer.