Skip to content

Commit 6d8ee2a

Browse files
Editor: Add a background clip block support
Adds a background.backgroundClip block support, so a block can set which box its background is painted into: border-box, padding-box, content-box, or the text itself. Clipping to the text is what gives blocks a gradient text fill. Registers the property in the style engine and theme.json, allows the declarations through KSES, and skips the has-background class when the background is clipped to the text. Props aaronrobertshaw. See #66094.
1 parent ab2373a commit 6d8ee2a

6 files changed

Lines changed: 257 additions & 3 deletions

File tree

‎src/wp-includes/block-supports/background.php‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function wp_register_background_support( $block_type ) {
4444
* @since 6.6.0 Removed requirement for `backgroundImage.source`. A file/url is the default.
4545
* @since 6.7.0 Added support for `backgroundAttachment` output.
4646
* @since 7.1.0 Added support for `background.gradient` output.
47+
* @since 7.2.0 Added support for `background.backgroundClip` output.
4748
*
4849
* @access private
4950
*
@@ -56,9 +57,10 @@ function wp_render_background_support( $block_content, $block ) {
5657
$block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
5758
$has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false );
5859
$has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false );
60+
$has_background_clip_support = block_has_support( $block_type, array( 'background', 'backgroundClip' ), false );
5961

6062
if (
61-
( ! $has_background_image_support && ! $has_background_gradient_support ) ||
63+
( ! $has_background_image_support && ! $has_background_gradient_support && ! $has_background_clip_support ) ||
6264
! isset( $block_attributes['style']['background'] )
6365
) {
6466
return $block_content;
@@ -67,8 +69,9 @@ function wp_render_background_support( $block_content, $block ) {
6769
// Check serialization skip for each feature individually.
6870
$skip_background_image = ! $has_background_image_support || wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' );
6971
$skip_background_gradient = ! $has_background_gradient_support || wp_should_skip_block_supports_serialization( $block_type, 'background', 'gradient' );
72+
$skip_background_clip = ! $has_background_clip_support || wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundClip' );
7073

71-
if ( $skip_background_image && $skip_background_gradient ) {
74+
if ( $skip_background_image && $skip_background_gradient && $skip_background_clip ) {
7275
return $block_content;
7376
}
7477

@@ -95,6 +98,10 @@ function wp_render_background_support( $block_content, $block ) {
9598
$background_styles['gradient'] = $block_attributes['style']['background']['gradient'] ?? null;
9699
}
97100

101+
if ( ! $skip_background_clip ) {
102+
$background_styles['backgroundClip'] = $block_attributes['style']['background']['backgroundClip'] ?? null;
103+
}
104+
98105
$styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
99106

100107
if ( ! empty( $styles['css'] ) ) {
@@ -111,7 +118,19 @@ function wp_render_background_support( $block_content, $block ) {
111118
}
112119

113120
$tags->set_attribute( 'style', $updated_style );
114-
$tags->add_class( 'has-background' );
121+
122+
/*
123+
* A background clipped to the text paints the glyphs rather than the
124+
* block's box, so there is no block background to announce. Read the
125+
* clip from the serialized styles so an unsupported or skipped value
126+
* left in the block's attributes cannot suppress the class.
127+
*/
128+
$background_clip = $background_styles['backgroundClip'] ?? null;
129+
$has_background = ! empty( $background_styles['backgroundImage'] ) || ! empty( $background_styles['gradient'] );
130+
131+
if ( $has_background && 'text' !== $background_clip ) {
132+
$tags->add_class( 'has-background' );
133+
}
115134
}
116135

117136
return $tags->get_updated_html();

‎src/wp-includes/class-wp-theme-json.php‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ class WP_Theme_JSON {
259259
* @since 7.0.0 Added `dimensions.width` and `dimensions.height`.
260260
* Added `text-indent` property.
261261
* @since 7.1.0 Added `min-width` and `text-shadow`.
262+
* @since 7.2.0 Added `background-clip`.
262263
* @var array
263264
*/
264265
const PROPERTIES_METADATA = array(
@@ -270,6 +271,7 @@ class WP_Theme_JSON {
270271
'background-repeat' => array( 'background', 'backgroundRepeat' ),
271272
'background-size' => array( 'background', 'backgroundSize' ),
272273
'background-attachment' => array( 'background', 'backgroundAttachment' ),
274+
'background-clip' => array( 'background', 'backgroundClip' ),
273275
'border-radius' => array( 'border', 'radius' ),
274276
'border-top-left-radius' => array( 'border', 'radius', 'topLeft' ),
275277
'border-top-right-radius' => array( 'border', 'radius', 'topRight' ),
@@ -428,6 +430,7 @@ class WP_Theme_JSON {
428430
* Added support for `dimensions.width` and `dimensions.height`.
429431
* Added support for `typography.textIndent`.
430432
* @since 7.1.0 Added `viewport` property.
433+
* @since 7.2.0 Added `background.backgroundClip` property.
431434
* Added support for `background.gradient`, `dimensions.minWidth` and `blockVisibility.allowEditing`.
432435
* @since 7.2.0 Added support for `typography.textShadow`, `typography.textShadowPresets`,
433436
* and `typography.defaultTextShadowPresets`.
@@ -437,6 +440,7 @@ class WP_Theme_JSON {
437440
'appearanceTools' => null,
438441
'useRootPaddingAwareAlignments' => null,
439442
'background' => array(
443+
'backgroundClip' => null,
440444
'backgroundImage' => null,
441445
'backgroundSize' => null,
442446
'gradient' => null,
@@ -582,10 +586,12 @@ class WP_Theme_JSON {
582586
* @since 7.0.0 Added support for `dimensions.width` and `dimensions.height`.
583587
* @since 7.1.0 Added support for `background.gradient`,`dimensions.minWidth`,
584588
* and `typography.textShadow`.
589+
* @since 7.2.0 Added support for `background.backgroundClip`.
585590
* @var array
586591
*/
587592
const VALID_STYLES = array(
588593
'background' => array(
594+
'backgroundClip' => null,
589595
'backgroundImage' => null,
590596
'backgroundPosition' => null,
591597
'backgroundRepeat' => null,
@@ -3192,6 +3198,34 @@ protected static function compute_style_properties( $styles, $settings = array()
31923198
'name' => $css_property,
31933199
'value' => $value,
31943200
);
3201+
3202+
/*
3203+
* Background clipping needs vendor prefixed properties for
3204+
* cross-browser support. The `text` value clips the background to
3205+
* the text and makes it visible through a transparent fill. The box
3206+
* values only reset the fill color, to cancel any inherited text
3207+
* clipping. `-webkit-background-clip` is an alias of
3208+
* `background-clip` in Chromium, so resetting it would discard the
3209+
* value set above. Values outside the set the style engine accepts
3210+
* get neither, so a typo cannot reset the fill color.
3211+
*/
3212+
if ( 'background-clip' === $css_property ) {
3213+
if ( 'text' === $value ) {
3214+
$declarations[] = array(
3215+
'name' => '-webkit-background-clip',
3216+
'value' => 'text',
3217+
);
3218+
$declarations[] = array(
3219+
'name' => '-webkit-text-fill-color',
3220+
'value' => 'transparent',
3221+
);
3222+
} elseif ( in_array( $value, array( 'border-box', 'padding-box', 'content-box' ), true ) ) {
3223+
$declarations[] = array(
3224+
'name' => '-webkit-text-fill-color',
3225+
'value' => 'currentColor',
3226+
);
3227+
}
3228+
}
31953229
}
31963230

31973231
// If a variable value is added to the root, the corresponding property should be removed.

‎src/wp-includes/kses.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3684,6 +3684,7 @@ function kses_init() {
36843684
* @since 6.6.0 Added support for `grid-column`, `grid-row`, and `container-type`.
36853685
* @since 6.9.0 Added support for `white-space`.
36863686
* @since 7.1.0 Extended gradient support to allow any single-level nested function.
3687+
* @since 7.2.0 Added support for `background-clip`.
36873688
* Added support for transform functions, `clip-path` basic shapes,
36883689
* and URLs in the SVG element reference properties.
36893690
* @since 7.2.0 Added support for CSS anchor positioning properties and `text-shadow`.
@@ -3712,6 +3713,7 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
37123713
*
37133714
* @since 2.8.1
37143715
* @since 7.1.0 Added support for SVG presentation attributes.
3716+
* @since 7.2.0 Added support for `background-clip` and its vendor prefixed companions.
37153717
*
37163718
* @param string[] $attr Array of allowed CSS attributes.
37173719
*/
@@ -3726,6 +3728,9 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
37263728
'background-size',
37273729
'background-attachment',
37283730
'background-blend-mode',
3731+
'background-clip',
3732+
'-webkit-background-clip',
3733+
'-webkit-text-fill-color',
37293734

37303735
'border',
37313736
'border-radius',

‎src/wp-includes/style-engine/class-wp-style-engine.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ final class WP_Style_Engine {
5050
*
5151
* @since 6.1.0
5252
* @since 7.1.0 Added `background.gradient` property.
53+
* @since 7.2.0 Added `background.backgroundClip` property.
5354
* @var array
5455
*/
5556
const BLOCK_STYLE_DEFINITIONS_METADATA = array(
@@ -97,6 +98,10 @@ final class WP_Style_Engine {
9798
'has-background' => true,
9899
),
99100
),
101+
'backgroundClip' => array(
102+
'value_func' => array( self::class, 'get_background_clip_css_declarations' ),
103+
'path' => array( 'background', 'backgroundClip' ),
104+
),
100105
),
101106
'color' => array(
102107
'text' => array(
@@ -768,6 +773,50 @@ protected static function get_url_or_value_css_declaration( $style_value, $style
768773
return $css_declarations;
769774
}
770775

776+
/**
777+
* Style value parser that returns the CSS declarations for background clipping.
778+
*
779+
* For the `text` value the background is clipped to the block's text, which
780+
* requires the vendor prefixed properties and a transparent fill color.
781+
*
782+
* @since 7.2.0
783+
*
784+
* @param string $style_value A single raw style value from $block_styles array.
785+
* @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
786+
* @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
787+
*/
788+
protected static function get_background_clip_css_declarations( $style_value, $style_definition ) {
789+
if ( empty( $style_value ) || ! is_string( $style_value ) ) {
790+
return array();
791+
}
792+
793+
$valid_values = array( 'border-box', 'padding-box', 'content-box', 'text' );
794+
795+
if ( ! in_array( $style_value, $valid_values, true ) ) {
796+
return array();
797+
}
798+
799+
$css_declarations = array(
800+
'background-clip' => $style_value,
801+
);
802+
803+
if ( 'text' === $style_value ) {
804+
$css_declarations['-webkit-background-clip'] = 'text';
805+
$css_declarations['-webkit-text-fill-color'] = 'transparent';
806+
} else {
807+
/*
808+
* Only the fill color is restored. `-webkit-background-clip` is an
809+
* alias of `background-clip` in Chromium, so resetting it here would
810+
* discard the value set above. The fill color is inherited, so it
811+
* needs its initial value rather than `unset`, which would take a
812+
* transparent fill from an ancestor clipping to text.
813+
*/
814+
$css_declarations['-webkit-text-fill-color'] = 'currentColor';
815+
}
816+
817+
return $css_declarations;
818+
}
819+
771820
/**
772821
* Returns compiled CSS from CSS declarations.
773822
*

‎tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function filter_set_theme_root() {
7070
* @ticket 61720
7171
* @ticket 61858
7272
* @ticket 64974
73+
* @ticket 66094
7374
*
7475
* @covers ::wp_render_background_support
7576
*
@@ -282,6 +283,108 @@ public function data_background_block_support() {
282283
'expected_wrapper' => '<div>Content</div>',
283284
'wrapper' => '<div>Content</div>',
284285
),
286+
'background clip alone does not add has-background' => array(
287+
'theme_name' => 'block-theme-child-with-fluid-typography',
288+
'block_name' => 'test/background-clip-is-output',
289+
'background_settings' => array(
290+
'backgroundClip' => true,
291+
),
292+
'background_style' => array(
293+
'backgroundClip' => 'border-box',
294+
),
295+
'expected_wrapper' => '<div style="background-clip:border-box;-webkit-text-fill-color:currentColor;">Content</div>',
296+
'wrapper' => '<div>Content</div>',
297+
),
298+
'background clip text style is applied with vendor prefixes' => array(
299+
'theme_name' => 'block-theme-child-with-fluid-typography',
300+
'block_name' => 'test/background-clip-text-is-output',
301+
'background_settings' => array(
302+
'backgroundClip' => true,
303+
),
304+
'background_style' => array(
305+
'backgroundClip' => 'text',
306+
),
307+
'expected_wrapper' => '<div style="background-clip:text;-webkit-background-clip:text;-webkit-text-fill-color:transparent;">Content</div>',
308+
'wrapper' => '<div>Content</div>',
309+
),
310+
'background clip alone does not add has-background for block with only backgroundClip support' => array(
311+
'theme_name' => 'block-theme-child-with-fluid-typography',
312+
'block_name' => 'test/background-clip-only-support',
313+
'background_settings' => array(
314+
'backgroundClip' => true,
315+
),
316+
'background_style' => array(
317+
'backgroundClip' => 'padding-box',
318+
),
319+
'expected_wrapper' => '<p style="background-clip:padding-box;-webkit-text-fill-color:currentColor;">Content</p>',
320+
'wrapper' => '<p>Content</p>',
321+
),
322+
'background clip style is not applied if the block does not support it' => array(
323+
'theme_name' => 'block-theme-child-with-fluid-typography',
324+
'block_name' => 'test/background-clip-not-supported',
325+
'background_settings' => array(
326+
'backgroundClip' => false,
327+
),
328+
'background_style' => array(
329+
'backgroundClip' => 'text',
330+
),
331+
'expected_wrapper' => '<div>Content</div>',
332+
'wrapper' => '<div>Content</div>',
333+
),
334+
'background clip keeps has-background when a background is painted into a box' => array(
335+
'theme_name' => 'block-theme-child-with-fluid-typography',
336+
'block_name' => 'test/background-clip-with-gradient',
337+
'background_settings' => array(
338+
'gradient' => true,
339+
'backgroundClip' => true,
340+
),
341+
'background_style' => array(
342+
'gradient' => 'linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%)',
343+
'backgroundClip' => 'padding-box',
344+
),
345+
'expected_wrapper' => '<div class="has-background" style="background-image:linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%);background-clip:padding-box;-webkit-text-fill-color:currentColor;">Content</div>',
346+
'wrapper' => '<div>Content</div>',
347+
),
348+
'background clipped to text drops has-background from a gradient' => array(
349+
'theme_name' => 'block-theme-child-with-fluid-typography',
350+
'block_name' => 'test/background-clip-text-with-gradient',
351+
'background_settings' => array(
352+
'gradient' => true,
353+
'backgroundClip' => true,
354+
),
355+
'background_style' => array(
356+
'gradient' => 'linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%)',
357+
'backgroundClip' => 'text',
358+
),
359+
'expected_wrapper' => '<div style="background-image:linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%);background-clip:text;-webkit-background-clip:text;-webkit-text-fill-color:transparent;">Content</div>',
360+
'wrapper' => '<div>Content</div>',
361+
),
362+
'background clipped to text keeps has-background when the block does not support the clip' => array(
363+
'theme_name' => 'block-theme-child-with-fluid-typography',
364+
'block_name' => 'test/background-clip-text-without-clip-support',
365+
'background_settings' => array(
366+
'gradient' => true,
367+
'backgroundClip' => false,
368+
),
369+
'background_style' => array(
370+
'gradient' => 'linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%)',
371+
'backgroundClip' => 'text',
372+
),
373+
'expected_wrapper' => '<div class="has-background" style="background-image:linear-gradient(135deg,rgb(255,0,0) 0%,rgb(0,0,255) 100%);">Content</div>',
374+
'wrapper' => '<div>Content</div>',
375+
),
376+
'background clip style is appended to existing styles' => array(
377+
'theme_name' => 'block-theme-child-with-fluid-typography',
378+
'block_name' => 'test/background-clip-appends-styles',
379+
'background_settings' => array(
380+
'backgroundClip' => true,
381+
),
382+
'background_style' => array(
383+
'backgroundClip' => 'content-box',
384+
),
385+
'expected_wrapper' => '<div class="wp-block-test" style="color: red;background-clip:content-box;-webkit-text-fill-color:currentColor;">Content</div>',
386+
'wrapper' => '<div class="wp-block-test" style="color: red">Content</div>',
387+
),
285388
);
286389
}
287390

0 commit comments

Comments
 (0)