diff --git a/src/wp-includes/block-supports/layout.php b/src/wp-includes/block-supports/layout.php index 22fe6d7b7a51c..98b44079ba747 100644 --- a/src/wp-includes/block-supports/layout.php +++ b/src/wp-includes/block-supports/layout.php @@ -312,6 +312,7 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false * Renders the layout config to the block wrapper. * * @since 5.8.0 + * @since 6.3.0 Adds compound class to layout wrapper for global spacing styles. * @access private * * @param string $block_content Rendered block content. @@ -474,6 +475,10 @@ function wp_render_layout_support_flag( $block_content, $block ) { } } + // Add combined layout and block classname for global styles to hook onto. + $block_name = explode( '/', $block['blockName'] ); + $class_names[] = 'wp-block-' . end( $block_name ) . '-' . $layout_classname; + $content_with_outer_classnames = ''; if ( ! empty( $outer_class_names ) ) { diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 8a44afbb35ac2..14c0bc73f42a5 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1221,6 +1221,7 @@ protected function get_block_classes( $style_nodes ) { * Gets the CSS layout rules for a particular block from theme.json layout definitions. * * @since 6.1.0 + * @since 6.3.0 Reduced specificity for layout margin rules. * * @param array $block_metadata Metadata about the block to get styles for. * @return string Layout styles for the block. @@ -1317,7 +1318,7 @@ protected function get_layout_styles( $block_metadata ) { $spacing_rule['selector'] ); } else { - $format = static::ROOT_BLOCK_SELECTOR === $selector ? '%s .%s%s' : '%s.%s%s'; + $format = static::ROOT_BLOCK_SELECTOR === $selector ? ':where(%s .%s) %s' : '%s-%s%s'; $layout_selector = sprintf( $format, $selector, @@ -2532,8 +2533,9 @@ public function get_root_layout_rules( $selector, $block_metadata ) { $has_block_gap_support = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'blockGap' ) ) !== null; if ( $has_block_gap_support ) { $block_gap_value = static::get_property_value( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ) ); - $css .= '.wp-site-blocks > * { margin-block-start: 0; margin-block-end: 0; }'; - $css .= ".wp-site-blocks > * + * { margin-block-start: $block_gap_value; }"; + $css .= ":where(.wp-site-blocks) > * { margin-block-start: $block_gap_value; margin-block-end: 0; }"; + $css .= ':where(.wp-site-blocks) > :first-child:first-child { margin-block-start: 0; }'; + $css .= ':where(.wp-site-blocks) > :last-child:last-child { margin-block-end: 0; }'; // For backwards compatibility, ensure the legacy block gap CSS variable is still available. $css .= "$selector { --wp--style--block-gap: $block_gap_value; }"; diff --git a/src/wp-includes/theme.json b/src/wp-includes/theme.json index af447990608a3..581bdb0b32406 100644 --- a/src/wp-includes/theme.json +++ b/src/wp-includes/theme.json @@ -219,14 +219,19 @@ ], "spacingStyles": [ { - "selector": " > *", + "selector": " > :first-child:first-child", + "rules": { + "margin-block-start": "0" + } + }, + { + "selector": " > :last-child:last-child", "rules": { - "margin-block-start": "0", "margin-block-end": "0" } }, { - "selector": " > * + *", + "selector": " > *", "rules": { "margin-block-start": null, "margin-block-end": "0" @@ -279,14 +284,19 @@ ], "spacingStyles": [ { - "selector": " > *", + "selector": " > :first-child:first-child", + "rules": { + "margin-block-start": "0" + } + }, + { + "selector": " > :last-child:last-child", "rules": { - "margin-block-start": "0", "margin-block-end": "0" } }, { - "selector": " > * + *", + "selector": " > *", "rules": { "margin-block-start": null, "margin-block-end": "0" diff --git a/tests/phpunit/data/blocks/fixtures/core__column.server.html b/tests/phpunit/data/blocks/fixtures/core__column.server.html index a3624daaf02cf..d0b6ab4016d91 100644 --- a/tests/phpunit/data/blocks/fixtures/core__column.server.html +++ b/tests/phpunit/data/blocks/fixtures/core__column.server.html @@ -1,5 +1,5 @@ -
+

Column One, Paragraph One

diff --git a/tests/phpunit/data/blocks/fixtures/core__columns.server.html b/tests/phpunit/data/blocks/fixtures/core__columns.server.html index 1c144512d1238..e35ab2763e833 100644 --- a/tests/phpunit/data/blocks/fixtures/core__columns.server.html +++ b/tests/phpunit/data/blocks/fixtures/core__columns.server.html @@ -1,7 +1,7 @@ -
+
-
+

Column One, Paragraph One

@@ -11,7 +11,7 @@
-
+

Column Two, Paragraph One

diff --git a/tests/phpunit/data/blocks/fixtures/core__columns__deprecated.server.html b/tests/phpunit/data/blocks/fixtures/core__columns__deprecated.server.html index 9d213ddcdf34e..c61aabf6822a1 100644 --- a/tests/phpunit/data/blocks/fixtures/core__columns__deprecated.server.html +++ b/tests/phpunit/data/blocks/fixtures/core__columns__deprecated.server.html @@ -1,5 +1,5 @@ -
+

Column One, Paragraph One

diff --git a/tests/phpunit/data/blocks/fixtures/core__gallery-with-caption.server.html b/tests/phpunit/data/blocks/fixtures/core__gallery-with-caption.server.html index 18e3f1d8c87bc..49912d63c46c0 100644 --- a/tests/phpunit/data/blocks/fixtures/core__gallery-with-caption.server.html +++ b/tests/phpunit/data/blocks/fixtures/core__gallery-with-caption.server.html @@ -1,6 +1,6 @@