Make WordPress Core

Changeset 52329


Ignore:
Timestamp:
12/06/2021 10:40:45 PM (3 years ago)
Author:
hellofromTonya
Message:

Editor: Allow theme.json to take precedence in setting the root level element's reset margin value.

Any margin added to the root element via theme.json is overwritten by a CSS reset margin value provided by the Gutenberg Plugin.

This commit makes theme.json setting take precedence by generating the global styles body reset prior to processing the theme.json.

Follow-up to [52049].

Props get_dave, hellofromTonya.
Fixes #54550.

Location:
trunk
Files:
2 edited

Legend:

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

    r52321 r52329  
    356356     * @since 5.8.0
    357357     *
    358      * @param array $theme_json A structure that follows the theme.json schema.
    359      * @param string $origin    Optional. What source of data this object represents.
    360      *                          One of 'default', 'theme', or 'custom'. Default 'theme'.
     358     * @param array  $theme_json A structure that follows the theme.json schema.
     359     * @param string $origin     Optional. What source of data this object represents.
     360     *                           One of 'default', 'theme', or 'custom'. Default 'theme'.
    361361     */
    362362    public function __construct( $theme_json = array(), $origin = 'theme' ) {
     
    777777            }
    778778
     779            /*
     780             * Reset default browser margin on the root body element.
     781             * This is set on the root selector **before** generating the ruleset
     782             * from the `theme.json`. This is to ensure that if the `theme.json` declares
     783             * `margin` in its `spacing` declaration for the `body` element then these
     784             * user-generated values take precedence in the CSS cascade.
     785             * @link https://github.com/WordPress/gutenberg/issues/36147.
     786             */
     787            if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
     788                $block_rules .= 'body { margin: 0; }';
     789            }
     790
    779791            // 2. Generate the rules that use the general selector.
    780792            $block_rules .= self::to_ruleset( $selector, $declarations );
     
    787799
    788800            if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
    789                 $block_rules .= 'body { margin: 0; }';
    790801                $block_rules .= '.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }';
    791802                $block_rules .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; }';
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r52275 r52329  
    377377        );
    378378
    379         $expected = 'body{--wp--style--block-gap: 1em;}body { margin: 0; }.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); }.wp-block-columns{--wp--style--block-gap: 24px;}';
    380         $this->assertEquals( $expected, $theme_json->get_stylesheet() );
    381         $this->assertEquals( $expected, $theme_json->get_stylesheet( array( 'styles' ) ) );
     379        $expected = 'body { margin: 0; }body{--wp--style--block-gap: 1em;}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); }.wp-block-columns{--wp--style--block-gap: 24px;}';
     380        $this->assertSame( $expected, $theme_json->get_stylesheet() );
     381        $this->assertSame( $expected, $theme_json->get_stylesheet( array( 'styles' ) ) );
    382382    }
    383383
     
    507507
    508508        $variables = 'body{--wp--preset--color--grey: grey;--wp--preset--font-family--small: 14px;--wp--preset--font-family--big: 41px;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}';
    509         $styles    = 'body{color: var(--wp--preset--color--grey);}body { margin: 0; }.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); }a{background-color: #333;color: #111;}.wp-block-group{border-radius: 10px;padding: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.wp-block-image{border-top-left-radius: 10px;border-bottom-right-radius: 1em;margin-bottom: 30px;}';
     509        $styles    = 'body { margin: 0; }body{color: var(--wp--preset--color--grey);}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); }a{background-color: #333;color: #111;}.wp-block-group{border-radius: 10px;padding: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.wp-block-image{border-top-left-radius: 10px;border-bottom-right-radius: 1em;margin-bottom: 30px;}';
    510510        $presets   = '.has-grey-color{color: var(--wp--preset--color--grey) !important;}.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}.has-grey-border-color{border-color: var(--wp--preset--color--grey) !important;}.has-small-font-family{font-family: var(--wp--preset--font-family--small) !important;}.has-big-font-family{font-family: var(--wp--preset--font-family--big) !important;}';
    511511        $all       = $variables . $styles . $presets;
    512         $this->assertEquals( $all, $theme_json->get_stylesheet() );
    513         $this->assertEquals( $styles, $theme_json->get_stylesheet( array( 'styles' ) ) );
    514         $this->assertEquals( $presets, $theme_json->get_stylesheet( array( 'presets' ) ) );
    515         $this->assertEquals( $variables, $theme_json->get_stylesheet( array( 'variables' ) ) );
     512        $this->assertSame( $all, $theme_json->get_stylesheet() );
     513        $this->assertSame( $styles, $theme_json->get_stylesheet( array( 'styles' ) ) );
     514        $this->assertSame( $presets, $theme_json->get_stylesheet( array( 'presets' ) ) );
     515        $this->assertSame( $variables, $theme_json->get_stylesheet( array( 'variables' ) ) );
    516516    }
    517517
Note: See TracChangeset for help on using the changeset viewer.