Make WordPress Core


Ignore:
Timestamp:
05/03/2024 04:45:20 AM (7 months ago)
Author:
isabel_brison
Message:

Editor: add Style Engine support for nested CSS rules.

Adds support for passing a $rules_group string to wp_style_engine_get_stylesheet_from_css_rules(), so rules can be nested under a media query, layer or other rule.

Props isabel_brison, ramonopoly.
Fixes #61099.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/style-engine/class-wp-style-engine-css-rule.php

    r55819 r58089  
    3737
    3838    /**
     39     * A parent CSS selector in the case of nested CSS, or a CSS nested @rule,
     40     * such as `@media (min-width: 80rem)` or `@layer module`.
     41     *
     42     * @since 6.6.0
     43     * @var string
     44     */
     45    protected $rules_group;
     46
     47    /**
    3948     * Constructor.
    4049     *
    4150     * @since 6.1.0
     51     * @since 6.6.0 Added the `$rules_group` parameter.
    4252     *
    4353     * @param string                                    $selector     Optional. The CSS selector. Default empty string.
     
    4656     *                                                                or a WP_Style_Engine_CSS_Declarations object.
    4757     *                                                                Default empty array.
     58     * @param string                                    $rules_group  A parent CSS selector in the case of nested CSS, or a CSS nested @rule,
     59     *                                                                such as `@media (min-width: 80rem)` or `@layer module`.
    4860     */
    49     public function __construct( $selector = '', $declarations = array() ) {
     61    public function __construct( $selector = '', $declarations = array(), $rules_group = '' ) {
    5062        $this->set_selector( $selector );
    5163        $this->add_declarations( $declarations );
     64        $this->set_rules_group( $rules_group );
    5265    }
    5366
     
    91104
    92105    /**
     106     * Sets the rules group.
     107     *
     108     * @since 6.6.0
     109     *
     110     * @param string $rules_group A parent CSS selector in the case of nested CSS, or a CSS nested @rule,
     111     *                            such as `@media (min-width: 80rem)` or `@layer module`.
     112     * @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
     113     */
     114    public function set_rules_group( $rules_group ) {
     115        $this->rules_group = $rules_group;
     116        return $this;
     117    }
     118
     119    /**
     120     * Gets the rules group.
     121     *
     122     * @since 6.6.0
     123     *
     124     * @return string
     125     */
     126    public function get_rules_group() {
     127        return $this->rules_group;
     128    }
     129
     130    /**
    93131     * Gets the declarations object.
    94132     *
     
    116154     *
    117155     * @since 6.1.0
     156     * @since 6.6.0 Added support for nested CSS with rules groups.
    118157     *
    119158     * @param bool $should_prettify Optional. Whether to add spacing, new lines and indents.
     
    124163     */
    125164    public function get_css( $should_prettify = false, $indent_count = 0 ) {
    126         $rule_indent         = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
    127         $declarations_indent = $should_prettify ? $indent_count + 1 : 0;
    128         $suffix              = $should_prettify ? "\n" : '';
    129         $spacer              = $should_prettify ? ' ' : '';
    130         $selector            = $should_prettify ? str_replace( ',', ",\n", $this->get_selector() ) : $this->get_selector();
    131         $css_declarations    = $this->declarations->get_declarations_string( $should_prettify, $declarations_indent );
     165        $rule_indent                = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
     166        $nested_rule_indent         = $should_prettify ? str_repeat( "\t", $indent_count + 1 ) : '';
     167        $declarations_indent        = $should_prettify ? $indent_count + 1 : 0;
     168        $nested_declarations_indent = $should_prettify ? $indent_count + 2 : 0;
     169        $suffix                     = $should_prettify ? "\n" : '';
     170        $spacer                     = $should_prettify ? ' ' : '';
     171        // Trims any multiple selectors strings.
     172        $selector         = $should_prettify ? implode( ',', array_map( 'trim', explode( ',', $this->get_selector() ) ) ) : $this->get_selector();
     173        $selector         = $should_prettify ? str_replace( array( ',' ), ",\n", $selector ) : $selector;
     174        $rules_group      = $this->get_rules_group();
     175        $has_rules_group  = ! empty( $rules_group );
     176        $css_declarations = $this->declarations->get_declarations_string( $should_prettify, $has_rules_group ? $nested_declarations_indent : $declarations_indent );
    132177
    133178        if ( empty( $css_declarations ) ) {
     
    135180        }
    136181
     182        if ( $has_rules_group ) {
     183            $selector = "{$rule_indent}{$rules_group}{$spacer}{{$suffix}{$nested_rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$nested_rule_indent}}{$suffix}{$rule_indent}}";
     184            return $selector;
     185        }
     186
    137187        return "{$rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$rule_indent}}";
    138188    }
Note: See TracChangeset for help on using the changeset viewer.