Skip to content

Commit f265042

Browse files
Reduce specificity of global styles and layout selectors.
1 parent 473e255 commit f265042

File tree

3 files changed

+301
-17
lines changed

3 files changed

+301
-17
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ protected function get_layout_styles( $block_metadata, $types = array() ) {
14161416
$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.
14171417
$node = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
14181418
$layout_definitions = wp_get_layout_definitions();
1419-
$layout_selector_pattern = '/^[a-zA-Z0-9\-\.\ *+>:\(\)]*$/'; // Allow alphanumeric classnames, spaces, wildcard, sibling, child combinator and pseudo class selectors.
1419+
$layout_selector_pattern = '/^[a-zA-Z0-9\-\.\,\ *+>:\(\)]*$/'; // Allow alphanumeric classnames, spaces, wildcard, sibling, child combinator and pseudo class selectors.
14201420

14211421
/*
14221422
* Gap styles will only be output if the theme has block gap support, or supports a fallback gap.
@@ -1491,7 +1491,7 @@ protected function get_layout_styles( $block_metadata, $types = array() ) {
14911491
$spacing_rule['selector']
14921492
);
14931493
} else {
1494-
$format = static::ROOT_BLOCK_SELECTOR === $selector ? ':where(%s .%s) %s' : '%s-%s%s';
1494+
$format = static::ROOT_BLOCK_SELECTOR === $selector ? ':where(.%2$s) %3$s' : ':where(%1$s-%2$s) %3$s';
14951495
$layout_selector = sprintf(
14961496
$format,
14971497
$selector,
@@ -1575,7 +1575,7 @@ protected function get_layout_styles( $block_metadata, $types = array() ) {
15751575
}
15761576

15771577
$layout_selector = sprintf(
1578-
'%s .%s%s',
1578+
'.%s%s',
15791579
$selector,
15801580
$class_name,
15811581
$base_style_rule['selector']
@@ -2582,7 +2582,7 @@ static function ( $pseudo_selector ) use ( $selector ) {
25822582
}
25832583

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

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

26002600
// 5. Generate and append the feature level rulesets.
26012601
foreach ( $feature_declarations as $feature_selector => $individual_feature_declarations ) {
2602-
$block_rules .= static::to_ruleset( $feature_selector, $individual_feature_declarations );
2602+
$block_rules .= static::to_ruleset( ":where($feature_selector)", $individual_feature_declarations );
26032603
}
26042604

26052605
// 6. Generate and append the style variation rulesets.
@@ -2632,7 +2632,7 @@ public function get_root_layout_rules( $selector, $block_metadata ) {
26322632
* user-generated values take precedence in the CSS cascade.
26332633
* @link https://github.com/WordPress/gutenberg/issues/36147.
26342634
*/
2635-
$css .= 'body { margin: 0;';
2635+
$css .= ':where(body) { margin: 0;';
26362636

26372637
/*
26382638
* If there are content and wide widths in theme.json, output them
@@ -2675,8 +2675,8 @@ public function get_root_layout_rules( $selector, $block_metadata ) {
26752675
if ( $has_block_gap_support ) {
26762676
$block_gap_value = static::get_property_value( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ) );
26772677
$css .= ":where(.wp-site-blocks) > * { margin-block-start: $block_gap_value; margin-block-end: 0; }";
2678-
$css .= ':where(.wp-site-blocks) > :first-child:first-child { margin-block-start: 0; }';
2679-
$css .= ':where(.wp-site-blocks) > :last-child:last-child { margin-block-end: 0; }';
2678+
$css .= ':where(.wp-site-blocks) > :first-child { margin-block-start: 0; }';
2679+
$css .= ':where(.wp-site-blocks) > :last-child { margin-block-end: 0; }';
26802680

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

0 commit comments

Comments
 (0)