Make WordPress Core

Changeset 62702


Ignore:
Timestamp:
07/13/2026 05:39:32 AM (2 months ago)
Author:
isabel_brison
Message:

Editor: enable text alignment for viewport states.

Updates states support to process text align values so block instances can set viewport-specific text align.

Props isabel_brison, andrewserong.
Fixes #65615.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/states.php

    r62671 r62702  
    363363        }
    364364
    365         $compiled = wp_style_engine_get_styles(
     365        $compiled     = wp_style_engine_get_styles(
    366366                wp_normalize_state_style_for_css_output( $style )
    367367        );
    368 
    369         if ( empty( $compiled['declarations'] ) ) {
     368        $declarations = $compiled['declarations'] ?? array();
     369        $text_align   = $style['typography']['textAlign'] ?? null;
     370        // Base text alignment is class-based, so state styles need a declaration.
     371        if ( is_string( $text_align ) && '' !== trim( $text_align ) ) {
     372                $declarations['text-align'] = $text_align;
     373        }
     374
     375        if ( empty( $declarations ) ) {
    370376                return;
    371377        }
     
    374380                'state'        => $state,
    375381                'selector'     => $selector,
    376                 'declarations' => $compiled['declarations'],
     382                'declarations' => $declarations,
    377383        );
    378384        if ( ! empty( $rules_group ) ) {
  • trunk/tests/phpunit/tests/block-supports/states.php

    r62559 r62702  
    961961                $this->assertStringContainsString(
    962962                        '@media (width <= 480px){.' . $matches[0] . '{color:#ff0000 !important;}}',
     963                        $actual_stylesheet
     964                );
     965        }
     966
     967        /**
     968         * Tests that responsive text alignment generates media-query scoped CSS.
     969         *
     970         * @covers ::wp_render_block_states_support
     971         *
     972         * @ticket 65615
     973         */
     974        public function test_responsive_text_alignment_generates_media_query_scoped_css() {
     975                $this->ensure_block_registered( 'core/paragraph' );
     976
     977                $block_content = '<p class="wp-block-paragraph has-text-align-left">Hello</p>';
     978                $block         = array(
     979                        'blockName' => 'core/paragraph',
     980                        'attrs'     => array(
     981                                'style' => array(
     982                                        'typography' => array(
     983                                                'textAlign' => 'left',
     984                                        ),
     985                                        '@mobile'    => array(
     986                                                'typography' => array(
     987                                                        'textAlign' => 'right',
     988                                                ),
     989                                        ),
     990                                ),
     991                        ),
     992                );
     993
     994                $actual = wp_render_block_states_support( $block_content, $block );
     995
     996                $this->assertMatchesRegularExpression(
     997                        '/^<p class="wp-block-paragraph has-text-align-left (wp-states-[a-f0-9]{8})">Hello<\/p>$/',
     998                        $actual
     999                );
     1000                preg_match( '/wp-states-[a-f0-9]{8}/', $actual, $matches );
     1001                $actual_stylesheet = wp_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) );
     1002
     1003                $this->assertStringContainsString(
     1004                        '@media (width <= 480px){.' . $matches[0] . '{text-align:right !important;}}',
    9631005                        $actual_stylesheet
    9641006                );
Note: See TracChangeset for help on using the changeset viewer.