Make WordPress Core


Ignore:
Timestamp:
07/06/2026 04:23:49 AM (2 months ago)
Author:
isabel_brison
Message:

Editor: add support to style current menu item via theme.json.

Adds processing of custom states from theme.json and a new -current custom state type that is enabled for navigation link block.

Props onemaggie, isabel_brison.
Fixes #64806.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r62639 r62641  
    75717571                $this->assertStringNotContainsString( 'gap', $result, 'Array value should be skipped' );
    75727572        }
     7573
     7574        /**
     7575         * Test that block custom states (e.g. -current) are processed correctly.
     7576         *
     7577         * @covers WP_Theme_JSON::get_styles_for_block
     7578         *
     7579         * @ticket 64806
     7580         */
     7581        public function test_block_custom_states_are_processed() {
     7582                // Only -current styles, no base block styles, so we can assert the
     7583                // output uses the current-menu-item selector and not the block selector.
     7584                $theme_json = new WP_Theme_JSON(
     7585                        array(
     7586                                'version' => WP_Theme_JSON::LATEST_SCHEMA,
     7587                                'styles'  => array(
     7588                                        'blocks' => array(
     7589                                                'core/navigation-link' => array(
     7590                                                        '-current' => array(
     7591                                                                'color' => array(
     7592                                                                        'text'       => 'red',
     7593                                                                        'background' => 'blue',
     7594                                                                ),
     7595                                                        ),
     7596                                                ),
     7597                                        ),
     7598                                ),
     7599                        )
     7600                );
     7601
     7602                $current_node = array(
     7603                        'path'     => array( 'styles', 'blocks', 'core/navigation-link', '-current' ),
     7604                        'selector' => '.wp-block-navigation .current-menu-item',
     7605                );
     7606                $expected     = ':root :where(.wp-block-navigation .current-menu-item){background-color: blue;color: red;}';
     7607
     7608                $this->assertSame( $expected, $theme_json->get_styles_for_block( $current_node ) );
     7609        }
     7610
     7611        /**
     7612         * Test that block custom states compound correctly with pseudo-selectors (e.g. -current + :hover).
     7613         *
     7614         * @covers WP_Theme_JSON::get_styles_for_block
     7615         *
     7616         * @ticket 64806
     7617         */
     7618        public function test_block_custom_states_compound_with_pseudo_selectors() {
     7619                $theme_json = new WP_Theme_JSON(
     7620                        array(
     7621                                'version' => WP_Theme_JSON::LATEST_SCHEMA,
     7622                                'styles'  => array(
     7623                                        'blocks' => array(
     7624                                                'core/navigation-link' => array(
     7625                                                        '-current' => array(
     7626                                                                'color'  => array(
     7627                                                                        'text'       => 'red',
     7628                                                                        'background' => 'blue',
     7629                                                                ),
     7630                                                                ':hover' => array(
     7631                                                                        'color' => array(
     7632                                                                                'text'       => 'blue',
     7633                                                                                'background' => 'white',
     7634                                                                        ),
     7635                                                                ),
     7636                                                                ':focus' => array(
     7637                                                                        'color' => array(
     7638                                                                                'text'       => 'green',
     7639                                                                                'background' => 'yellow',
     7640                                                                        ),
     7641                                                                ),
     7642                                                        ),
     7643                                                ),
     7644                                        ),
     7645                                ),
     7646                        )
     7647                );
     7648
     7649                $current_node = array(
     7650                        'path'     => array( 'styles', 'blocks', 'core/navigation-link', '-current' ),
     7651                        'selector' => '.wp-block-navigation .current-menu-item',
     7652                );
     7653                $hover_node   = array(
     7654                        'path'     => array( 'styles', 'blocks', 'core/navigation-link', '-current', ':hover' ),
     7655                        'selector' => '.wp-block-navigation .current-menu-item:hover',
     7656                );
     7657                $focus_node   = array(
     7658                        'path'     => array( 'styles', 'blocks', 'core/navigation-link', '-current', ':focus' ),
     7659                        'selector' => '.wp-block-navigation .current-menu-item:focus',
     7660                );
     7661
     7662                $expected  = ':root :where(.wp-block-navigation .current-menu-item){background-color: blue;color: red;}';
     7663                $expected .= ':root :where(.wp-block-navigation .current-menu-item:hover){background-color: white;color: blue;}';
     7664                $expected .= ':root :where(.wp-block-navigation .current-menu-item:focus){background-color: yellow;color: green;}';
     7665
     7666                $actual  = $theme_json->get_styles_for_block( $current_node );
     7667                $actual .= $theme_json->get_styles_for_block( $hover_node );
     7668                $actual .= $theme_json->get_styles_for_block( $focus_node );
     7669
     7670                $this->assertSame( $expected, $actual );
     7671        }
     7672
     7673        /**
     7674         * Test that non-whitelisted custom states are ignored, and that custom states
     7675         * are ignored on blocks that do not declare support for them.
     7676         *
     7677         * @covers WP_Theme_JSON::get_stylesheet
     7678         *
     7679         * @ticket 64806
     7680         */
     7681        public function test_block_custom_states_ignores_non_whitelisted() {
     7682                // A non-whitelisted state key on a block that supports custom states.
     7683                $theme_json_bogus_state = new WP_Theme_JSON(
     7684                        array(
     7685                                'version' => WP_Theme_JSON::LATEST_SCHEMA,
     7686                                'styles'  => array(
     7687                                        'blocks' => array(
     7688                                                'core/navigation-link' => array(
     7689                                                        'color'  => array(
     7690                                                                'text' => 'black',
     7691                                                        ),
     7692                                                        '-bogus' => array(
     7693                                                                'color' => array(
     7694                                                                        'text' => 'yellow',
     7695                                                                ),
     7696                                                        ),
     7697                                                ),
     7698                                        ),
     7699                                ),
     7700                        )
     7701                );
     7702
     7703                $stylesheet_bogus = $theme_json_bogus_state->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) );
     7704                $this->assertStringNotContainsString( '-bogus', $stylesheet_bogus );
     7705                $this->assertStringNotContainsString( 'yellow', $stylesheet_bogus );
     7706
     7707                // A valid custom state key on a block that does not support custom states.
     7708                $theme_json_unsupported_block = new WP_Theme_JSON(
     7709                        array(
     7710                                'version' => WP_Theme_JSON::LATEST_SCHEMA,
     7711                                'styles'  => array(
     7712                                        'blocks' => array(
     7713                                                'core/paragraph' => array(
     7714                                                        'color'    => array(
     7715                                                                'text' => 'black',
     7716                                                        ),
     7717                                                        '-current' => array(
     7718                                                                'color' => array(
     7719                                                                        'text' => 'red',
     7720                                                                ),
     7721                                                        ),
     7722                                                ),
     7723                                        ),
     7724                                ),
     7725                        )
     7726                );
     7727
     7728                $stylesheet_unsupported = $theme_json_unsupported_block->get_stylesheet( array( 'styles' ), null, array( 'skip_root_layout_styles' => true ) );
     7729                $expected               = ':root :where(p){color: black;}';
     7730                $this->assertSame( $expected, $stylesheet_unsupported );
     7731                $this->assertStringNotContainsString( '-current', $stylesheet_unsupported );
     7732                $this->assertStringNotContainsString( 'current-menu-item', $stylesheet_unsupported );
     7733        }
    75737734}
Note: See TracChangeset for help on using the changeset viewer.