Make WordPress Core


Ignore:
Timestamp:
07/14/2026 02:16:06 AM (4 months ago)
Author:
ramonopoly
Message:

Block Supports: Add background gradient support that can combine with background image

Adds background.gradient as a new block support, enabling blocks to combine a background gradient with a background image via comma-separated background-image CSS values (e.g., background-image: linear-gradient(...), url(...)).

Before, the only way to apply a gradient is through color.gradient, which renders as a background CSS shorthand and conflicts with background image
properties. By introducing background.gradient (stored at style.background.gradient), gradients become part of the background style group, allowing the style engine to combine gradient and image values without conflict.

Props aaronrobertshaw, ramonopoly, wildworks.

Fixes #64974.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php

    r61008 r62718  
    7070         * @ticket 61720
    7171         * @ticket 61858
     72         * @ticket 64974
    7273         *
    7374         * @covers ::wp_render_background_support
    … …  
    126127        public function data_background_block_support() {
    127128                return array(
    128                         'background image style is applied' => array(
     129                        'background image style is applied'      => array(
    129130                                'theme_name'          => 'block-theme-child-with-fluid-typography',
    130131                                'block_name'          => 'test/background-rules-are-output',
    … …  
    200201                                'wrapper'             => '<div classname="wp-block-test" style>Content</div>',
    201202                        ),
     203                        'background gradient style is applied'   => array(
     204                                'theme_name'          => 'block-theme-child-with-fluid-typography',
     205                                'block_name'          => 'test/background-gradient-rules-are-output',
     206                                'background_settings' => array(
     207                                        'gradient' => true,
     208                                ),
     209                                'background_style'    => array(
     210                                        'gradient' => 'linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%)',
     211                                ),
     212                                'expected_wrapper'    => '<div class="has-background" style="background-image:linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%);">Content</div>',
     213                                'wrapper'             => '<div>Content</div>',
     214                        ),
     215                        'background gradient style is not applied if the block does not support it' => array(
     216                                'theme_name'          => 'block-theme-child-with-fluid-typography',
     217                                'block_name'          => 'test/background-gradient-rules-are-not-output',
     218                                'background_settings' => array(
     219                                        'gradient' => false,
     220                                ),
     221                                'background_style'    => array(
     222                                        'gradient' => 'linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%)',
     223                                ),
     224                                'expected_wrapper'    => '<div>Content</div>',
     225                                'wrapper'             => '<div>Content</div>',
     226                        ),
     227                        'background gradient style with preset slug is applied' => array(
     228                                'theme_name'          => 'block-theme-child-with-fluid-typography',
     229                                'block_name'          => 'test/background-gradient-preset-slug',
     230                                'background_settings' => array(
     231                                        'gradient' => true,
     232                                ),
     233                                'background_style'    => array(
     234                                        'gradient' => 'var:preset|gradient|vivid-cyan-blue',
     235                                ),
     236                                'expected_wrapper'    => '<div class="has-background" style="background-image:var(--wp--preset--gradient--vivid-cyan-blue);">Content</div>',
     237                                'wrapper'             => '<div>Content</div>',
     238                        ),
     239                        'background gradient and image combined' => array(
     240                                'theme_name'          => 'block-theme-child-with-fluid-typography',
     241                                'block_name'          => 'test/background-gradient-and-image-combined',
     242                                'background_settings' => array(
     243                                        'backgroundImage' => true,
     244                                        'gradient'        => true,
     245                                ),
     246                                'background_style'    => array(
     247                                        'backgroundImage' => array(
     248                                                'url' => 'https://example.com/image.jpg',
     249                                        ),
     250                                        'gradient'        => 'linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%)',
     251                                ),
     252                                'expected_wrapper'    => '<div class="has-background" style="background-image:linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%), url(&apos;https://example.com/image.jpg&apos;);background-size:cover;">Content</div>',
     253                                'wrapper'             => '<div>Content</div>',
     254                        ),
     255                        'background gradient with hsl colors and image combined' => array(
     256                                'theme_name'          => 'block-theme-child-with-fluid-typography',
     257                                'block_name'          => 'test/background-gradient-hsl-and-image',
     258                                'background_settings' => array(
     259                                        'backgroundImage' => true,
     260                                        'gradient'        => true,
     261                                ),
     262                                'background_style'    => array(
     263                                        'backgroundImage' => array(
     264                                                'url' => 'https://example.com/image.jpg',
     265                                        ),
     266                                        'gradient'        => 'linear-gradient(135deg,hsl(0,100%,50%) 0%,hsl(240,100%,50%) 100%)',
     267                                ),
     268                                'expected_wrapper'    => '<div class="has-background" style="background-image:linear-gradient(135deg,hsl(0,100%,50%) 0%,hsl(240,100%,50%) 100%), url(&apos;https://example.com/image.jpg&apos;);background-size:cover;">Content</div>',
     269                                'wrapper'             => '<div>Content</div>',
     270                        ),
    202271                        'background image style is not applied if the block does not support background image' => array(
    203272                                'theme_name'          => 'block-theme-child-with-fluid-typography',
    … …  
    216285                );
    217286        }
     287
     288        /**
     289         * Tests that gradient background CSS values pass KSES.
     290         *
     291         * safecss_filter_attr() removes each url() and gradient from its safety test
     292         * string, so gradients that use functions beyond rgb()/rgba(), and gradients
     293         * combined with a url() background image (in either order), survive
     294         * sanitization.
     295         *
     296         * @ticket 64974
     297         *
     298         * @covers ::safecss_filter_attr
     299         *
     300         * @dataProvider data_background_gradient_values_pass_kses
     301         *
     302         * @param string $css The CSS declaration to test.
     303         */
     304        public function test_background_gradient_values_pass_kses( $css ) {
     305                $result = safecss_filter_attr( $css );
     306                $this->assertNotEmpty( $result, "Expected CSS to be allowed: $css" );
     307                $this->assertStringContainsString( 'background-image', $result );
     308        }
     309
     310        /**
     311         * Data provider for gradient background-image KSES tests.
     312         *
     313         * @return array[]
     314         */
     315        public function data_background_gradient_values_pass_kses() {
     316                return array(
     317                        'gradient first with rgb colors'     => array(
     318                                'background-image: linear-gradient(135deg, rgb(255,0,0) 0%, rgb(0,0,255) 100%), url(https://example.com/image.jpg)',
     319                        ),
     320                        'url first with rgb colors'          => array(
     321                                'background-image: url(https://example.com/image.jpg), linear-gradient(135deg, rgb(255,0,0) 0%, rgb(0,0,255) 100%)',
     322                        ),
     323                        'gradient first with rgba colors'    => array(
     324                                'background-image: linear-gradient(135deg, rgba(0,0,0,0.8) 0%, rgba(255,255,255,0.2) 100%), url(https://example.com/image.jpg)',
     325                        ),
     326                        'gradient first with hsl colors'     => array(
     327                                'background-image: linear-gradient(135deg, hsl(0, 100%, 50%) 0%, hsl(240, 100%, 50%) 100%), url(https://example.com/image.jpg)',
     328                        ),
     329                        'gradient first with hsla colors'    => array(
     330                                'background-image: linear-gradient(135deg, hsla(0, 100%, 50%, 0.8) 0%, hsla(240, 100%, 50%, 0.5) 100%), url(https://example.com/image.jpg)',
     331                        ),
     332                        'gradient first with oklch colors'   => array(
     333                                'background-image: linear-gradient(135deg, oklch(0.7 0.15 30) 0%, oklch(0.5 0.2 260) 100%), url(https://example.com/image.jpg)',
     334                        ),
     335                        'gradient first with lab colors'     => array(
     336                                'background-image: linear-gradient(135deg, lab(50% 40 59.5) 0%, lab(70% -45 0) 100%), url(https://example.com/image.jpg)',
     337                        ),
     338                        'radial gradient with url'           => array(
     339                                'background-image: radial-gradient(circle, rgb(255,0,0) 0%, rgb(0,0,255) 100%), url(https://example.com/image.jpg)',
     340                        ),
     341                        'conic gradient with url'            => array(
     342                                'background-image: conic-gradient(rgb(255,0,0), rgb(0,0,255)), url(https://example.com/image.jpg)',
     343                        ),
     344                        'repeating-linear gradient with url' => array(
     345                                'background-image: repeating-linear-gradient(45deg, rgb(255,0,0) 0px, rgb(0,0,255) 40px), url(https://example.com/image.jpg)',
     346                        ),
     347                        'var preset gradient first with url' => array(
     348                                'background-image: var(--wp--preset--gradient--vivid-cyan-blue), url(https://example.com/image.jpg)',
     349                        ),
     350                        'url first with var preset gradient' => array(
     351                                'background-image: url(https://example.com/image.jpg), var(--wp--preset--gradient--vivid-cyan-blue)',
     352                        ),
     353                        'gradient with hex colors'           => array(
     354                                'background-image: linear-gradient(135deg, #ff0000 0%, #0000ff 100%), url(https://example.com/image.jpg)',
     355                        ),
     356                        'standalone hsl gradient'            => array(
     357                                'background-image: linear-gradient(135deg, hsl(0, 100%, 50%) 0%, hsl(240, 100%, 50%) 100%)',
     358                        ),
     359                        'standalone oklch gradient'          => array(
     360                                'background-image: linear-gradient(oklch(0.7 0.15 30), oklch(0.5 0.2 260))',
     361                        ),
     362                        'standalone gradient with calc'      => array(
     363                                'background-image: linear-gradient(red 0%, blue calc(50% + 10px))',
     364                        ),
     365                );
     366        }
    218367}
Note: See TracChangeset for help on using the changeset viewer.