Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce layout rule specificity and add compound classname #4623

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/wp-includes/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,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'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For wp_render_layout_support_flag annotation:

@since 6.3.0 Adds compound class to layout wrapper for global spacing styles.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, thanks!

$class_names[] = 'wp-block-' . end( $block_name ) . '-' . $layout_classname;

$content_with_outer_classnames = '';

if ( ! empty( $outer_class_names ) ) {
Expand Down
7 changes: 4 additions & 3 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something like @since 6.3.0 Reduces specificity of layout margin rules. be accurate for the get_layout_styles annotation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks!

$layout_selector = sprintf(
$format,
$selector,
Expand Down Expand Up @@ -2532,8 +2532,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; }";
Expand Down
22 changes: 16 additions & 6 deletions src/wp-includes/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<div class="wp-block-column is-layout-flow">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">

<p>Column One, Paragraph One</p>

Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/data/blocks/fixtures/core__columns.server.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<div class="wp-block-columns has-3-columns is-layout-flex wp-container-1">
<div class="wp-block-columns has-3-columns is-layout-flex wp-container-1 wp-block-columns-is-layout-flex">

<div class="wp-block-column is-layout-flow">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">

<p>Column One, Paragraph One</p>

Expand All @@ -11,7 +11,7 @@
</div>


<div class="wp-block-column is-layout-flow">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">

<p>Column Two, Paragraph One</p>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<div class="wp-block-columns has-3-columns is-layout-flex wp-container-1">
<div class="wp-block-columns has-3-columns is-layout-flex wp-container-1 wp-block-columns-is-layout-flex">

<p class="layout-column-1">Column One, Paragraph One</p>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

<figure
class="wp-block-gallery has-nested-images columns-default is-cropped columns-2 wp-block-gallery-1 is-layout-flex"
class="wp-block-gallery has-nested-images columns-default is-cropped columns-2 wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex"
>

<figure class="wp-block-image size-large">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

<figure
class="wp-block-gallery has-nested-images columns-default is-cropped columns-2 wp-block-gallery-1 is-layout-flex"
class="wp-block-gallery has-nested-images columns-default is-cropped columns-2 wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex"
>

<figure class="wp-block-image size-large">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<figure class="wp-block-gallery has-nested-images columns-1 is-cropped wp-block-gallery-1 is-layout-flex">
<figure class="wp-block-gallery has-nested-images columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="wp-block-image size-large">
<img data-id="1421"
src="https://sergioestevaofolio.files.wordpress.com/2016/09/cropped-img_9054-1.jpg?w=190"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<div class="wp-block-gallery columns-2 is-cropped alignwide wp-block-gallery-1 is-layout-flex">
<div class="wp-block-gallery columns-2 is-cropped alignwide wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="blocks-gallery-image">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="title" />
</figure>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<ul class="wp-block-gallery columns-2 is-cropped alignwide wp-block-gallery-1 is-layout-flex">
<ul class="wp-block-gallery columns-2 is-cropped alignwide wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<li class="blocks-gallery-item">
<figure>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" data-id="1" alt="title" class="wp-image-1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<ul class="wp-block-gallery columns-2 is-cropped alignwide wp-block-gallery-1 is-layout-flex">
<ul class="wp-block-gallery columns-2 is-cropped alignwide wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<li class="blocks-gallery-item">
<figure>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="title" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<figure class="wp-block-gallery columns-3 is-cropped alignwide wp-block-gallery-1 is-layout-flex">
<figure class="wp-block-gallery columns-3 is-cropped alignwide wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<ul class="blocks-gallery-grid">
<li class="blocks-gallery-item">
<figure>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<figure class="wp-block-gallery columns-3 is-cropped wp-block-gallery-1 is-layout-flex">
<figure class="wp-block-gallery columns-3 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<ul class="blocks-gallery-grid">
<li class="blocks-gallery-item">
<figure>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<figure class="wp-block-gallery columns-3 is-cropped wp-block-gallery-1 is-layout-flex">
<figure class="wp-block-gallery columns-3 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<ul class="blocks-gallery-grid">
<li class="blocks-gallery-item">
<figure>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

<figure class="wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex">
<figure class="wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="wp-block-image size-large"><a href="http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg"><img data-id="705" src="http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg" alt="" class="wp-image-705"/></a></figure>
<figure class="wp-block-image size-large"><a href="http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg"><img data-id="704" src="http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg" alt="" class="wp-image-704"/></a></figure>
<figure class="wp-block-image size-large"><a href="http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg"><img data-id="703" src="http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg" alt="" class="wp-image-703"/></a></figure>
</figure>


<figure class="wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex">
<figure class="wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="wp-block-image size-large"><a href="http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg"><img data-id="705" src="http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1-682x1024.jpg" alt="" class="wp-image-705"/></a></figure>
<figure class="wp-block-image size-large"><a href="http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg"><img data-id="704" src="http://wptest.local/wp-content/uploads/2020/09/test-image-edited-1024x682.jpg" alt="" class="wp-image-704"/></a></figure>
<figure class="wp-block-image size-large"><a href="http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg"><img data-id="703" src="http://wptest.local/wp-content/uploads/2020/04/test-image-1024x683.jpg" alt="" class="wp-image-703"/></a></figure>
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function data_layout_support_flag_renders_classnames_on_wrapper() {
),
),
),
'expected_output' => '<div class="wp-block-group is-layout-flow"></div>',
'expected_output' => '<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow"></div>',
),
'single wrapper block layout with constrained type' => array(
'args' => array(
Expand All @@ -226,7 +226,7 @@ public function data_layout_support_flag_renders_classnames_on_wrapper() {
),
),
),
'expected_output' => '<div class="wp-block-group is-layout-constrained"></div>',
'expected_output' => '<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained"></div>',
),
'multiple wrapper block layout with flow type' => array(
'args' => array(
Expand All @@ -247,7 +247,7 @@ public function data_layout_support_flag_renders_classnames_on_wrapper() {
),
),
),
'expected_output' => '<div class="wp-block-group"><div class="wp-block-group__inner-wrapper is-layout-flow"></div></div>',
'expected_output' => '<div class="wp-block-group"><div class="wp-block-group__inner-wrapper is-layout-flow wp-block-group-is-layout-flow"></div></div>',
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -3863,7 +3863,7 @@ public function test_wp_filter_content_tags_does_not_lazy_load_first_image_in_bl
$_wp_current_template_content = '<!-- wp:post-content /-->';

$html = get_the_block_template_html();
$this->assertSame( '<div class="wp-site-blocks"><div class="entry-content wp-block-post-content is-layout-flow">' . $expected_content . '</div></div>', $html );
$this->assertSame( '<div class="wp-site-blocks"><div class="entry-content wp-block-post-content is-layout-flow wp-block-post-content-is-layout-flow">' . $expected_content . '</div></div>', $html );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here @ticket 58548

}

/**
Expand Down Expand Up @@ -3918,7 +3918,7 @@ static function( $attr ) {
$_wp_current_template_content = '<!-- wp:post-featured-image /--> <!-- wp:post-content /-->';

$html = get_the_block_template_html();
$this->assertSame( '<div class="wp-site-blocks">' . $expected_featured_image . ' <div class="entry-content wp-block-post-content is-layout-flow">' . $expected_content . '</div></div>', $html );
$this->assertSame( '<div class="wp-site-blocks">' . $expected_featured_image . ' <div class="entry-content wp-block-post-content is-layout-flow wp-block-post-content-is-layout-flow">' . $expected_content . '</div></div>', $html );
}

/**
Expand Down
Loading