Make WordPress Core


Ignore:
Timestamp:
05/30/2024 01:14:15 AM (12 months ago)
Author:
isabel_brison
Message:

Editor: level global styles specificity at 0-1-0.

Adjusts the block level global styles selectors so they have a consistent specificity of 0-1-0, and adjusts base and layout global style selectors to their minimum required specificity.

Props aaronrobertshaw, isabel_brison, andrewserong, mukesh27.
Fixes #61165.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r58228 r58241  
    12611261     *
    12621262     * @since 6.2.0
     1263     * @since 6.6.0 Enforced 0-1-0 specificity for block custom CSS selectors.
    12631264     *
    12641265     * @param string $css      The CSS to process.
     
    12751276            if ( $is_root_css ) {
    12761277                // If the part doesn't contain braces, it applies to the root level.
    1277                 $processed_css .= trim( $selector ) . '{' . trim( $part ) . '}';
     1278                $processed_css .= ':root :where(' . trim( $selector ) . '){' . trim( $part ) . '}';
    12781279            } else {
    12791280                // If the part contains braces, it's a nested CSS rule.
     
    12871288                    ? static::scope_selector( $selector, $nested_selector )
    12881289                    : static::append_to_selector( $selector, $nested_selector );
    1289                 $processed_css  .= $part_selector . '{' . trim( $css_value ) . '}';
    1290             }
     1290                $final_selector  = ":root :where($part_selector)";
     1291                $processed_css  .= $final_selector . '{' . trim( $css_value ) . '}';}
    12911292        }
    12921293        return $processed_css;
     
    14091410     * @since 6.5.1 Only output rules referencing content and wide sizes when values exist.
    14101411     * @since 6.5.3 Add types parameter to check if only base layout styles are needed.
     1412     * @since 6.6.0 Updated layout style specificity to be compatible with overall 0-1-0 specificity in global styles.
    14111413     *
    14121414     * @param array $block_metadata Metadata about the block to get styles for.
     
    14351437        $node                     = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
    14361438        $layout_definitions       = wp_get_layout_definitions();
    1437         $layout_selector_pattern  = '/^[a-zA-Z0-9\-\.\ *+>:\(\)]*$/'; // Allow alphanumeric classnames, spaces, wildcard, sibling, child combinator and pseudo class selectors.
     1439        $layout_selector_pattern  = '/^[a-zA-Z0-9\-\.\,\ *+>:\(\)]*$/'; // Allow alphanumeric classnames, spaces, wildcard, sibling, child combinator and pseudo class selectors.
    14381440
    14391441        /*
     
    15101512                                    );
    15111513                                } else {
    1512                                     $format          = static::ROOT_BLOCK_SELECTOR === $selector ? ':where(%s .%s) %s' : '%s-%s%s';
     1514                                    $format          = static::ROOT_BLOCK_SELECTOR === $selector ? '.%2$s %3$s' : '%1$s-%2$s %3$s';
    15131515                                    $layout_selector = sprintf(
    15141516                                        $format,
     
    15941596
    15951597                            $layout_selector = sprintf(
    1596                                 '%s .%s%s',
    1597                                 $selector,
     1598                                '.%s%s',
    15981599                                $class_name,
    15991600                                $base_style_rule['selector']
     
    25022503     * @since 6.1.0
    25032504     * @since 6.6.0 Setting a min-height of HTML when root styles have a background gradient or image.
     2505     *              Updated general global styles specificity to 0-1-0.
    25042506     *
    25052507     * @param array $block_metadata Metadata about the block to get styles for.
     
    26512653
    26522654        // 2. Generate and append the rules that use the general selector.
    2653         $block_rules .= static::to_ruleset( $selector, $declarations );
     2655        $block_rules .= static::to_ruleset( ":root :where($selector)", $declarations );
    26542656
    26552657        // 3. Generate and append the rules that use the duotone selector.
     
    26682670        // 5. Generate and append the feature level rulesets.
    26692671        foreach ( $feature_declarations as $feature_selector => $individual_feature_declarations ) {
    2670             $block_rules .= static::to_ruleset( $feature_selector, $individual_feature_declarations );
     2672            $block_rules .= static::to_ruleset( ":root :where($feature_selector)", $individual_feature_declarations );
    26712673        }
    26722674
    26732675        // 6. Generate and append the style variation rulesets.
    26742676        foreach ( $style_variation_declarations as $style_variation_selector => $individual_style_variation_declarations ) {
    2675             $block_rules .= static::to_ruleset( $style_variation_selector, $individual_style_variation_declarations );
     2677            $block_rules .= static::to_ruleset( ":root :where($style_variation_selector)", $individual_style_variation_declarations );
    26762678        }
    26772679
     
    26842686     * @since 6.1.0
    26852687     * @since 6.6.0 Use `ROOT_CSS_PROPERTIES_SELECTOR` for CSS custom properties and improved consistency of root padding rules.
     2688     *              Updated specificity of body margin reset and first/last child selectors.
    26862689     *
    26872690     * @param string $selector The root node selector.
     
    27152718        * @link https://github.com/WordPress/gutenberg/issues/36147.
    27162719        */
    2717         $css .= 'body { margin: 0; }';
     2720        $css .= ':where(body) { margin: 0; }';
    27182721
    27192722        if ( $use_root_padding ) {
     
    27382741            $block_gap_value = static::get_property_value( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ) );
    27392742            $css            .= ":where(.wp-site-blocks) > * { margin-block-start: $block_gap_value; margin-block-end: 0; }";
    2740             $css            .= ':where(.wp-site-blocks) > :first-child:first-child { margin-block-start: 0; }';
    2741             $css            .= ':where(.wp-site-blocks) > :last-child:last-child { margin-block-end: 0; }';
     2743            $css            .= ':where(.wp-site-blocks) > :first-child { margin-block-start: 0; }';
     2744            $css            .= ':where(.wp-site-blocks) > :last-child { margin-block-end: 0; }';
    27422745
    27432746            // For backwards compatibility, ensure the legacy block gap CSS variable is still available.
Note: See TracChangeset for help on using the changeset viewer.