Skip to content

Commit 5f4cddb

Browse files
Reduce specificity of global styles and layout selectors.
1 parent 08b4dd3 commit 5f4cddb

File tree

3 files changed

+303
-19
lines changed

3 files changed

+303
-19
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ function wp_get_layout_definitions() {
5151
),
5252
'spacingStyles' => array(
5353
array(
54-
'selector' => ' > :first-child:first-child',
54+
'selector' => ' > :first-child',
5555
'rules' => array(
5656
'margin-block-start' => '0',
5757
),
5858
),
5959
array(
60-
'selector' => ' > :last-child:last-child',
60+
'selector' => ' > :last-child',
6161
'rules' => array(
6262
'margin-block-end' => '0',
6363
),
@@ -116,13 +116,13 @@ function wp_get_layout_definitions() {
116116
),
117117
'spacingStyles' => array(
118118
array(
119-
'selector' => ' > :first-child:first-child',
119+
'selector' => ' > :first-child',
120120
'rules' => array(
121121
'margin-block-start' => '0',
122122
),
123123
),
124124
array(
125-
'selector' => ' > :last-child:last-child',
125+
'selector' => ' > :last-child',
126126
'rules' => array(
127127
'margin-block-end' => '0',
128128
),
@@ -150,7 +150,7 @@ function wp_get_layout_definitions() {
150150
),
151151
),
152152
array(
153-
'selector' => ' > *',
153+
'selector' => ' > :is(*, div)', // :is(*, div) instead of just * increases the specificity by 001.
154154
'rules' => array(
155155
'margin' => '0',
156156
),
@@ -172,7 +172,7 @@ function wp_get_layout_definitions() {
172172
'displayMode' => 'grid',
173173
'baseStyles' => array(
174174
array(
175-
'selector' => ' > *',
175+
'selector' => ' > :is(*, div)', // :is(*, div) instead of just * increases the specificity by 001.
176176
'rules' => array(
177177
'margin' => '0',
178178
),
@@ -261,7 +261,7 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
261261
),
262262
),
263263
array(
264-
'selector' => "$selector$selector > * + *",
264+
'selector' => "$selector > * + *",
265265
'declarations' => array(
266266
'margin-block-start' => $gap_value,
267267
'margin-block-end' => '0',
@@ -370,7 +370,7 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
370370
),
371371
),
372372
array(
373-
'selector' => "$selector$selector > * + *",
373+
'selector' => "$selector > * + *",
374374
'declarations' => array(
375375
'margin-block-start' => $gap_value,
376376
'margin-block-end' => '0',
@@ -735,7 +735,7 @@ function wp_render_layout_support_flag( $block_content, $block ) {
735735
$has_block_gap_support = isset( $block_gap );
736736

737737
$style = wp_get_layout_style(
738-
".$container_class.$container_class",
738+
".$container_class",
739739
$used_layout,
740740
$has_block_gap_support,
741741
$gap_value,

src/wp-includes/class-wp-theme-json.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ protected function get_layout_styles( $block_metadata, $types = array() ) {
14241424
$has_fallback_gap_support = ! $has_block_gap_support; // This setting isn't useful yet: it exists as a placeholder for a future explicit fallback gap styles support.
14251425
$node = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
14261426
$layout_definitions = wp_get_layout_definitions();
1427-
$layout_selector_pattern = '/^[a-zA-Z0-9\-\.\ *+>:\(\)]*$/'; // Allow alphanumeric classnames, spaces, wildcard, sibling, child combinator and pseudo class selectors.
1427+
$layout_selector_pattern = '/^[a-zA-Z0-9\-\.\,\ *+>:\(\)]*$/'; // Allow alphanumeric classnames, spaces, wildcard, sibling, child combinator and pseudo class selectors.
14281428

14291429
/*
14301430
* Gap styles will only be output if the theme has block gap support, or supports a fallback gap.
@@ -1499,7 +1499,7 @@ protected function get_layout_styles( $block_metadata, $types = array() ) {
14991499
$spacing_rule['selector']
15001500
);
15011501
} else {
1502-
$format = static::ROOT_BLOCK_SELECTOR === $selector ? ':where(%s .%s) %s' : '%s-%s%s';
1502+
$format = static::ROOT_BLOCK_SELECTOR === $selector ? ':where(.%2$s) %3$s' : ':where(%1$s-%2$s) %3$s';
15031503
$layout_selector = sprintf(
15041504
$format,
15051505
$selector,
@@ -1583,7 +1583,7 @@ protected function get_layout_styles( $block_metadata, $types = array() ) {
15831583
}
15841584

15851585
$layout_selector = sprintf(
1586-
'%s .%s%s',
1586+
'.%s%s',
15871587
$selector,
15881588
$class_name,
15891589
$base_style_rule['selector']
@@ -2591,7 +2591,7 @@ static function ( $pseudo_selector ) use ( $selector ) {
25912591
}
25922592

25932593
// 2. Generate and append the rules that use the general selector.
2594-
$block_rules .= static::to_ruleset( $selector, $declarations );
2594+
$block_rules .= static::to_ruleset( ":where($selector)", $declarations );
25952595

25962596
// 3. Generate and append the rules that use the duotone selector.
25972597
if ( isset( $block_metadata['duotone'] ) && ! empty( $declarations_duotone ) ) {
@@ -2608,7 +2608,7 @@ static function ( $pseudo_selector ) use ( $selector ) {
26082608

26092609
// 5. Generate and append the feature level rulesets.
26102610
foreach ( $feature_declarations as $feature_selector => $individual_feature_declarations ) {
2611-
$block_rules .= static::to_ruleset( $feature_selector, $individual_feature_declarations );
2611+
$block_rules .= static::to_ruleset( ":where($feature_selector)", $individual_feature_declarations );
26122612
}
26132613

26142614
// 6. Generate and append the style variation rulesets.
@@ -2643,8 +2643,8 @@ public function get_root_layout_rules( $selector, $block_metadata ) {
26432643
$content_size = static::is_safe_css_declaration( 'max-width', $content_size ) ? $content_size : 'initial';
26442644
$wide_size = isset( $settings['layout']['wideSize'] ) ? $settings['layout']['wideSize'] : $settings['layout']['contentSize'];
26452645
$wide_size = static::is_safe_css_declaration( 'max-width', $wide_size ) ? $wide_size : 'initial';
2646-
$css .= static::ROOT_CSS_PROPERTIES_SELECTOR . ' { --wp--style--global--content-size: ' . $content_size . ';';
2647-
$css .= '--wp--style--global--wide-size: ' . $wide_size . '; }';
2646+
$css .= static::ROOT_CSS_PROPERTIES_SELECTOR . ' { --wp--style--global--content-size: ' . $content_size . ';';
2647+
$css .= '--wp--style--global--wide-size: ' . $wide_size . '; }';
26482648
}
26492649

26502650
/*
@@ -2655,7 +2655,7 @@ public function get_root_layout_rules( $selector, $block_metadata ) {
26552655
* user-generated values take precedence in the CSS cascade.
26562656
* @link https://github.com/WordPress/gutenberg/issues/36147.
26572657
*/
2658-
$css .= 'body { margin: 0; }';
2658+
$css .= ':where(body) { margin: 0; }';
26592659

26602660
if ( $use_root_padding ) {
26612661
// Top and bottom padding are applied to the outer block container.
@@ -2682,8 +2682,8 @@ public function get_root_layout_rules( $selector, $block_metadata ) {
26822682
if ( isset( $this->theme_json['settings']['spacing']['blockGap'] ) ) {
26832683
$block_gap_value = static::get_property_value( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ) );
26842684
$css .= ":where(.wp-site-blocks) > * { margin-block-start: $block_gap_value; margin-block-end: 0; }";
2685-
$css .= ':where(.wp-site-blocks) > :first-child:first-child { margin-block-start: 0; }';
2686-
$css .= ':where(.wp-site-blocks) > :last-child:last-child { margin-block-end: 0; }';
2685+
$css .= ':where(.wp-site-blocks) > :first-child { margin-block-start: 0; }';
2686+
$css .= ':where(.wp-site-blocks) > :last-child { margin-block-end: 0; }';
26872687

26882688
// For backwards compatibility, ensure the legacy block gap CSS variable is still available.
26892689
$css .= static::ROOT_CSS_PROPERTIES_SELECTOR . " { --wp--style--block-gap: $block_gap_value; }";

0 commit comments

Comments
 (0)