Skip to content

Commit 7e66724

Browse files
authored
Merge branch 'WordPress:trunk' into trunk
2 parents d7caead + 0113999 commit 7e66724

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

src/wp-includes/block-supports/states.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,24 @@ function wp_add_block_state_style_rule( &$css_rules, $state, $selector, $style,
362362
return;
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+
$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+
}
368374

369-
if ( empty( $compiled['declarations'] ) ) {
375+
if ( empty( $declarations ) ) {
370376
return;
371377
}
372378

373379
$css_rules[] = array(
374380
'state' => $state,
375381
'selector' => $selector,
376-
'declarations' => $compiled['declarations'],
382+
'declarations' => $declarations,
377383
);
378384
if ( ! empty( $rules_group ) ) {
379385
$css_rules[ count( $css_rules ) - 1 ]['rules_group'] = $rules_group;

tests/phpunit/tests/block-supports/states.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,48 @@ public function test_responsive_root_state_generates_media_query_scoped_css() {
964964
);
965965
}
966966

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;}}',
1005+
$actual_stylesheet
1006+
);
1007+
}
1008+
9671009
/**
9681010
* Tests that a responsive element color generates media-query scoped CSS.
9691011
*

0 commit comments

Comments
 (0)