Make WordPress Core


Ignore:
Timestamp:
05/03/2024 04:45:20 AM (3 years 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/tests/phpunit/tests/style-engine/wpStyleEngineProcessor.php

    r56574 r58089  
    5050
    5151        /**
     52         * Tests adding nested rules with at-rules and returning compiled CSS rules.
     53         *
     54         * @ticket 61099
     55         *
     56         * @covers ::add_rules
     57         * @covers ::get_css
     58         */
     59        public function test_should_return_nested_rules_as_compiled_css() {
     60                $a_nice_css_rule = new WP_Style_Engine_CSS_Rule( '.a-nice-rule' );
     61                $a_nice_css_rule->add_declarations(
     62                        array(
     63                                'color'            => 'var(--nice-color)',
     64                                'background-color' => 'purple',
     65                        )
     66                );
     67                $a_nice_css_rule->set_rules_group( '@media (min-width: 80rem)' );
     68
     69                $a_nicer_css_rule = new WP_Style_Engine_CSS_Rule( '.a-nicer-rule' );
     70                $a_nicer_css_rule->add_declarations(
     71                        array(
     72                                'font-family'      => 'Nice sans',
     73                                'font-size'        => '1em',
     74                                'background-color' => 'purple',
     75                        )
     76                );
     77                $a_nicer_css_rule->set_rules_group( '@layer nicety' );
     78
     79                $a_nice_processor = new WP_Style_Engine_Processor();
     80                $a_nice_processor->add_rules( array( $a_nice_css_rule, $a_nicer_css_rule ) );
     81
     82                $this->assertSame(
     83                        '@media (min-width: 80rem){.a-nice-rule{color:var(--nice-color);background-color:purple;}}@layer nicety{.a-nicer-rule{font-family:Nice sans;font-size:1em;background-color:purple;}}',
     84                        $a_nice_processor->get_css( array( 'prettify' => false ) )
     85                );
     86        }
     87
     88        /**
    5289         * Tests compiling CSS rules and formatting them with new lines and indents.
    5390         *
    … …  
    97134';
    98135                $this->assertSameIgnoreEOL(
     136                        $expected,
     137                        $a_wonderful_processor->get_css( array( 'prettify' => true ) )
     138                );
     139        }
     140
     141        /**
     142         * Tests compiling nested CSS rules and formatting them with new lines and indents.
     143         *
     144         * @ticket 61099
     145         *
     146         * @covers ::get_css
     147         */
     148        public function test_should_return_prettified_nested_css_rules() {
     149                $a_wonderful_css_rule = new WP_Style_Engine_CSS_Rule( '.a-wonderful-rule' );
     150                $a_wonderful_css_rule->add_declarations(
     151                        array(
     152                                'color'            => 'var(--wonderful-color)',
     153                                'background-color' => 'orange',
     154                        )
     155                );
     156                $a_wonderful_css_rule->set_rules_group( '@media (min-width: 80rem)' );
     157
     158                $a_very_wonderful_css_rule = new WP_Style_Engine_CSS_Rule( '.a-very_wonderful-rule' );
     159                $a_very_wonderful_css_rule->add_declarations(
     160                        array(
     161                                'color'            => 'var(--wonderful-color)',
     162                                'background-color' => 'orange',
     163                        )
     164                );
     165                $a_very_wonderful_css_rule->set_rules_group( '@layer wonderfulness' );
     166
     167                $a_wonderful_processor = new WP_Style_Engine_Processor();
     168                $a_wonderful_processor->add_rules( array( $a_wonderful_css_rule, $a_very_wonderful_css_rule ) );
     169
     170                $expected = '@media (min-width: 80rem) {
     171        .a-wonderful-rule {
     172                color: var(--wonderful-color);
     173                background-color: orange;
     174        }
     175}
     176@layer wonderfulness {
     177        .a-very_wonderful-rule {
     178                color: var(--wonderful-color);
     179                background-color: orange;
     180        }
     181}
     182';
     183                $this->assertSame(
    99184                        $expected,
    100185                        $a_wonderful_processor->get_css( array( 'prettify' => true ) )
Note: See TracChangeset for help on using the changeset viewer.